Aug 12, 2014

9. ArrayList

Unlike Arrays, ArrayList size is not set, at declaration, and elements can be added. To use ArrayList in a scrapbook, we have to right click on the jpage, select Set Imports... and add the java.util package. This is same as writing import java.util.*; in a Java application.




The Object is given inside the angle brackets. In the example, we used the built-in String, but it can be any class. The size() method gives the current size of the ArrayList.


// *** 1. Start (Constructors, size)
ArrayList<String> names1 = new ArrayList<String>();
ArrayList<String> names2 = new ArrayList<String>(10);
System.out.println("names1 size = " + names1.size());
System.out.println("names2 size = " + names2.size());
// *** 1. End



We can use the add() method to add Objects. Objects are added at end, should the index not be provided, or at index, if provided. addAll() will add a list of objects. remove(index) can be used to remove an object at given index value. The ArrayList is moved automatically so it starts at 0 and is continuous. Thus, if you add or remove near the middle, for example, half of the objects are moved. Examples, in audio filtering, will be given later, to build a more complicated application.


// *** 2. Start (add, remove)
names1.add("Mathew");
names1.add("Mark");
names1.add("Luke");
names1.add("John");
names2.addAll(names1);
names2.remove(2);
names2.add(0,"Peter");
System.out.println("names1 size = " + names1.size());
System.out.println("names2 size = " + names2.size());
// *** 2. End



The array version of the for loop can iterate over all the elements in an ArrayList.


// *** 3. Start (for)
for (String str : names1) {
 System.out.print(str + ", ");
}
System.out.println();
for (String str : names2) {
 System.out.print(str + ", ");
}
System.out.println();
// *** 3. End



If we only want the object at a particular index, we can use the get() method.


// *** 4. Start (get)
String str1 = names1.get(0);
String str2 = names2.get(3);
System.out.println("str1 = " + str1 + ", " +
       "str2 = " + str2 );
// *** 4. End



indexOf() method returns the index of the object. If not found, returns -1. The contains() method return the boolean indicating if the object is found in the ArrayList.


// *** 5. Start (indexOf, contains)
int i1 = names1.indexOf("Mark");
int i2 = names2.indexOf("Peter");
System.out.println("i1 = " + i1 + ", i2 = " + i2);
boolean b1 = names1.contains("Peter");
boolean b2 = names2.contains("Peter");
System.out.println("b1 = " + b1 + ", b2 = " + b2);
// *** 5. End



// ex9.jpage
// *** 1. Start (Constructors, size)
ArrayList<String> names1 = new ArrayList<String>();
ArrayList<String> names2 = new ArrayList<String>(10);
System.out.println("names1 size = " + names1.size());
System.out.println("names2 size = " + names2.size());
// *** 1. End
// *** 2. Start (add, remove)
names1.add("Mathew");
names1.add("Mark");
names1.add("Luke");
names1.add("John");
names2.addAll(names1);
names2.remove(2);
names2.add(0,"Peter");
System.out.println("names1 size = " + names1.size());
System.out.println("names2 size = " + names2.size());
// *** 2. End
// *** 3. Start (for)
for (String str : names1) {
 System.out.print(str + ", ");
}
System.out.println();
for (String str : names2) {
 System.out.print(str + ", ");
}
System.out.println();
// *** 3. End
// *** 4. Start (get)
String str1 = names1.get(0);
String str2 = names2.get(3);
System.out.println("str1 = " + str1 + ", " +
       "str2 = " + str2 );
// *** 4. End
// *** 5. Start (indexOf, contains)
int i1 = names1.indexOf("Mark");
int i2 = names2.indexOf("Peter");
System.out.println("i1 = " + i1 + ", i2 = " + i2);
boolean b1 = names1.contains("Peter");
boolean b2 = names2.contains("Peter");
System.out.println("b1 = " + b1 + ", b2 = " + b2);
// *** 5. End



No comments:

Post a Comment