NEWS: Total Picture Views: 151536 | Total Article Views: 161020 | Top 5 Most Popular Article: 1. Am I Stuck Algorithm with Becker Robot | 2. PickUpThings with Becker Robot | 3. How to read a JSON and work with it | 4. Encrypted Data GUI (Download .Zip) | 5. Create Wall and Things with Becker

How to create and use 2D Arrays Java

Description:

In this example we will show how to use 2 dimensional arrays and print the result in print. This is the result:

0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6 6 6
7 7 7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9

 

Code:

import java.util.Arrays;

/**
 *
 * @author Jean-Mathieu
 */
public class 2DArrays {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int arraySize = 10;
            int inc = 0;
             int[][] array = new int[arraySize][arraySize];
             for (int[]x : array) {
                 Arrays.fill(x, inc++);
             }
             
             for(int i = 0; i < arraySize; i++){
               for(int j = 0; j < arraySize; j++){
                  System.out.printf("%5d ", array[i][j]);
               }
               System.out.println();
            }
    }
    
}
Attachments: None

Tags: 2d arrays Arrays Array Jagged

Total Views: 812

My name is Jean-Mathieu

I created this website so other people could enjoy finding useful stuff easier. If you have any question do not hesitate to contact me.

jean8mathieuCreated on 11/22/14 and updated on 01/24/15


affiliate_link

Disclosure: We are a website that needs compensation to operate like any other website on the internet.
We may receive consideration for our reviews but we are totally unbiased and do not accept paid reviews or fake reviews claiming to be something they are not.