Home  >  Article  >  Java  >  Introduction to the differences between Java collections and arrays

Introduction to the differences between Java collections and arrays

黄舟
黄舟Original
2017-03-09 10:59:321641browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn