Java Array Learning Notes

Source: Internet
Author: User

Simple understanding of arrays

A simple understanding of Java arrays is the ability to create and assemble them, access their elements by using integer index values, and their dimensions cannot be changed, and they are arrays.

The particularity of an array

There are many ways to hold objects in Java, so where are the different points of the array?

Arrays differ from other kinds of containers in three ways: efficiency, type, and ability to preserve basic types .

In Java, an array is the most efficient way to store and randomly access an object's reference sequence. An array is a simple linear sequence, which allows the element to be accessed very quickly. But the price paid for it is that the size of the array object is fixed and immutable throughout its life cycle.

Before generics, other container classes treat objects as if they were not of any concrete type. That is, they treat these objects as the root class object of all classes in Java. arrays are better than containers before generics, because we can create an array to hold a specific type.

Arrays can hold basic types, whereas containers before generics cannot. but with generics, containers can specify and examine the types of objects they hold, and with automatic wrapping, the container looks like it can hold the base type.

Below is a sample code that introduces the comparison of arrays to generic containers:

: arrays/containercomparison.javaimport java.util.*;import static  net.mindview.util.print.*;class berylliumsphere {  private static long  Counter;  private final long id = counter++;  public string  tostring ()  { return  "sphere "  + id; }}public class  Containercomparison {  public static void main (String[] args)  {     BerylliumSphere[] spheres = new BerylliumSphere[10];     for (int i = 0; i < 5; i++)        Spheres[i] = new berylliumsphere ();     print (Arrays.toString (spheres));     print (Spheres[4]);     list<berylliumsphere> spherelist  =      new arrayList<berylliumsphere> ();     for (int i = 0; i < 5;  i++)       spherelist.add (New berylliumsphere ());     print (spherelist);     print (Spherelist.get (4));    int[]  Integers = { 0, 1, 2, 3, 4, 5 };    print ( arrays.tostring (integers));     print (Integers[4]);     list<integer > intList = new ArrayList<Integer> (       Arrays.aslist (0, 1, 2, 3, 4, 5));     intlist.add (;  )   print (intlist);     print (Intlist.get (4));   }} /* output:[ Sphere 0, sphere 1, sphere 2, sphere 3, sphere 4, null,  null, null, null,&nbsP;null]sphere 4[sphere 5, sphere 6, sphere 7, sphere 8, sphere  9]sphere 9[0, 1, 2, 3, 4, 5]4[0, 1, 2, 3, 4, 5,  97]4*///:~


Java Array Learning Notes

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.