Dear Readers,
2. add(int index,Object object)
This method add object at given index number.
Example :
3. remove(int index)
This method removes element from specified index.
Example:
4. remove(Object object)
This method will search specified object in list if object exist in list then it will remove it and return true otherwise it return false.
Example:
5. Sorting of ArrayList
To sort arrayList, we can use java.util.Collections class inbulit method sort.
Example :
6. size()
This method will return the no. of elements in arrayList.
Example :
7. get(int index)
This method will return the element from given index.
Example :
8. contains(Object object)
This method will return true if object present in arrayList otherwise return false.
Example :
Complete Code
In my last post I presented overview of ArrayList. Now I am writing some common functions which are often use while using ArrayList.
1.add()
We can add element by using add(Object object) method.
Example:
2. add(int index,Object object)
This method add object at given index number.
Example :
3. remove(int index)
This method removes element from specified index.
Example:
This method will search specified object in list if object exist in list then it will remove it and return true otherwise it return false.
Example:
5. Sorting of ArrayList
To sort arrayList, we can use java.util.Collections class inbulit method sort.
Example :
6. size()
This method will return the no. of elements in arrayList.
Example :
7. get(int index)
This method will return the element from given index.
Example :
8. contains(Object object)
This method will return true if object present in arrayList otherwise return false.
Example :
Complete Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | package com.javasuitor.examples; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; public class ArrayListDemo { public static void main(String[] args) { List<String> countryList = new ArrayList<String>(); // add() method countryList.add(new String("India")); countryList.add(new String("England")); countryList.add(new String("Australia")); countryList.add(new String("Japan")); countryList.add(new String("China")); // We can add object at specified index. // Here Iraq will add on index 2 of countryList. countryList.add(2,new String("Iraq")); System.out.println("After add operations"); displayList(countryList); // remove(int index) method removes element from given index // this will remove England from countryList. countryList.remove(1); // remove (Object object) method countryList.remove(new String ("Japan")); System.out.println("\nAfter remove operation "); displayList(countryList); // To perform sorting on ArrayList we can use Collection class's inbuilt method Collections.sort(countryList); System.out.println( "\nAfter sorting "); displayList(countryList); // To get number of elements exist in list we use size() method. System.out.println("\ncountryList size = "+countryList.size()); // To get element from desired index we can use get method of ArrayList System.out.println("\nElement at index 3 : "+countryList.get(3)); // contains() method to check whether element is present in list or not System.out.println("Is India present in list : "+countryList.contains(new String("India"))); } // method shows the contents of ArrayList public static void displayList(List<String> list){ // We are using iterator to display contents of list Iterator<String> iterator = list.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } } } |
No comments:
Post a Comment