UTILIZING THE METHODS OF THE ARRAYLIST CLASS
This lab focuses on the use of the methods in the ArrayList class. The methods in the ArrayList class which could be used are:
- add(E o) - Appends the specified element to the end of this list.
- add(int index, E element) - Inserts the specified element at the specified position in this list.
- remove(Object obj) - Removes a single instance of the specified
element from this collection, if it is present.
For example:
ArrayList<String> myList = new ArrayList<String>();
myList.add ("yes");
myList.add ("no");
myList.add ("maybe");
System.out.println(myList);
The output would be:
[yes, no, maybe]
If we then have:
myList.add ("blue");
myList.add ("red");
myList.remove ("no");
System.out.println(myList);
The output would be:
[yes, maybe, blue, red]
Note: There are many other methods in the ArrayList class than
what is listed here. You will learn about them and use them in labs to come.