Java Array (1): Arrays and multidimensional arrays

Source: Internet
Author: User

Our basic view of arrays is that you can create them by using integer index values to access their elements, and their dimensions cannot be changed.

But sometimes we need to evaluate whether we are using arrays or more flexible tools. The array is a simple linear sequence, which makes the element access very fast. but the price paid for this speed is that the array size is fixed and immutable throughout its life cycle.

An array identifier is simply a reference, regardless of which type of array is used. the primitive type array holds the values of the base type directly, and the object array holds a reference to other objects.

1 classApple {2     Private Static intCount = 1;3 4      PublicApple () {5System.out.println ("Apple" + count++);6     }7 }8 9  Public classarrayoptions {Ten      Public Static voidMain (string[] args) { OneApple[] A;//Create an array reference of an Apple type AApple[] B =NewAPPLE[5];//Create an Apple-type array object, but all point to null -System.out.println ("B:" + arrays.tostring (b));//b: [null , NULL, NULL, NULL, NULL] -Apple[] C =NewApple[4]; the          for(inti = 0; i < c.length; i++) -C[i] =NewApple ();//Apple1 Apple2 Apple3 Apple4 -         //Aggregate Initialization: -Apple[] D = {NewApple (),NewApple (),NewApple ()};//Apple5 Apple6 Apple7 +         //Dynamic Aggregate initialization: -A =Newapple[]{NewApple (),NewApple ()};//Apple8 Apple9 +System.out.println ("a.length =" + a.length);//a.length = 2 ASystem.out.println ("b.length =" + b.length);//b.length = 5 atSystem.out.println ("c.length =" + c.length);//c.length = 4 -System.out.println ("d.length =" + d.length);//d.length = 3 -A =D; -System.out.println ("a.length =" + a.length);//a.length = 3 -  -         int[] e;//Create an array reference of a primitive type in         int[] f =New int[5];//Create an Apple base array object with an initialization value of 0 -System.out.println ("F:" + arrays.tostring (f));//f: [0, 0, 0, 0, 0] to         int[] G =New int[4]; +          for(inti = 0; i < g.length; i++) -G[i] = i *i; the         int[] h = {11, 47, 93}; *System.out.println ("f.length =" + f.length);//f.length = 5 $System.out.println ("g.length =" + g.length);//g.length = 4Panax NotoginsengSystem.out.println ("h.length =" + h.length);//h.length = 3 -E =h; theSystem.out.println ("e.length =" + e.length);//e.length = 3 +E =New int[]{1, 2}; ASystem.out.println ("e.length =" + e.length);//e.length = 2 the     } +}

Multidimensional arrays

1 Importjava.util.Arrays;2 3 classApple {4     Private Static intCount = 1;5 6      PublicString toString () {7         return"Apple" + count++;8     }9 }Ten  One  Public classMutiarray { A      Public Static voidMain (string[] args) { -         //(1) Use arrays.deeptostring () to print multidimensional arrays -         int[] A = {{1, 2, 3}, {4, 5, 6}}; the         Long[][][] B =New Long[2] [2] [3]; -System.out.println (Arrays.deeptostring (a));//[ [1, 2, 3], [4, 5, 6] -System.out.println (arrays.deeptostring (b));//[[[ 0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0] ] -  +         //(2) Multidimensional array values can be set in the following way, and each vector in the array that makes up the matrix can have any length (rough array) -         int[] C =New int[3][]; +          for(inti = 0; i < c.length; i++) { AC[i] =New int[i + 1]; at              for(intj = 0; J < C[i].length; J + +) { -C[I][J] = j + 1; -             } -         } -System.out.println (Arrays.deeptostring (c));//[1], [1, 2], [1, 2, 3] -  in         //(3) Arrays of non-basic types (wrapper classes and object classes) can also be handled in a similar manner -Apple[][] D = {{NewApple (),NewApple ()}, {NewApple (),NewApple (),NewApple ()}}; toInteger[][] e = {{1, 2}, {3, 4, 5}, {6, 7}}; +System.out.println (arrays.deeptostring (d));//[ [Apple1, Apple2], [Apple3, Apple4, Apple5] -System.out.println (Arrays.deeptostring (e));//[ [1, 2], [3, 4, 5], [6, 7]] the     } *}

Java Array (1): Arrays and multidimensional arrays

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.