Home > Java > javaTutorial > body text

What is the difference between arrays and collections in java?

青灯夜游
Release: 2019-12-26 14:10:42
Original
3829 people have browsed it

Java is an object-oriented programming language. It not only absorbs the various advantages of the C language, but also abandons the difficult-to-understand concepts such as multiple inheritance and pointers in C. Therefore, the Java language has two characteristics: powerful functions and simple and easy to use. .

What is the difference between arrays and collections in java?

Array

Array is a built-in data type in the Java language. It is a linear sequence that can quickly access other Elements, arrays are different from other languages.

When you create an array, its capacity is unchanged and cannot be changed during the life cycle. JAVA arrays will also perform boundary checks. If an out-of-bounds phenomenon is found, a RuntimeException will be reported. Error, of course checking bounds comes at the cost of efficiency.

Collection

JAVA also provides other collections, list, map, and set. When they handle objects, it is as if these objects do not have their own types, but directly return to the roots. For Object, you only need to create a collection, put the object in it, and convert it to your own type when taking it out.

Collections and arrays are containers in Java. So what is the difference between a set and an array? Let me introduce it to you below.

The difference between arrays and collections in java

1. Arrays can store basic data types and objects, while collections can only store objects (which can be stored in the form of wrapper classes basic data types).

2. The length of the array is fixed, but the length of the collection can be changed.

3. When defining an array, you must specify the array element type. By default, all elements in the collection are Object

4. The number of elements actually stored in the array cannot be obtained directly. length is used to obtain the number of elements in the array. length, but you can directly obtain the number of elements actually stored in the collection through size()

5. Collections have multiple implementation methods and different applicable occasions, unlike arrays that only allocate continuous space

6. Collections exist in the form of interfaces and classes, with the characteristics of encapsulation, inheritance and polymorphism. Various complex operations can be realized through simple method and attribute calls, greatly improving the efficiency of software development.

Recommended learning: Java video tutorial

The above is the detailed content of What is the difference between arrays and collections in java?. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!