3 converting b/w array and List Flashcards

1
Q

How can an ArrayList be converted to an array?

A

Convert the ArrayList to an array and assign it to array variable.
Call the toArray method on the ArrayList and assign it to the array. Example:
String [] myArray = myArrayList.toArray();

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

How can an array be converted to a List?

A

orignal array and array backed list are linked. It’s a fixed sized list known as a “backed List”

String[] array = { “hawk”, “robin” }; // [hawk, robin]
List list = Arrays.asList(array); // returns fixed size list
System.out.println(list.size()); // 2
list.set(1, “test”); // [hawk, test]
array[0] = “new”; // [new, test]
for (String b : array) System.out.print(b + “ “); // new test
list.remove(1); // throws UnsupportedOperation Exception

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