Java Multidimensional Array Traversal detailed

Source: Internet
Author: User

An array is a container object in Java that has multiple values of a single type. The array length is determined when the array is created. After it has been created, its length is fixed. The following is an array of length 10:

public class Arraydemo {private int arraysize=10;public int[] arrayofintegers = new Int[arraysize];}

The above code is an example of a one-dimensional array. In other words, the array length can only grow in one direction. Many times we need arrays to grow on multiple dimensions. This array we call multidimensional arrays. For simplicity, we call it a 2-dimensional array. A two-dimensional array is useful when we need a matrix or x-y coordinate system. Here is an example of a two-dimensional array:

public class Theproblemof2darray {private static final int arr_size=10;public static void main (string[] args) {int arr[][] =new int[arr_size][arr_size];}}

Imagine a two-dimensional array that looks like a matrix of an X-y coordinate system.

However, it may be surprising to Java developers that Java does not actually have a two-dimensional array.

In a real array, all the elements are stored in memory in contiguous chunks of memory, but not in a two-dimensional array in Java. The elements in all one-dimensional arrays in Java occupy adjacent memory locations and are therefore a true array.

In Java, when we define:

int singleelement//denotes an int variable
Int[] Singledarray//denotes an array of int variables (one-dimensional)
Int[][] Twodarray//Represents an array of array of int variables (two-dimensional)

This means that, in the example above, a two-dimensional array is a reference to an array, and each of its elements is a reference to another int array.

This picture clearly explains the concept.

Because the two-dimensional arrays are scattered in memory, there are some effects on performance. To analyze this discrepancy, I wrote a simple Java program that shows the importance of the traversal order.

Package arraytraverse;/** * Problems with two-dimensional arrays * * We are initializing a 2-dimensional array of any size. (To simplify the analysis we use two-dimensional square matrices) we will iterate over the same array in a couple of different ways, analyzing the results * The performance gap between the two iterations is large * @author Mohit * */public class Theproblemof2darray {//array size: the more    The greater the performance gap, the more obvious the private static final int arr_size=9999;        public static void Main (string[] args) {//new array int arr[][]=new int[arr_size][arr_size];        Long Currtime=system.currenttimemillis ();         Colmajor (arr);        SYSTEM.OUT.PRINTLN ("Total time in Colmajor:" + (System.currenttimemillis ()-currtime) + "MS");        New array, exactly the same as ARR int arr1[][]=new int[arr_size][arr_size];        Currtime=system.currenttimemillis (); Rowmajor (ARR1); This was the only difference in above System.out.println ("Total time in col:" + (System.currenttimemillis ()-CURRT    IME) + "MS"); }/** * The following code iterates through the array by column FIRST * * * * * * * * * * * */private static void Colmajor (int arr[][]) {for (int i=0;i<arr_size;i++) {for (int j=0;j<arr_size;j++) {//See this, we is traversing J first and then I arr[i][j]=i+j;     }}}/** * If we convert the inside and outside loops * program to iterate through the array in line precedence * that is, scan the bank before scanning the next line * This means that we access the array each time we access different columns (and therefore also access different pages) * Minor changes to the code will cause this program to take more time to complete traversal */private static void Rowmajor (int arr[][]) {for (int i=0;i<arr_size;i++             {for (int j=0;j<arr_size;j++) {/* see this, we'll go through J First, then traverse I, but for the access element * They're farther away, so it takes more            */arr[j][i]=i+j; }        }    }}

The following is the result of the example:

Java Multidimensional Array Traversal detailed

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.