More About ArrayList
More Methods
Let's look at some more common methods in ArrayList. The example code below will (again) come after the following code:
ArrayList<String> fruits = new ArrayList<String>();
fruits.add("apple");
fruits.add("banana");
fruits.add("cantaloupe");
indexOf(Object o)
Sometimes you will want to search through your list to find a specific Object. With an array, you would need to write multiple lines of code that loop through the array looking for the item. With an ArrayList, you can just use the provided method indexOf
. It will search through the list and return the index of the first occurrence of the Object. If the Object isn't there. it will return -1
instead.
This is the official documentation:
indexOf(Object o)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
For example:
int c = fruits.indexOf("cantaloupe");
This will set c
to 2
, the index of cantaloupe
.
remove(index)
What if you want to remove an item from the List? You use the remove()
method, which has this description:
remove(int index)
Removes the element at the specified position in this list.
If you click on the method name, you'll be brought to the longer description, which adds:
Shifts any subsequent elements to the left (subtracts one from their indices).
So remove()
will remove the item from the specified index and then shift all of the items over to fill the gap. ArrayList always makes sure to keep all of the items in a list together with no gaps! This way the 7th item in a list will always be located at the 7th index location.
fruits.remove(1);
This will result in an ArrayList with just 2 items:
{"apple", "cantaloupe"}
remove(Object o)
Below the first remove method, you'll see another one with the same name but a different parameter:
remove(Object o)
Removes the first occurrence of the specified element from this list, if it is present.
This method searches through the List for the given Object and will remove the first occurrence that it finds. It will return true
if it finds the Object and false
otherwise (though you can ignore what it returns).
fruits.remove("cantaloupe");
This will result in a single-item ArrayList:
{"apple"}
ArrayList vs Array
When should you use an ArrayList, and when should you use an array? The simple array is useful in certain situations when you need a fixed-size list or fast performance (or if you're learning about algorithms). However, in most cases you'll probably want the ArrayList. The ArrayList provides many more features, such as automatic resizing and shifting, and a bunch of useful methods.
This table shows how to do things with Array and ArrayList:
Array | ArrayList | |
---|---|---|
creation | String[] array = new int[10]; |
ArrayList<String> list = new ArrayList<String>(); |
add item | array[0] = "hello"; Need to specify index. Cannot auto-resize |
list.add("hello"); Adds to end of list and resizes as necessary. (Can also insert into list with set.) |
get item | String w = array[0]; |
String x = list.get(0); |
get size | int l = array.length; returns the length of the array, regardless of how many items are in it. |
int s = list.size(); returns the number of items in the list. |
search | Need to loop through array. | indexOf("hello"); returns the index of "hello". |
Iterating through ArrayList
You can go through an ArrayList the same way you go through an Array - with a for-each loop. For example:
for(String fruit: fruits){
System.out.println(fruit);
}
If fruits
had the original 3 Strings from the top of the page, this will print out:
apple banana cantaloupe
If you need more control, you can use the basic for-loop. However, to access each element, you'll need to use the get()
method instead of the weird array syntax. For example:
for(int i=0; i<fruits.size(); i++){
System.out.println( fruits.get(i) );
}
This will print the same output as the previous code. Notice it stopped when i<fruits.size()
was no longer true. In an Array, such a loop could have continued printing until i
reached fruits.length
, which could have included many empty (or null) cells.
Task
- Create a method
deleteNote
which takes in an integer parameter and removes the note located at that index in the Notebook. - Create a method
search
in Notebook which takes in one String parameter and returns the index of the first Note that contains that String. If no Note has the String, it should return-1
.
You can use your Note'sfind
method, or create a new method in Note that checks if the Note contains a specific character sequence or String. - Create a method
printSummaries
which prints out a summary of all Notes in the Notebook, with each summary on its own line. (You can use Note'ssummarize
method.) - (Advanced) Create a method
returnSummaries
, which returns a String of all Note summaries. The String should number each summary with an index, add a period and space ". " between the number and summary, and add the newline symbol "\n" after each summary. For example, given a Notebook with these two notes:
"Hello world!"
"To be or not to be that is the question."
returnSummaries
should return the following String:
"0. Hello world!\n1. To be or not to be t\n"
when printed, the String would look like this:
0. Hello world! 1. To be or not to be t
Challenge
What number will the following code print out?
ArrayList<String> fruits = new ArrayList<String>();
fruits.add("apple");
fruits.add("banana");
fruits.add("cantaloupe");
fruits.remove("apple");
int h = fruits.indexOf("honeydew");
int c = fruits.indexOf("cantaloupe");
System.out.println(h+c);
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
BentheMuffin
Nov 16, 5:16 PMCan I have some help? For some reason I cannot add notes to notebook.
import java.util.ArrayList;
public class Notebook
{
private ArrayList notes;
}
Learneroo
Nov 17, 12:08 AM@BentheMuffin, you'll want to set a type when you create the ArrayList. See ArrayList Type.