Skip to main content
Monday is APCS after school work/makeup day!

APCS

Go Search
Linda Noss
APCS
Java
VGP
Web Page Design
Consumer Math
Desktop Publishing
See your grade
  

Monday is APCS after school work/makeup day!

ArrayList full API

An ArrayList is a group of objects all of the same type.

The length of an Arraylist is dynamic, meaning that an arraylist can expand and contract WHILE the program is running.

  • An arraylist can only hold lists of object, no primative types(int, double, boolean) 
  • The objects are immutable whic h means they cannot be changed once they are created

Declaration

The array is declare the same way as an Array except you use the word ArrayList instead of Array:

ArrayList<object> myList = new ArrayList<object>();

for example:

private ArrayList<String> myList= new ArrayList<String>();

Setting Values

Because you can add values to an arrayList as the program is running you have to use the add() method to fill the arrayList:


myList.add("DotNetSpider");

This one puts 1032 in the i th position in the list, shifting all others up as necessary.

myList.add(i,1032);  

You can also remove items from the list

myList.remove("DotNetSpider");

There are also many more methods. To see the full list please click on link below


See the arrayList tutorial and API

You will also see some unique loops that can be used with arrayLists

foreach loop. This is fast and works for all kinds of lists, but is not entirely flexible (only sequential forward with no deletions, additions, or multiple references). This should be your first choice in programming. Works efficiently with both ArrayList and LinkedList.

ArrayList<String> a = new ArrayList<String>();
. . .
for (String s : a) {
    System.out.println(s);
}