Basic skills for Java Programmers-array

Source: Internet
Author: User

Basic skills for Java Programmers-array and memory control


A Java array is a set of values of the same type and a reference type in Java. To use an array in Java, the array must be initialized. Once the array is initialized, its length cannot be changed, and each value in the array is assigned an initial value.

Java array initialization includes static initialization and dynamic initialization.

Static initialization: the initial values of elements in the specified array displayed by the programmer. The length of the array is determined by the system.

    int[] arr = new int[]{0,1,2};

Dynamic initialization: the length of the specified array displayed by the programmer. The system assigns the initial value to each element in the array.

            int[] arr = new int[3];

The Java array is a static array and its length cannot be changed. Unlike the Javascript dynamic language, its array length can be dynamically changed.

<script type="text/javascript">var arr = [];arr[6] = 10;alert(arr.length);</script>

Is initialization required before using Java arrays?

// Static initialization int [] arr1 = new int [] {0, 1, 2}; // initialization of int [] arr2; arr2 = arr1; For (INT pre: arr2) {system. out. println ("pre:" + pre );}

We can see that arr2 is not initialized, but can still be used. Java array is of reference type
Variable, which does not represent the array object itself, as long as it points to a valid array object, pointing to a valid memory, the array variable can be used in the program.

Each element in the Java array is a variable, and Java does not have a multi-dimensional array. A multi-dimensional array indicates that each element in the first one dimension of the array is a reference variable and references an array of the same type.

int[][] arr =new int[3][];arr[0] = new int[]{0,1};arr[1] = new int[]{0,2};arr[2] = new int[]{0,3};System.out.println(arr.length);System.out.println("----------------");for (int i = 0; i < arr.length; i++) {for (int j = 0; j < arr[i].length; j++) {System.out.println(arr[i][j]);}System.out.println("----------------");}

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.