ARRAYLISTS
ARRAYLISTS:
Introduction:
When we work with arrays in Java, we’ve been limited by the fact that once an array is created, it has a fixed size. We can’t add or remove elements.
But what if we needed to add to the book lists, newsfeeds, and other structures we were using arrays to represent?
To represent dynamic lists, we can use Java’s
ArrayList
s. ArrayList
s allow us to:- Store elements of the same type (just like arrays)
- Access elements by index (just like arrays)
- Add elements
- Remove elements
Remember how we had to import
java.util.Arrays
in order to use additional array methods? To use an ArrayList
at all, we need to import them from Java’s util
a package as well:import java.util.ArrayList;Creating ArrayLists
To create anArrayList
, we need to declare the type of objects it will hold, just as we do with arrays:
ArrayList<String> babyNames;We use angle brackets<
and>
to declare the type of theArrayList
. These symbols are used for generics. Generics are a Java construct that allows us to define classes and objects as parameters of anArrayList
. For this reason, we can’t use primitive types in anArrayList
:
// This code won't compile: ArrayList<int> ages; // This code will compile: ArrayList<Integer> ages;The<Integer>
generic has to be used in anArrayList
instead. You can also use<Double>
and<Char>
for types you would normally declare asdouble
s orchar
s.We can initialize to an emptyArrayList
using thenew
keyword:
// Declaring: ArrayList<Integer> ages; // Initializing: ages = new ArrayList<Integer>(); // Declaring and initializing in one line: ArrayList<String> babyNames = new ArrayList<String>();Adding an Item
siNow we have an emptyArrayList
, but how do we get it to store values?ArrayList
comes with anadd()
method that takes an argument to add to the end of theArrayList
:ArrayList<Integer> sudokuRow1 = new ArrayList<Integer>(); sudokuRow1.add(4); // sudokuRow1 now holds [4] sudokuRow1.add(8); // sudokuRow1 now holds [4, 8] sudokuRow1.add(3); // sudokuRow1 now holds [4, 8, 3]ArrayList Size
Let’s say we have anArrayList
that stores items in a user’s online shopping cart. As the user navigates through the site and adds items, their cart grows bigger and bigger.If we wanted to display the number of items in the cart, we could find the size of it using thesize()
method:
ArrayList<String> shoppingCart = new ArrayList<String>(); shoppingCart.add("Slim fir Jean"); System.out.println(shoppingCart.size()); // 1 is printed shoppingCart.add("T-Shirt"); System.out.println(shoppingCart.size()); // 2 is printed shoppingCart.add("White Shoes"); System.out.println(shoppingCart.size()); // 3 is printedIn dynamic objects likeArrayList
s, it’s important to know how to access the amount of objects we have stored.Accessing an Index
With arrays, we can use bracket notation to access a value at a particular index:
double[] ratings = {3.2, 2.5, 1.7}; System.out.println(ratings[1]);This code prints2.5
, the value at index1
of the array.ForArrayList
s, bracket notation won’t work. Instead, we use the methodget()
to access an index:
ArrayList<String> shoppingCart = new ArrayList<shoppingCart>(); shoppingCart.add("Slim fir Jean"); shoppingCart.add("T-shirt"); shoppingCart.add("White Shoes"); System.out.println(shoppingCart.get(2));This code prints"Magnifying Glass"
, which is the value at index 2 of theArrayList
.Changing a Value
When we were using arrays, we could rewrite entries by using bracket notation to reassign values:
String[] shoppingCart = {"Slim fit Jean", "T-shirt", "White shoes"}; shoppingCart[0] = "Black Jean"; // shoppingCart now holds ["Black Jean", "Tweed Houndstooth Hat", "Magnifying Glass"]ArrayList
has a slightly different way of doing this, using theset()
method:
ArrayList<String> shoppingCart = new ArrayList<shoppingCart>(); shoppingCart.add("Black Jean"); shoppingCart.add("T-shirt"); shoppingCart.add("White shoes"); shoppingCart.set(0, "Black Jeans"); // shoppingCart now holds ["Black Jeans", "T-shirt", "White shoes"]Removing an Item
What if we wanted to get rid of an entry altogether? For arrays, we would have to make a completely new array without the value.Luckily,ArrayList
s allow us to remove an item by specifying the index to remove:
ArrayList<String> shoppingCart = new ArrayList<String>(); shoppingCart.add("Black Jeans"); shoppingCart.add("T-shirt"); shoppingCart.add("White shoes"); shoppingCart.remove(1); // shoppingCart now holds ["T-shirt", "White shoes"]We can also remove an item by specifying the value to remove:
ArrayList<String> shoppingCart = new ArrayList<String>(); shoppingCart.add("Black Jeans"); shoppingCart.add("T-shirt"); shoppingCart.add("White shoes"); shoppingCart.remove("Black Jeans"); // shoppingCart now holds ["T-shirt", "White shoes"]Note: This command removes the FIRST instance of the value"Black Jeans"
.
No comments: