Arrays
Arrays are the odd duck out in the world of collections. They are basically a List
but aren't List
s.1
You can make a List
which is a view over an array with Arrays.asList
.
import java.util.List;
import java.util.Arrays;
class Main {
void main() {
String[] furniture = new String[] {
"Ottoman",
"Table",
"Dresser"
};
List<String> furnitureList = Arrays.asList(furniture);
System.out.println(furnitureList);
}
}
Changes made to the List
returned from Arrays.asList
will be reflected in the underlying array.
import java.util.List;
import java.util.Arrays;
class Main {
void main() {
String[] furniture = new String[] {
"Ottoman",
"Table",
"Dresser"
};
List<String> furnitureList = Arrays.asList(furniture);
furnitureList.set(0, "Recliner");
System.out.println(Arrays.toString(furniture));
}
}
Accordingly, any methods on List
which try to perform operations that an array cannot support (such as .add
) will throw exceptions.
import java.util.List;
import java.util.Arrays;
class Main {
void main() {
String[] furniture = new String[] {
"Ottoman",
"Table",
"Dresser"
};
List<String> furnitureList = Arrays.asList(furniture);
// Cannot add to an array!
furnitureList.add("Shelf");
}
}
1
Arrays are unique beasts. This is true both in Java the language and in the virtual machine Java code runs on.
This is partially attributable to arrays coming first in the history - List
and friends were not in the first version of Java.