About Arrays in Java

Source: Internet
Author: User

Preface: Recently just finished reading the "Java programming thought" in the chapter on the array, all the knowledge about the Java array, should be considered to understand almost. Here again to comb again, in order to encounter vague knowledge, convenient to consult.

There are about three ways to hold objects in Java: by using the object's field to hold an object (by way of a combination), through the array, through the collection container . Let's take a look at this. By using arrays to hold objects with the collection container, it is generally clear what type of object I want to hold, which means that the array is used to hold the same type of object. Before the jdk1.5, this is a big advantage of the array relative to the collection container, since it is possible to throw any type of object into the collection container before it is introduced, and when an object is thrown into the container, the type of the object is at run time, it is difficult to know that to use this object again, it is necessary to make an unsafe downward transformation (thrown into the collection The object in the jdk1.5 is converted up to object, and after the introduction of generics, the collection container can hold only objects of the same type, so this advantage of the array is lost after jdk1.5. The array has a disadvantage relative to the collection container, that is, after an array object is created, the size of the array is fixed and the length of the array cannot be extended in subsequent operations on the array. For a collection container, when you create a collection container object, you do not need to describe the size of the collection container and how many objects you want to put in the collection. At this point, everything seems to imply that the collection is more convenient than the array, and that the collection can be done. This is actually the case, the author of the Java programming idea also suggests that you can use collections without arrays. But the array as a lightweight data type for holding objects is still common in code.

In fact, to completely understand the array, as long as the understanding of the array as a data type, in the content of how to store. This allows you to create arrays, initialize them, or assign values to array elements ... are well understood.

(Picture one)

1. Creation of Array objects:

There are two ways to declare an array variable: int a[] and int[] A , the latter is recommended because it is clear that this is an array with an int value.

Array as a reference type of data type, array variable holds the reference address of the array object in the heap memory.

An array of one-dimensional primitive types:

About Arrays in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.