The ArrayList Notebook
You created a nice Note class, but now you need a class to store different Notes. In this node, we'll create a simple Class called Notebook
which will contain methods for adding, retrieving and listing notes. What structure should be used in Notebook to keep track of its Notes? We've learned about Arrays, so we'll start with a simple Class that uses an array to store Notes:
public class ArrayNotebook
{
private Note[] notes;
/** Constructor for objects of class Notebook */
public ArrayNotebook()
{
notes = new Note[10];
}
}
This code declares an array variable for holding Note objects. The constructor initializes an empty Note array with 10 cells.
Limitations of Array
You're now ready to create a method to add Notes to notes
, but you right away face the limitation of the simple array - there's no easy way to just add items to an array. You would need to create an index variable just to keep track of where to add the Notes. And what happens when you need to add more than 10 notes when the array is only 10 cells large? Things would get very messy.
public class ArrayNotebook
{
private Note[] notes;
private int index;
public ArrayNotebook()
{
notes = new Note[10];
index = 0;
}
public void addNote(Note note){
if(index < notes.length){
notes[index] = note;
index++;
}
}
}
Array is machine-optimized and useful in certain cases, but it's just not powerful enough for many situations. It would be nice if Java provided a Class that made it easier to handle lists of items, so you could add and remove items without worrying about the size of the array.
ArrayList
Java does have such a class - ArrayList. However, it doesn't come "built-in" with the language, and it needs to be imported to be used. We'll cover the details later, but for now you can just type the following code at the very top of your code:
import java.util.ArrayList;
public class Notebook
{
//...
That's it. You now have access to the ArrayList class just like you created it yourself in the same project. (Except it might be a little better than what you would have created!)
Using ArrayList
Unlike the unusual syntax of the array, the ArrayList works like a regular Object. It can be declared and created with the following code:
ArrayList someList = new ArrayList();
Like String, ArrayList comes with many useful methods. To add an item to it, you can just use the add() method. For example:
someList.add(someObject);
This code will add someObject
to the end of someList
. if someList
isn't large enough to take another Object, it will automatically expand in size! So you can add objects to an ArrayList without worrying about its size or any index variable.
The Notebook class can now be updated to use ArrayList instead of Array. The addNote method can just call ArrayList's add() method in one line of code:
import java.util.ArrayList;
public class Notebook
{
private ArrayList notes;
public Notebook()
{
notes = new ArrayList();
}
public void addNote(Note note){
notes.add(note);
}
}
Task
Complete the Notebook class in BlueJ as shown above.
Challenge
Declare and create an ArrayList called fruit
in one line of code.
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Challenge
Add the String "orange"
to the ArrayList fruit
in one line of code.
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
Lukas Dancak
Nov 11, 3:53 PMHi. There is diference between code here in article and code in BlueJ project. What is the right code ?
BlueJ:
private ArrayList notes;
This Article:
private ArrayList notes;
}
Lukas Dancak
Nov 11, 3:57 PMThe answer is in next node. Sory for question.
thales
Jul 11, 9:29 AMi cant figure it out at all, can anyone help me and just give the right awnsers?
Gary
Mar 1, 12:20 PMI must be missing something obvious.... why do neither of these work:
fruit.add(orange);
fruit.add(String orange);
Learneroo
Mar 1, 8:11 PMStrings need quotes.
catypus
Jun 15, 1:23 AMWow... so apparently I don't know what I'm doing here:
private ArrayList fruit = new ArrayList();
doesn't work. I understand that would only work INSIDE a class.
Maybe I just answered my own question.
Please confirm, why can't I call it private?
Learneroo
Jun 15, 7:28 AMThat's correct. When you're inside a method, every variable you declare can only be accessed inside the method, so you don't use the
private
modifier.