Java Tutorial
ArrayList()This constructor builds an empty array list.
ArrayList(Collection collection)This constructor builds an array list that is initialized with the elements of the collection.
ArrayList(int capacity)This constructor builds an array list that has specified the initial capacity. here capacity is the size of the underlying array that is used to store the elements. Capacity grows automatically as elements are added to an array list.
boolean add(Object)adds new object in the array list.
boolean add(int, Object)adds new object at the specified index of array list.
boolean addAll(Collection)
boolean addAll(int, Collection)adds the collection of objects starting from the specified index of array list. Returns true if object was added to the ArrayList, otherwise false.
void clear()
boolean contails(Object)
boolean containsAll(Collection)
boolean equals(Object)
int hashCode()
boolean isEmpty()
Iterator iterator()
boolean remove(Object)
boolean removeAll(Collection)
boolean retainAll(Collection)
int size()
Object[] toArray()
// Codingpointer Demonstrates ArrayList import java.util.*; class ArrayListTest { public static void main(String args[]) { // Creates ArrayList ArrayList arrList = new ArrayList(); System.out.println("Array size default: " + arrList.size()); // add array list elements. arrList.add(1); arrList.add(2); arrList.add(3); arrList.add(4); arrList.add(5); arrList.add(6); arrList.add(2, 4.5); System.out.println("Size of array list: "+ arrList.size()); // prints array list elements System.out.println("Elements in array list: " + arrList); } }Output:
$ javac ArrayListTest.java Note: ArrayListTest.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. $ java ArrayListTest Array size default: 0 Size of array list: 7 Elements in array list: [1, 2, 4.5, 3, 4, 5, 6]
// Codingpointer Demonstrates ArrayList import java.util.*; class ArrayListTest { public static void main(String args[]) { // Creates ArrayList ArrayList arrList = new ArrayList(); System.out.println("Array size default: " + arrList.size()); // add array list elements. arrList.add("1"); arrList.add("1"); arrList.add("3"); arrList.add("4"); arrList.add("5"); arrList.add("6"); arrList.add(2, "4.5"); System.out.println("Size of array list: "+ arrList.size()); // prints array list elements System.out.println("Elements in array list: " + arrList); // remove array list elements arrList.remove("1"); arrList.remove(4); System.out.println("Size of array list after deletions: "+arrList.size()); System.out.println("Elements in array list: " + arrList); } }Output:
$ java ArrayListTest Array size default: 0 Size of array list: 7 Elements in array list: [1, 1, 4.5, 3, 4, 5, 6] Size of array list after deletions: 5 Elements in array list: [1, 4.5, 3, 4, 6]
// Codingpointer Demonstrates ArrayList import java.util.*; class ArrayListTest { public static void main(String args[]) { // Creates ArrayList ArrayList arrList = new ArrayList(); System.out.println("Array size default: " + arrList.size()); // add array list elements. arrList.add(new Integer(5)); arrList.add(new Integer(1)); arrList.add(new Integer(3)); arrList.add(new Integer(2)); arrList.add(new Integer(4)); System.out.println("Size of array list: "+ arrList.size()); // prints array list elements System.out.println("Elements in array list: " + arrList); // get array Object arr[] = arrList.toArray(); int sum = 0; // sum array for(int i=0; i<arr.length; i++) { sum += ((Integer) arr[i]).intValue(); } System.out.println("Sum is: " + sum); } }Output:
$ java ArrayListTest Array size default: 0 Size of array list: 5 Elements in array list: [5, 1, 3, 2, 4] Sum is: 15« Previous Next »
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page