Java record -31-java array

Source: Internet
Author: User
Tags array definition

1. array of arrays : The collection of data of the same type is called an array.

2. How to define an array:type[] variable name = number of elements in the new type[array ];

an integer array oflength ten can be defined as follows:int[] A = new int[10];

3. The index of the element in the array is starting from 0 , for the array, the largest array index = = array of length -1.

4. Array Definition and assignment method:

Int[] A = new int[2]; A[0] = 1; A[1]=2;int a[] = new INT[2]; As defined above is equivalent to int[] a = {n}; The initial value is assigned at the time of definition. Int[] A = new int[]{1,2}; Assign initial value int[] a = new int[2]{1,2} at the time of definition; This definition is wrong, compile exception://cannot define dimension expressions when an array initializer is provided

5. the array loop assigns the initial value:

Int[] A = new int[100];for (int i=0; i<a.length; i++) {a[i] = i+1;}

Each of the arrays in Java has a property named length , which represents the size of the array.

A. length = $;// Compile error, cannot specify value for final variable length .

The length property is public,final,int . Once the array length is determined, the size cannot be changed.

6.

Int[] A = new int[3]; System.out.println (a[2]);

When you define an array, the default is to assign an initial value to each element, and the integer type assigns 0.

7. The type of each element in the array is the same, occupying the same size of the address space, the bottom is placed in order next to each other, looking for the time in order to find.

8.

Int[] A = {1,2,3};int[] b = {n/a}; System.out.println (A.equals (b));


The array is also inherited from the object class, and there is no equals method to override object . Compares the contents of two arrays for the same, and cannot use the equals method.

9. int[] A = {n/a};

A is an array variable reference that points to the first address of the array, which is the address of the first element. Each element type in the array is of type int , where only the data value itself is stored.

The array can contain not only the native data type, but also the reference type.

public class referencearray {     Public static void main (String[] args)  {        &NBSP;PERSON[]&NBSP;P&NBSP;=&NBSP;NEW&NBSP;PERSON[2];&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;P[0]  = new person (;        p[1] = new ) person (+);         for (int i=0; i<p.length; i++) {             system.out.println (p[i].age);         }    }}class Person{     public int age;    public person (int age) {         this.age = age;    }} 

The person is a custom class, you can also build an array, and the default initial value is null.

person[] p = new person[10];  

object? The answer is no. The code simply generates a person null Span style= "font-family: ' Courier New ';" >person object, each element in the array is loaded with a reference instead of an object, yes person int 0

storing elements in an array:

Each element in an array of native data types holds the data value itself;

Each element in an array of reference types is loaded with the object's reference address, not the object itself.

Two-dimensional array: a two-dimensional array is a planar structure, essentially an array of arrays. How two-dimensional arrays are defined:type[][] aa = new type[2][3]; a two-dimensional array can be an irregular array.

14.

public static void Main (string[] args) {int[][] aa = new INT[2][3];        System.out.println (Aa[0] instanceof int[]);        Aa[0][0] = 1;        int[][] bb = new int[2][];    int[][] cc = new int[][]{{1,2,3},{2}}; }

A two-dimensional array traversal typically uses a two-layer nested loop, whereas a one-dimensional array typically uses a single layer of loop traversal.


Java record -31-java array

Related Article

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.