ArrayLists Flashcards

1
Q

What is a Class?

A

A blueprint of an Object that defines the data and behavior of an Object

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

Give an example of a Class and Object

A

A blueprint of a house is a Class. The actual house is the Object.

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

What is the Collection Class?

A

The framework that allows us to store an manipulate groups of objects.

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

What is an ArrayList?

A

A Collection Class Object that allows us to use Objects in an array.

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

Explain the difference between an Array and an ArrayList.

A

Arrays are:

  • Fixed length
  • Hold one type of data
  • Are accessed by index number
  • Have a predefined number of elements
  • May contain primitives or Objects

ArrayLists are:

  • Variable in length
  • Hold more than one type of data
  • Use methods to access the data
  • Do not need to have predefined length/size
  • Can only contain Objects. No primitives
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the Object Class?

A

The class that holds all other Java classes

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

An ArrayList is a type of:

A

List

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

A List is a type of:

A

Object

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

Two ways to define an ArrayList:

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

In what order are the elements of an ArrayList stored?

A

In the order that they were added

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

What is an import statement?

A

Imports the predefined package and class that will be used for a type of Object

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

What needs to be imported to use an ArrayList?

A

java.util.ArrayList;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
What does this code do?
ArrayList  names = new ArrayList();
A

Creates a new empty ArrayList of Strings`

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

How do you add an element to an ArrayList?

A

array-list-name.add(Object);

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

Why can’t you add an int, double, boolean, or float to an ArrayList?

A

ArrayLists cannot hold primitive types.

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

How do you return the number of elements in an ArrayList?

A

array-list-name.size();

17
Q

How do you retrieve an element at a specified index?

A

array-list-name.get(index);

18
Q

How do you get all the elements of an ArrayList?

A

Set up a for-loop to iterate through the elements using .get();

19
Q

Do ArrayLists allow duplicate elements?

A

Yes

20
Q

How do you add elements to the middle of an ArrayList?

A

array-list-name.add(index, Object);

21
Q

If you add an element at a specified index, what happens to the other elements?

A

They get pushed down

22
Q

How do you remove elements from an ArrayList?

A

array-list-name.remove(index);

23
Q

How do you find out if an element is already in an ArrayList?

A

array-list-name.contains(Object);

24
Q

How do you get the index of the first occurrence of an Object in an ArrayList?

A

array-list-name.indexOf(Object);

25
Q

How do you get the index of the last occurrence of an Object in an ArrayList?

A

array-list-name.lastIndexOf(Object);

26
Q

How do you turn an ArrayList into an Array?

A

Datatype [ ] arrayName = array-list-name.toArray(new datatype[0]);

27
Q

How do you sort an ArrayList?

A

Collections.sort(ArrayListName);

28
Q

How do you reverse the order of an ArrayList?

A

Collections.reverse(ArrayListName);

29
Q

Describe a for-each loop.

A
  • A shorthand for-loop that will iterate through the entire Array/ArrayList.
  • Will go from beginning to end and cannot skip elements
30
Q

How do you define a for-each loop?

A

for(datatype elem-name : array/ArrayList-name)