java--Array

Source: Internet
Author: User

An array is a combination of multiple arrays of the same data type, where the data type is no matter what the data type.

Array variables are reference-type variables, arrays can be objects, and each element in an array is equivalent to a member variable of an object, so array elements can be initialized by default. (Blog java--Variable Classification There is a description of the initialization of member variables).

one-dimensional arrays
declaration

type var[]; or type[] var< /span>

initialize

int a[];a=new int[3];a[0]=0;a[1]=1;a[2]=2; 

Static initialization: Defines the same time that the array element allocates space and assigns a value

int a[]={1,2,3}; 
memory analysis
Class Testarray{public static void Main (String arg[]) {int[] s;s=new int[5];for (int i=0;i<5;i++) {s[i]=i;}}}

The above program runs to the last. The memory should be this:

two-dimensional arrays

two-dimensional arrays can be considered as elements of an array The array.

initialize

Dynamic initialization (note: java the Declaration and creation of multidimensional arrays in the order from high-to low-dimensional )

int a[][]=new int[3][];a[0]=new int[2];a[1]=new int[4];a[2]=new int
  [3]; 

int a[][]={{1,2},{2,3},{3,4}}; 
Memory analysis:
Class Testarr{public static void Main (String arg[]) {int s[][]=new int[3][];s[0]=new int[2];s[1]=new int[4];s[2]=new int[ 3];for (int i=0;i<a.length;i++) {for (int j=0;j<s[i].length;j++) {s[i][j]=i;}}}}

The above program runs to the end, the memory should be this


Summary

The element of a one-dimensional array in 1.java is the same variable as the data type, while a two-dimensional array is an array of multiple one-dimensional array groups, so it is important to understand one dimension well. 2. An array is a reference variable that can be used as an object. And its element is the member variable.

java--Array

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.