Java TOPIC 47 – ARRAYLISTS LESSON NOTE JAVA
ARRAYS = STATIC SIZE Arrays in java have a static size.
That means that they cannot be resized. If you ever had to remove an element from an array, it would require
you to create an entirely new array and copy the content of one array to the
other. Similarly, adding an element to
an array would also require that you create a new array and copy the content
from the original array to the new one.
Doing this is quite tedious and can be very inefficient. ARRAYLISTS Luckily, in Java we can use ArrayLists. They
are essentially arrays that can be resized. It is important to note that an ArrayList object is really built on top of a normal
static array but that the work required to change its size is hidden away
from us. For now, we will not worry about the efficiency of using ArrayLists. Such
details will be discussed in grade 12. We will simply focus on how we can use them. CREATING
AN ARRAYLIST Before creating an ArrayList, we need the
import statement: import java.util.ArrayList; Creating an ArrayList requires that we
specify the type of data that we want to store in the structure. This is done using generics – something we
will learn about next year. For generics, instead of using int, we use Integer and instead of using
double, we use Double. ArrayList<Integer>
ali = new ArrayList<Integer>(); Here is how we create a double ArrayList: ArrayList<Double>
ald = new ArrayList<Double>(); Here is how we create a Point ArrayList: ArrayList<Point>
points = new ArrayList<Point>(); INITIAL SIZE When
an ArrayList is created, it contains no
elements. Therefore, its initial size
is zero. ADDING ELEMENTS We
can add elements by using the add method. EXAMPLE Create
an integer ArrayList called numbers and insert the
numbers 3, 5 and 8 in it. SOLUTION ArrayList<Integer>
numbers = new ArrayList<Integer>(); numbers.add(5); numbers.add(8); OUTPUTTING THE CONTENT
TO SCREEN We
can simply output the content of an ArrayList by
including it in a sysout statement. This makes use of the ArrayList’s
toString()
method. EXAMPLE
ArrayList<Integer>
al = new ArrayList<Integer>(); al.add(4); al.add(6); System.out.println(al); The
above code will output the following: [4, 6] ADDING AN ELEMENT AT A
SPECIFIC LOCATION We
can add an element at a specific location using the add method and specifying the
index number as the first argument. EXAMPLE Demonstrate
how one can insert an element at a specific location in an ArrayList.
ArrayList<Integer>
al = new ArrayList<Integer>(); al.add(5); al.add(8); al.add(1, 3); //add the value 3 at index 1 System.out.println(al);
We
can get the size of an ArrayList using the size method. EXAMPLE Create
an integer ArrayList called n, add a few elements
and then output the size to screen. SOLUTION ArrayList<Integer>
n = new ArrayList<Integer>(); REMOVING AN ELEMENT We
can remove an element based on its index number by using the remove method. EXAMPLE CODE The
following code will create an ArrayList, add some
elements and then remove element 1 from the list. ArrayList<Integer>
al = new ArrayList<Integer>(); al.add(5); al.add(8); al.add(3); al.add(2); System.out.println(al); al.remove(1); System.out.println(al); The
code above will output the following: [5, 8, 3, 2] [5, 3, 2] GETTING THE VALUE OF AN
ELEMENT We
can get the value of an element with the get method. EXAMPLE CODE The
following code will display the content of element 0 onto the screen. ArrayList<Integer>
al = new ArrayList<Integer>(); al.add(2); al.add(4); System.out.println(al.get(0)); CLEARING
THE ARRAYLIST Given
an ArrayList name al, we can remove all the elements from al by using: al.clear(); |
|
|