Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,104 questions

40,777 answers

573 users

How to use 1D array as 2D array in Java

1 Answer

0 votes
package javaapplication1;

public class JavaApplication1 {

    public static void main(String[] args) {

        int row = 3;
        int col = 4;

        int[][] arr2d = new int[row][col];

        arr2d[0][0] = 100;
        arr2d[1][1] = 11;
        arr2d[2][2] = 22;
        arr2d[2][3] = 8888;

        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                System.out.print(arr2d[i][j] + "   ");    
            }
            System.out.println();
        }
        System.out.println();

        int[] arr1d = new int[row * col];

        arr1d[0 * col + 0] = 100;
        arr1d[1 * col + 1] = 11;
        arr1d[2 * col + 2] = 22;
        arr1d[2 * col + 3] = 8888;

        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                System.out.print(arr1d[i * col + j] + "   ");
            }
            System.out.println();
        }
    }
}


/*

run:
                   
100   0   0   0   
0   11   0   0   
0   0   22   8888   

100   0   0   0   
0   11   0   0   
0   0   22   8888  

*/

 





answered Jan 8, 2017 by avibootz
edited Jan 10, 2017 by avibootz

Related questions

1 answer 62 views
62 views asked Jan 8, 2017 by avibootz
1 answer 26 views
1 answer 25 views
...