ArrayList (List) Methods Flashcards
Declaring a ArrayList
ArrayList arraylist = new ArrayList();
Adding an Item at the List
arraylist.add(“Element”);
Adding an element at a particular position of ArrayList
arraylist.add(3,Element);
Adding all element of a list to the end of the ArrayList
arraylist.addall(list-name);
Adding a list to a particular position of ArrayList
arraylist.addall(2,list-name);
Adding a ArrayList to Another ArrayList
arraylist-first.append(arraylist-second);
Removing an Item from the List (Any Element)
arraylist.remove(“Element”);
Removing an Item from a particular position of an ArrayList
arraylist.remove(2);
Retrieving an Item from an ArrayList based on the Index
arraylist.get(5)
Retrieving a sublist from an ArrayList <i>Based on Index</i>
arraylist.sublist(2,5)
Retrieving Index of an Item
arraylist.indexOf(“Element”);
Retrieving Last Index of an Item
arraylist.lastIndexOf(“Element”);
Size of an ArrayList
arraylist.size();
Sorting an ArrayList
Collection.sort(arraylist);
Reverse Sorting an Array List
Method 1 : Collection.reverse(arraylist);
Method 2 : Collection.sort (arraylist,Collection.reverseOrder());