The difference between Java collections and arrays
- 2020-06-03 06:31:48
- OfStack
Both collections and containers are containers in Java.
The difference between
Array features: fixed size, can only store data of the same data type
Collection features: The size can be dynamically expanded, can store various types of data
conversion
Array to collection:
Arrays. asList (array)
Example:
int[] arr = {1,3,4,6,6};
Arrays.asList(arr);
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
Collection converted to an array:
Collection. toArray ();
Example:
List list = new ArrayList();
list.add("a");
list.add("b");
list.toArray();
System.out.println(list.toString());