unit 7 Flashcards

1
Q

What is an ArrayList in Java?

A

A resizable array-like structure from java.util package.

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

How do you import ArrayList?

A

import java.util.ArrayList;

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

How do you declare an ArrayList of Strings?

A

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

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

How do you add an element to an ArrayList?

A

list.add(“item”);

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

How do you access an element in an ArrayList?

A

list.get(index)

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

How do you change an element in an ArrayList?

A

list.set(index

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

How do you remove an element from an ArrayList?

A

list.remove(index)

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

How do you find the size of an ArrayList?

A

list.size()

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

What happens if you access an invalid index in an ArrayList?

A

IndexOutOfBoundsException

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

What type of data can ArrayLists store?

A

Objects (not primitives directly)

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

What wrapper class is used for int in ArrayLists?

A

Integer

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

What wrapper class is used for double in ArrayLists?

A

Double

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

Can ArrayLists store primitive types?

A

No

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

What is auto-boxing?

A

Automatic conversion between primitives and wrapper classes.

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

What is unboxing?

A

Automatic conversion from wrapper classes back to primitives.

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

How do you traverse an ArrayList using a for loop?

A

for (int i = 0; i < list.size(); i++) { list.get(i); }

17
Q

How do you traverse an ArrayList using an enhanced for loop?

A

for (String s : list) { System.out.println(s); }

18
Q

Can you modify elements while using enhanced for loop?

19
Q

How do you find if an ArrayList contains an item?

A

list.contains(item)

20
Q

How do you find the index of an item in an ArrayList?

A

list.indexOf(item)

21
Q

What does list.indexOf(item) return if item is not found?

22
Q

How do you remove an item by value?

A

list.remove(new Integer(value)) or list.remove(“string”)

23
Q

What happens when you remove an item from an ArrayList?

A

Elements shift left and size decreases.

24
Q

How can you copy one ArrayList into another?

A

Use a loop or the constructor: new ArrayList<>(oldList);

25
What is the difference between Array and ArrayList?
Arrays have fixed size; ArrayLists resize dynamically.