ArrayLists and Collections Flashcards

1
Q

What are some disadvantages of partially filled arrays?

A
  • you must pass around two references (object and size)
  • the limited size can cause problems
  • Elements must be shifted left or right often
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are some disadvantages of the ArrayList class?

A

they can only hold Objects, not primitive types. To get around this, we use Integer, Double, Boolean, etc.

There is internal complexity and a speed penalty. Usually this is OK, but in computationally intensive tasks, ordinary arrays are preferred

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

What are wrapper classes?

A

Classes that represent primitive types as objects (Integer, Double, Boolean, etc.)

They store references to immutable objects (just like String)

Java will convert freely between primitive types and their wrapper type

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

How can you add things to an ArrayList?

A

a.add(“testing”) adds to the end

when 0 <= c <= a.size()
a.add(c, “testing”) inserts at c
a.set(c, “new”) replaces the item at c
when c < 0 or c > a.size()
a.add(c, “testing”), a.set(c, “new”)
gives an IndexOutOfBoundsException

add returns a boolean result meaning “did it change?”
set returns the old value that was deleted

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

How can you remove things from an ArrayList

A

when 0 <= c < a.size
a.remove(c) removes the element at index 0 and returns the deleted element
when c < 0 or c >= a.size
a.remove(c) gives an IndexOutOfBoundsException

a.remove(“testing”) removes that String, returns a boolean (“was it there?”)

a.clear() makes a empty

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

How do you search for something in ArrayLists?

A

int indexOf(Object)
int lastIndexOf(Object)

both of these methods use .equals() to determine equality

boolean contains(Object)

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

What is an alias?

A

An alias is a reference to the same object as another variable. Any changes to one of them will affect the other

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

How can you do a deep copy of an array?

A

a2 = new int[a1.length];
System.arrayCopy(a1, 0, a2, 0, a1.length);

However, if you need a true deep copy of an array of mutable objects, you should make clones at two different levels. In this case, writing your own loop is better

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

What does “garbage collection” do?

A

Frees up memory by deleting objects that have n places where the reference is stored (orphans)

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

What is a collection class?

A

A class whose primary role is to store a bunch of other objects. Simply using arrays of objects can make your main class very full; delegating the handling of the specifics of your objects to a collection makes this more useful. It makes your code reusable and encapsulation

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