Today we’ll be examining ArrayLists. I had once covered arrays, but ArrayLists are different. ArrayLists unlike Arrays are resize-able after creation, making them dynamic and able to adapt to unknown and unexpected size ranges.
What exactly are ArrayLists though and what do they look like? ArrayLists are part of the Java Collection framework. The java.util package contains all the classes and interfaces for the Collection framework, making collection types outside of the standard built-in array easy to find or keep track of for import.
I will be creating code to demonstrate usage of an ArrayList and what you can do with one with the example of a list of test grades.
ArrayList<Integer> grades = new ArrayList<Integer>(); // create ArrayList named grades that stores integers
First we create our ArrayList. A new ArrayList Object, we name it grades for its imminent purpose.
grades.add(100); // add element to ArrayList
grades.set(0, 0);
grades.remove(0);
grades.clear();
grades.size
We may use all sorts of different ArrayList Methods.
- add – add element to ArrayList
- set – adjust element of ArrayList; first 0 is index, second is integer value, String, or whichever immutable form you choose for your ArrayList
- remove – remove index-specified element from ArrayList
- clear – remove all elements from ArrayList
- size – display current size of ArrayList
Let’s make this program more substantial, adding additional methods from import to use and really flesh out our program.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class Array_List {
public static void main(String[] args) {
...
Random randNo = new Random(); // declare/establish java.util.Random object
grades.add(randNo.nextInt(40) + 60);
We import ArrayList as needed, Random class for randomization, and Collections to call sort() method to sort our numerical data.
int group_size = 1;
do {
grades.add(randNo.nextInt(40) + 60);
group_size++;
} while (group_size < 19);
Rather than entering in the ArrayList’s add() method 19 more times, let’s make a do-while loop that iterates until it populates additional grades for 20 total grades. Big class! Notice the 40 and 60. This tells my Random class Object randNo to generate a number between 1 and 40, add 60 to it, then give the sum as an object to the ArrayList. Finally, we sort it, then print it.
Collections.sort(grades);
for (int iterator : grades)
{
System.out.print(iterator + " ");
}
We create a for-each loop to iterate throughout size of our ArrayList. Rather than use a standard for loop to declare and initialize loop iterator variable, we instead declare a variable of same type as base type of array/ArrayList, followed by a colon, which is then followed by our ArrayList name. Is more commonly used with Collections class ArrayList than array, minimizing chance of OutOfBounds exceptions.
I hope that you found this content informative, both blog and video! Please check out the full code as depicted on YouTube on my Github account.