|
The ArrayList class is a concrete implementation of List interface. This class supports dynamic arrays that can grow as needed. In java, standard arrays are of fixed length. After arrays are created, they cannot grow or shrink, which means you must know in advance how many elements an array will hold. However, sometimes you may not know how large an array you need. To handle this situation, collection framework has defined ArrayList class. An object of this can dynamically increase or decrease in size.
import java.util.ArrayList;
public class AraryListDemo {
public static void main(String[] args) {
ArrayList al = new ArrayList();
System.out.print("Initial size of al : " + al.size());
System.out.print("\n");
//add.elements to the array list
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
al.add(1,"A2");//inserts objects "A2" into array at index 1
System.out.print("size of al after additions " + al.size());
System.out.print("\n");
//display the array list
System.out.print("contents of al: " + al );
System.out.print("\n");
//Remove elements from the array list
al.remove("F");
al.remove(2);
System.out.print("size of after deletions : " + al.size());
System.out.print("\n");
System.out.print("contents of al:" + al);
}
}
|
Output Screen:
Initial size of al: 0
size of al after additions 7
contents of al: [C, A2, A, E, B, D, F]
size of after deletions : 5
contents of al:[C, A2, E, B, D]
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.