Advertisement
1) ArrayList() |
---|
This constructor creates an empty ArrayList of a particular object type. Example - |
ArrayList<Integer> arrL= new ArrayList<Integer>();
2) ArrayList(Collection <? extends E>c) |
---|
This constructor creates an ArrayList initialized with the elements of Collection c. Collection c must hold similar type of objects as the type declared by the ArrayList. |
ArrayList<Integer> arrL1 = new ArrayList<Integer>():
ArrayList<Integer> arrL2 = new ArrayList<Integer>(arrL1);
Methods | Description |
---|---|
boolean add(E e) | This method adds the specified element e to the end of an ArrayList. |
add(int index, E element | This method adds the element at the specified index in an ArrayList. |
int size() | This method returns the total number of elements in an ArrayList. |
void clear() | This method removes all of the elements from the ArrayList |
E get(int index) | This method returns the element at the specified index in an ArrayList. |
contains(Object o) | This method checks if ArrayList contains a specific Object. |
E remove(int index) | This method removes an element at a specified index in an ArrayList. |
boolean remove(Object o) | This method removes the first occurrence of an Object from the ArrayList, if it is present. |
Object[] toArray() | This method returns an Object array containing all the elements of an ArrayList. |
Iterator |
This method returns an Iterator to iterate over elements of an ArrayList. |
ListIterator |
This method returns a ListIterator to iterate over elements of an ArrayList. |
Advertisement
//Java - Example of ArrayList
import java.util.*;
class ArrayList1
{
public static void main(String... ar)
{
ArrayList<String> arr= new ArrayList<String>();
System.out.println("Initial size of ArrayList = "+ arr.size());
arr.add("C");
arr.add("A");
arr.add("4");
arr.add("F");
arr.add("x");
arr.add("1");
System.out.println("ArrayList after adding objects = " + arr);
System.out.println("Size of ArrayList = "+ arr.size());
arr.remove(4); //Removing an element at the 4th index
System.out.println("ArrayList afer removing element at 4th index = "+ arr);
arr.remove("1"); //Removing an element "1"
System.out.println("ArrayList afer removing element 1 = "+ arr);
System.out.println("Size of ArrayList = "+ arr.size());
}
}
Initial size of ArrayList = 0
ArrayList after adding objects = [C, A, 4, F, x, 1]
Size of ArrayList = 6
ArrayList afer removing element at 4th index = [C, A, 4, F, 1]
ArrayList afer removing element 1 = [C, A, 4, F]
Size of new ArrayList = 4
//Java - Example of ArrayList
import java.util.*;
class ArrayList1
{
public static void main(String... ar)
{
ArrayList<Integer> array1= new ArrayList<Integer>();
array1.add(4);
array1.add(1);
array1.add(5);
array1.add(2);
array1.add(3);
System.out.println("ArrayList after adding objects = " + array1);
System.out.println("Size of ArrayList = "+ array1.size());
System.out.println("Creating a new ArrayList created from an existing one");
ArrayList<Integer> array2= new ArrayList<Integer>(array1);
System.out.println("Contents of new ArrayList : " + array2);
System.out.println("Size of new ArrayList = "+ array2.size());
}
}
ArrayList after adding objects = [4, 1, 5, 2, 3]
Size of ArrayList = 5
Creating a new ArrayList created from an existing ArrayList
Contents of new ArrayList : [4, 1, 5, 2, 3]
Size of new ArrayList = 5
//Java - Example of ArrayList
import java.util.*;
class ArrayList1
{
public static void main(String... ar)
{
ArrayList<Integer> array1= new ArrayList<Integer>();
array1.add(4);
array1.add(1);
array1.add(5);
array1.add(2);
array1.add(3);
System.out.println("ArrayList after adding objects = " + array1);
System.out.println("Size of ArrayList = "+ array1.size());
//Converting an ArrayList to an Object array.
Object[] a = array1.toArray();
System.out.print("Contents of an Object Array created from ArrayList - ");
//Iterating over the elements of Object array
for(Object element : a)
System.out.print(element + " " );
}
}
ArrayList after adding objects = [4, 1, 5, 2, 3]
Size of ArrayList = 5
Contents of an Object Array after created from ArrayList - 4 1 5 2 3
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement