This article mainly introduces the difference between Java collections and arrays. It has a very good reference value. Let’s take a look at it with the editor.
Collections and containers are both containers in Java.
Difference
Array features: fixed size, can only store data of the same data type
Collection features: the size can be dynamically expanded, and can store various Types of data
Conversion
Convert 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 array:
Collection.toArray();Example:List list = new ArrayList(); list.add("a"); list.add("b"); list.toArray(); System.out.println(list.toString());
The above is the detailed content of Introduction to the differences between Java collections and arrays. For more information, please follow other related articles on the PHP Chinese website!