Search an element of Java ArrayList Example

  1. /*
  2.   Search an element of Java ArrayList Example
  3.   This Java Example shows how to search an element of java
  4.   ArrayList object using contains, indexOf and lastIndexOf methods.
  5. */
  6.  
  7. import java.util.ArrayList;
  8.  
  9. public class SearchAnElementInArrayListExample {
  10.  
  11. public static void main(String[] args) {
  12.  
  13. //create an ArrayList object
  14. ArrayList arrayList = new ArrayList();
  15.  
  16. //Add elements to Arraylist
  17. arrayList.add("1");
  18. arrayList.add("2");
  19. arrayList.add("3");
  20. arrayList.add("4");
  21. arrayList.add("5");
  22. arrayList.add("1");
  23. arrayList.add("2");
  24.  
  25. /*
  26.   To check whether the specified element exists in Java ArrayList use
  27.   boolean contains(Object element) method.
  28.   It returns true if the ArrayList contains the specified objct, false
  29.   otherwise.
  30.   */
  31.  
  32. boolean blnFound = arrayList.contains("2");
  33. System.out.println("Does arrayList contain 2 ? " + blnFound);
  34.  
  35. /*
  36.   To get an index of specified element in ArrayList use
  37.   int indexOf(Object element) method.
  38.   This method returns the index of the specified element in ArrayList.
  39.   It returns -1 if not found.
  40.   */
  41.  
  42. int index = arrayList.indexOf("4");
  43. if(index == -1)
  44. System.out.println("ArrayList does not contain 4");
  45. else
  46. System.out.println("ArrayList contains 4 at index :" + index);
  47.  
  48. /*
  49.   To get last index of specified element in ArrayList use
  50.   int lastIndexOf(Object element) method.
  51.   This method returns index of the last occurrence of the
  52.   specified element in ArrayList. It returns -1 if not found.
  53.   */
  54.  
  55. int lastIndex = arrayList.lastIndexOf("1");
  56. if(lastIndex == -1)
  57. System.out.println("ArrayList does not contain 1");
  58. else
  59. System.out.println("Last occurrence of 1 in ArrayList is at index :"
  60. + lastIndex);
  61.  
  62. }
  63. }
  64. /*
  65. Output would be
  66. Does arrayList contain 2 ? true
  67. ArrayList contains 4 at index :3
  68. Last occurrence of 1 in ArrayList is at index :5
  69. */

Java ArrayList Comparison.

  1. /*
  2.  Check if subset contains subset arraylist
  3.  
  4.  */
  5. import java.util.ArrayList;
  6. public class Test {
  7. public static void main(String[] args){
  8. ArrayList list1 = new ArrayList();
  9. ArrayList list3 = new ArrayList();
  10. ArrayList list6 = new ArrayList();
  11. list1.add(10);
  12. list1.add(20);
  13. list1.add(32);
  14. //list1.add(40);
  15. ArrayList list2 = new ArrayList();
  16. list2.add(11);
  17. list2.add(21);
  18. list2.add(31);
  19. //list2.add(41);
  20. ArrayList list4 = new ArrayList();
  21. list4.add(10);
  22. list4.add(20);
  23. list4.add(30);
  24. list4.add(40);
  25. list4.add(50);
  26. list4.add(60);
  27. list4.add(32);
  28. //list4.add(10);
  29. ArrayList list5 = new ArrayList();
  30. list5.add(11);
  31. list5.add(21);
  32. list5.add(31);
  33. list5.add(41);
  34. list5.add(51);
  35. list5.add(61);
  36. list5.add(31);
  37. //list5.add(11);
  38. int iSize = 0;
  39. int iSize1 = 0;
  40. String str = "";
  41. boolean blnFound;
  42. if (list1.size()== list2.size()){
  43. iSize = list1.size();
  44. for (int i=0; i < iSize; i++){
  45. list3.add(list1.get(i)+"."+list2.get(i));
  46. }
  47. System.out.println("New Array list3 : "+list3);
  48. }else {
  49. System.out.println(" Array1 Does not match Array2");
  50. }
  51. if (list4.size()== list5.size()){
  52. iSize1 = list4.size();
  53. for (int i=0; i < iSize1; i++){
  54. list6.add(list4.get(i)+"."+list5.get(i));
  55. }
  56. System.out.println("New Array list6 : "+list6);
  57. }else {
  58. System.out.println(" Array4 Does not match Array5");
  59. }
  60. // Check if array6 contains array3
  61. if(list1.size()== list2.size() && list4.size()== list5.size()){
  62. for (int i=0; i< list3.size(); i++){
  63. blnFound = list6.contains(list3.get(i));
  64. if(blnFound){
  65. str="True";
  66. }
  67. else{
  68. str="False";
  69. break;
  70. }
  71. }
  72. System.out.println("Result of Array3 Contains in Array6 is :"+str);
  73. }
  74. }
  75. }

Post new comment

To combat spam, please enter the code in the image.


Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!