Java #14 – ArrayList

Java #14 – ArrayList


While an array is a convenient way to
store a lot of data, it requires you to specify the size in
advance. Sometimes, we don’t know how many
items we want to store. In this case, an ArrayList can be an
excellent choice. Basically, an ArrayList acts like an
array, except it grows automatically when it
gets full. It allows you to efficiently access anything
stored in it using a “get” method, to efficiently change the item stored
at any location using a “set” method, and to add items with an “add” method. The “size” method returns how many
items are currently in the ArrayList. The method “readIntegers” in the
class ArrayListDemo shows how to create an ArrayList
which will contain integers. ArrayList is a generic class, which
means you can specify what kinds of things will be stored in it using less
than, greater than. ArrayLists can’t store basic types,
only classes, but each basic type has a
corresponding class. For “int”, that is the class “Integer”. You’ll note Integer is capitalized,
because it is the name of a class. Java automatically converts between
“int” and “Integer” as required. As with all objects, we must call a
constructor to create one. Then, while there are still more ints
to be read from the scanner, we call the add method on “result”,
our ArrayList object, to add them. At the end, we return result. The method “reverse” demonstrates
how to use “get” and “set”. This method loops through the first
half of the array, getting one item from the first half and its corresponding item from the
second half and swapping them. We set I to 0, then move the 0th
entry to temp, move the right side to 0 and move
temp to the right side. Then I becomes 1. We move the 1st entry to temp,
move the right side to 1 and move temp to the right side.
Then I becomes 2. We move the 2nd entry to temp, move the right side to 2 and move
temp to the right side. Now the loop is over and the entries
are all reversed. The methods “print” and “alternatePrint”
show two different ways of printing all of the
elements in the ArrayList. Print uses a regular for loop that
counts from 0 to size-1, and then does a get on the ith
element of the ArrayList. “alternatePrint”, however, uses a
special kind of Java for loop, called an “iterator”. Here “Integer value colon list” means there
will be one iteration for each item in the ArrayList, and value will, in turn,
be the 0th, 1st, 2nd, though size minus 1st numbered
item in the ArrayList. Both of the loops do exactly the
same thing.

9 thoughts on “Java #14 – ArrayList”

  1. Thank you this was helpful to me.
    I'm making a program and i personally rarely use arraylist so i'm having to relearn it now that i need it. This doesn't totally help me but at least i can hit the ground running.

  2. @GutGenotype I believe vector is a shape or variable. ArrayList is like array but it stores reference variables to classes. By this i mean TestClass TC = new TestClass(); can be stored in arraylist.
    This advantage of array list is that once you reach your size limit you can still add objects to it and it will increase in size. For array you basically have to remake the array if you add to much to it then you lose all the old array data unless you save it some where.
    Don't know about vectors.

Leave a Reply

Your email address will not be published. Required fields are marked *