3 ArrayLists Flashcards Preview

Java OCA flash cards > 3 ArrayLists > Flashcards

Flashcards in 3 ArrayLists Deck (14):
1

T or F ArrayList can change size at runtime as needed

True

2

Write the three ways to create an ArrayList.

ArrayList list1 = new ArrayList();
ArrayList list2 = new ArrayList(10);
ArrayList list3 = new ArrayList(list2);

3

Does the size of an ArrayList have to be given when the ArrayList is declared?

No. This is legal. ArrayList list1 = new ArrayList();

4

What does generics mean in relation to an ArrayList?

Generics allows you to specify the type of object that the ArrayList will contain.
ArrayList list4 = new ArrayList();
ArrayList list5 = new ArrayList<>(); // no type specified

List list = new ArrayList<>(); //specifies String

Java allows you to tell the compiler what the type would be by specifying it between . 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.

5

Talk about the relationship between ArrayList and List.

ArrayList implements an interface called List.
An ArrayList is a List.
A List is an interface and interfaces can’t be instantiated.
List list6 = new ArrayList<>();
ArrayList list7 = new List<>(); // DOES NOT COMPILE

6

Name the 8 methods for ArrayList.
Hint:
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().

7

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.

8

How is an ArrayList sorted?

Collections.sort(myArrayList); // pass the list reference as parameter

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;

9

What is the import for List?

import java.util.List; It is NOT awt.list

10

What is the import for ArrayList?

java.util.ArrayList;

11

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]);

12

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.

13

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<>();

14

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 but no type specified
ArrayList list4 = new ArrayList(); // correct way to use generics. Diamond on left with type inside.