ArrayList Class Example 2
Import java.util.ArrayList;
public class ArrayListDemo2 {
Into
public static void Main (string[] args) {
arraylist<integer> A1 = new arraylist<integer> (); To create an empty ArrayList object
for (int i=0; i<10;i++) {
Integer a2 = new Integer (i);//Create a wrapper object of an integral type
A1.add (A2); Put this object into the ArrayList
}
Take out
System.out.println ("Elements in the array");
for (int i=0;i<a1.size (); i++) {
Integer a3 = (integer) (A1.get (i));//Get the element indexed as I in ArrayList
SYSTEM.OUT.PRINTLN (A3);
}
System.out.println ("------------");
A1.clear (); Empty
System.out.println ("The case after the array is emptied");
System.out.println ("The length of the array is" +a1.size ());
if (A1.isempty ()) {//Determine if empty
SYSTEM.OUT.PRINTLN ("Array is now empty");
}else{
SYSTEM.OUT.PRINTLN ("Array is now not empty");
}
}
}
This article is from the "Basic Java Primer Notes" blog, declined reprint!
ArrayList class Example 1