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

Introduction to the differences between Java collections and arrays

黄舟
Release: 2017-03-09 10:59:32
Original
1702 people have browsed it

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]);
}  
Copy after login

##Collection converted to array:

Collection.toArray();

Example:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template