3 ArrayLists Flashcards

1
Q

T or F ArrayList can change size at runtime as needed

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Write the three ways to create an ArrayList.

A
ArrayList list1 = new ArrayList();
ArrayList list2 = new ArrayList(10);
ArrayList list3 = new ArrayList(list2);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does generics mean in relation to an ArrayList?

A
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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Talk about the relationship between ArrayList and List.

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Name the 8 methods for ArrayList.

Adrian clearly contains equal issues removing set sizes.

A
a,c.c.e,i,r,s,s
add(),
 clear(),
 contains(),
 equals(),
 isEmpty(),
 remove(),
 set(),
 and size().
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the equals() method do for ArrayList?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How is an ArrayList sorted?

A

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;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the import for List?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the import for ArrayList?

A

java.util.ArrayList;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the only way to directly convert an ArrayList to an array?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the zero do in this statement? String[] stringArray = list.toArray(new String[0]);

A

It creates an array of exactly the size of the ArrayList that is being converted.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Does the diamond operator have to be on the right?

ArrayList list4 = new ArrayList<>();

A
No, it's optional Any of these are OK
ArrayList list4 = new ArrayList();
ArrayList list4 = new ArrayList<>();
ArrayList list4 = new ArrayList();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
Does the diamond operator have to be on the left?
ArrayList list4 = new ArrayList();
A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly