The fifth chapter uses the Java function library ArrayList
add(Object elem)
remove(int index)
remove(Object elem)
contains(Object elem)
isEmpty()
indexOf(Object elem)
size()
get(int index)
The difference between a ArrayList and a general array
ArrayList
ArrayList myList = new ArrayList ();
String a = new String("whoohoo");myList.add(a);String b = new String("Frog");myList.add(b);int theSize = myList.size();Object o = myList.get(1);myList.remove(1);
General Array
Boolean isIn = Mylist.contains (b); string[] myList = new string[2]; String a = new string ("Whoohoo"); mylist[0]=a; String b = new String ("Frog"); Mylist[1]=b;int thesize = Mylist.length; String o = Mylist[1];mylist[1]=null;boolean isIn = false;for (string item:mylist) {if (B.equals (item)) {IsIn = true; Break }}
- When created in a generic array, the size must be determined, but ArrayList only needs to create objects of this type at the row. You do not need to specify a size.
- Location must be specified when storing objects to a generic array
Q&a
Will using import make the program larger, and will the compilation process wrap the package or class in?
Using import only saves you the package name in front of each class, and the program does not become larger and slower because of the import.
Why import into a string class or system class is not required
Remember that Java.lang is a pre-quoted package.
Head First Java reading notes (vi) Understanding the Java API