How to use List:
1. Creating List
List<String> stringList = new ArrayList<String>();
2. Putting an Item into ArrayList
stringList.add("Item");
//no error because we are storing String
stringList.add(new Integer(2));
//compilation error
3. Checking size of ArrayList
int size = stringList.size();
4. Checking Index of an Item in Java ArrayList
You can use indexOf() method of ArrayList in Java to find out index of a particular object.
int index = stringList.indexOf("Item");
//location of Item object in List
5. Retrieving Item from ArrayList in a loop
Many a times we need to traverse on Java ArrayList and perform some operations on each retrieved item. Here are two ways of doing it without using Iterator. We will see use of Iterator in next section.
for (int i = 0; i < stringList.size(); i++)
String item = stringList.get(i);
System.out.println("Item " + i + " : " + item);
}
or
for(String item: stringList){
System.out.println("retrieved element: " + item);
}
6. Checking if ArrayList is Empty
boolean result = stringList.isEmpty(); //isEmpty() will return true if List is empty
or
if(stringList.size() == 0){
System.out.println("ArrayList is empty");
}
7. Removing an Item from ArrayList
There are two ways to remove any elements from ArrayList in Java. You can either remove an element based on its index or by providing object itself. Remove remove (int index) and remove (Object o) method is used to remove any element from ArrayList in Java.
stringList.remove(0);
stringList.remove(item);
8. Copying data from one ArrayList to another ArrayList in Java
ArrayList<String> copyOfStringList = new ArrayList<String>();
copyOfStringList.addAll(stringList);
9. Replacing an element at a particular index
Below code will replace first element of stringList from "Item" to "Item2".
stringList.set(0,"Item2");
10. Clearing all data from ArrayList
ArrayList in Java provides clear() method which removes all of the elements from this list. Below code will remote all elements from our stringList and make the list empty.
stingList.clear();
11. Converting from ArrayList to Array in Java
Java ArrayList provides you facility to get the array back from your ArrayList. You can use toArray(T[] a) method returns an array containing all of the elements in this list in proper sequence (from first to last element). "a" is the array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
String[] itemArray = new String[stringList.size()];
String[] returnedArray = stringList.toArray(itemArray);
12. How to convert an array to ArrayList in Java - Using Arrays.asList() method
ArrayList stringList = Arrays.asList(new String[]{"One", "Two", "Three");
13. Sorting elements of ArrayList in Java
You can use Collections.sort(List list) method to sort a Java ArrayList in natural order defined by Comparable interface and can use Collections.sort(List list, Comparator c) method to sort your Java ArrayList based upon custom Comparator.
This information was taken from
here
Немає коментарів:
Дописати коментар