3 ArrayLists Flashcards
T or F ArrayList can change size at runtime as needed
True
Write the three ways to create an ArrayList.
ArrayList list1 = new ArrayList(); ArrayList list2 = new ArrayList(10); ArrayList list3 = new ArrayList(list2);
Do the size of an ArrayList have to be given when the ArrayList is declared?
No. This is legal. ArrayList list1 = new ArrayList();
What does generics mean in relation to an ArrayList?
allow you to specify the type of class that the ArrayList will contain. ArrayList list4 = new ArrayList(); ArrayList list5 = new ArrayList<>();
Java allows you to tell the compiler what the type would be by specifying it between <
and >. Starting in Java 7, you can even omit that type from the right side.
ArrayList list5 = new ArrayList<>();
The < and > are
still required, though. This is called the diamond operator because <> looks like a diamond.
Talk about the relationship between ArrayList and List.
ArrayList implements an interface called List. In other
words, an ArrayList is a List. You will learn about interfaces in Chapter 5. In the meantime,
just know that you can store an ArrayList in a List reference variable but not vice
versa. The reason is that List is an interface and interfaces can’t be instantiated.
List list6 = new ArrayList<>();
ArrayList list7 = new List<>(); // DOES NOT COMPILE
Name the 8 methods for ArrayList.
Adrian clearly contains equal issues removing set sizes.
a,c.c.e,i,r,s,s add(), clear(), contains(), equals(), isEmpty(), remove(), set(), and size().
What does the equals() method do for ArrayList?
It looks inside at the elements. ArrayList has a custom implementation of equals() so you can compare two lists to see if they contain the same elements in the same order.
How is an ArrayList sorted?
Sorting an ArrayList is very similar to sorting an array. You just use a different helper class:
ArrayList numbers = new ArrayList();
numbers. add(99);
numbers. add(5);
numbers. add(81);
Collections.sort(numbers); // pass the list reference as parameter
System.out.println(numbers); [5, 81, 99]
- requires import java.util.Collections;
What is the import for List?
import java.util.List; It is NOT awt.list
What is the import for ArrayList?
java.util.ArrayList;
What is the only way to directly convert an ArrayList to an array?
It has to be converted to an array of type Object.
Object[] objectArray = list.toArray();
If you want to convert it to a specific type of array then you have to pass in a new array in the parameter.
String[] stringArray = list.toArray(new String[0]);
What does the zero do in this statement? String[] stringArray = list.toArray(new String[0]);
It creates an array of exactly the size of the ArrayList that is being converted.
Does the diamond operator have to be on the right?
ArrayList list4 = new ArrayList<>();
No, it's optional Any of these are OK ArrayList list4 = new ArrayList(); ArrayList list4 = new ArrayList<>(); ArrayList list4 = new ArrayList();
Does the diamond operator have to be on the left? ArrayList list4 = new ArrayList();
No, it is not required. It creates restriction on the type allowed in the array list. If it's there, it has to have a type inside. ArrayList<> list4 = new ArrayList(); // will not compile ArrayList list4 = new ArrayList();// ok