"Here I am."
"Hi, Ellie!"
"Today we'll talk about an interesting topic. I'm going to tell you about the ArrayList class."
"A new class? Cool! What can it do?"
"Let me start with the back story. The only thing programmers dislike about arrays is that you can't change their size. What do you do if you need to add three more elements to an array that only has one free slot?"
"The only solution to this problem is to create very big arrays, in order to guarantee you have enough room for all the elements. However, this often means wasted memory. If an array usually contains three elements, but there is even the smallest chance that it might need to accommodate 100 elements, you have to create a 100-element array."
"So, what did programmers come up with?"
"They wrote the ArrayList class, which does the same thing as an Array, but it can change its size."
"Interesting move. How did they do that?"
"Every ArrayList object stores a regular array of elements. When you read elements from an ArrayList, it reads them from its inner array. When you write them to the ArrayList, it writes them to its inner array. Here, compare these columns:"
Array | ArrayList |
---|---|
Create a container for elements | |
|
|
Get the number of elements | |
|
|
Get an element from an array/collection | |
|
|
Write an element into an array | |
|
|
"So, why is the ArrayList better? As far as I can tell, the code is now longer."
"First, the ArrayList supports several additional operations that programmers have to perform all the time. An ordinary array doesn't support these operations. For example, inserting or deleting elements from the middle of an array without leaving holes."
"Second, the ability to change the size of the array. When you need to add one more element but the internal array doesn't have any free slots, here is what happens inside the ArrayList:
a) Another array is created that is 50% bigger than current inner array, plus one element.
b) All elements from the old array are copied into the new one.
c) The new array is saved as the ArrayList object's inner array. The old array is declared garbage (we simply stop storing a reference to it)."
Array | ArrayList |
---|---|
Add an element at the end of the array | |
This action is not supported |
|
Add an element in the middle of the array | |
This action is not supported |
|
Add an element at the beginning of the array | |
This action is not supported |
|
Delete an element from the array | |
We could delete an element with list[3] = null . But this would leave a 'hole' in the array. |
|
"How do we work with this ArrayList?"
"Actually, just as we do with an ordinary array. Look. Let's compare working with an ArrayList to working with an array. Suppose we need to 'read in 10 strings and display them on the screen in reversed order'."
"Look at this:
public static void main(String[] args)
{
Reader r = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(r);
// Read strings from the keyboard
String[] list = new String[10];
for (int i = 0; i < list.length; i++)
{
String s = reader.readLine();
list[i] = s;
}
// Display the contents of the array
for (int i = 0; i < list.length; i++)
{
int j = list.length - i - 1;
System.out.println( list[j] );
}
}
public static void main(String[] args)
{
Reader r = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(r);
// Read strings from the keyboard
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < 10; i++)
{
String s = reader.readLine();
list.add(s);
}
// Display the contents of the collection
for (int i = 0; i < list.size(); i++)
{
int j = list.size() - i - 1;
System.out.println( list.get(j) );
}
}
I've used the same color to highlight similar actions in each column."
"On the one hand, everything is different. On the other, it's still the same."
"Right. Except that we don't use square brackets when working with an ArrayList. Instead, we use get, set and add methods."
"Yes, I gathered that much. Still, it looks very much the same."
GO TO FULL VERSION