The difference between Java collections and containers and the method of mutual conversion

Both collections and containers are containers in Java.

the difference

Array features: fixed size, can only store data of the same data type

Collection features: the size can be dynamically expanded, and various types of data can be stored

 

 

convert

Convert an array to a 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]);
}

Convert a collection to an array:

collection.toArray();

Example:

List list = new ArrayList();
list.add("a");
list.add("b");
 
list.toArray();
System.out.println(list.toString());

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325293697&siteId=291194637