ArrayList Methods and Docs
You've already seen ArrayList's add()
method, but ArrayList comes with many other useful methods. You can read about it on the Learneroo ArrayList reference, but to get all the info you should lookup ArrayList on the Official Java 7 API Documentation. The documentation may seem confusing, but it's important to have some idea as to how to use it.
Search
The first step to finding out more about a Library Class is to search for it. If you know the name of the Class, you can use Google, ctrl-F in your browser, or an extension to lookup the class on the official site.
Its useful to be quickly look up the documentation for a method while coding. To do this in BlueJ, press ctrl-space after starting to type the method name. You can also press it after typing the "dot" to pull up a list of all methods in that class. This will let you view documentation and select methods without having to type it all out manually. You can even use this shortcut for classes you create!
Here's an example of using ctrl-space to lookup String documentation:

Interface
As mentioned, you can use the Java Library without knowing how the Classes are implemented internally. All you need to know is its external interface:
- A general description of what the class does
- A list of its constructors and methods
- The parameters taken in and the return type of each method and constructor
- The purpose of each method and constructor
All of this information is provided in the official documentation.
ArrayList
To get started, open the page on ArrayList. Towards the top of ArrayList (after a short introduction to ArrayList and its Field and Constructor Summaries) you'll see the Method Summary, which is a list of all methods in the Class. Each row shows the name of the method, the parameters it takes, and a short description of what it does. You can click on the method name to get a longer technical description of the method.
size()
For example, let's say we wanted to know how many items were in an ArrayList(). If you scroll through the different methods, you'll see the method size()
:
int | size() Returns the number of elements in this list. |
This looks like the method you want! The return type on the left is an int
since it will return an integer with the size of the list.
ArrayList<String> fruits = new ArrayList<String>();
fruits.add("apple");
fruits.add("banana");
fruits.add("cantaloupe");
System.out.println( fruits.size() );
This will print out 3
, the number of items in fruits
.
get(index)
You can add items to ArrayList with add()
, but an ArrayList is only useful if you can get items out of it. ArrayList comes with the appropriately named get()
method for getting an item from a specific index. It takes in an integer, and returns the Object located at that position in the list. (Note that ArrayLists start at index 0, just like Arrays.)
String fruit = fruits.get(1);
System.out.println(fruit);
This will print out banana
, which is located in the 1th position of fruits
.
If you look at the documentation for get()
, you'll see this description:
get(int index)
Returns the element at the specified position in this list.
Task
- Create a method
getNote
in the Notebook Class that takes in an integer parameter and returns the Note located at that index in the Notebook (inside its ArrayList). - Create a method
numNotes
which returns the number of Notes in the Notebook. - Create a method
getLastNote
which returns the last Note in the Notebook. (Remember that the index of an ArrayList starts with 0).
Challenge
It can be confusing to remember the different names Java uses. ArrayList uses the method size()
for the number of items in it, while String uses the method length()
for its number of characters, and arrays use a final field called length
for the number of items it can hold.
You can review this in the code below. Which line of code will cause a compile error?
int[] nums = new int[5]; // Line 0
ArrayList<String> words = new ArrayList<String>(); //1
String word = "hello"; //2
int l = word.length; //3
int s = words.size(); //4
int n = nums.length; //5
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
BentheMuffin
Nov 15, 9:10 AMCan someone help me this code wont compile and I have no idea why.
public String getNote(int p){
String n = notes.get(p);
return notes.get(p);
}
Danail Dichev
Jan 24, 6:04 PM// getNote
public Note getNote(int pos){
Note gottenNote = notes.get(pos);
return gottenNote;
}
Bolke
Oct 8, 11:33 AMSo - there's no kind of feedback on the Tasks, right? Nor on whether they've been completed at all? I never do anything unless I have to, alas. That's why I come to this website to learn Java, instead of reading a book.
Learneroo
Oct 8, 2:17 PMAs mentioned in The String Class, you can download a provided BlueJ project and run test cases on your own computer after completing the code. This way you can get the benefit of coding in on your own computer and still get the feedback after completing your code.
Bolke
Oct 9, 4:43 AMI know. That's a very good thing. It's just, I'm the sort of person who never does that. I only complete something if, otherwise, you can't go on to the next page.
Sad but true!
Thanks for your answer.
Learneroo
Jun 16, 7:18 AMjava.lang.IndexOutOfBoundsException: Index: 0, Size: 0
means that it tried to access the first element in the ArrayList but the ArrayList was empty.