Convert two dimensional array into one dimensional array in java

Posted By: Java Examples - 10:14 PM

Share

& Comment

What is 2D array(matrix)?

Two-dimensional arrays are defined as "an array of arrays". Since an array type is a first-class Java type, we can have an array of ints, an array of Strings, or an array of Objects. For example, an array of ints will have the type int[]. Similarly we can have int[][], which represents an "array of arrays of  ints". Such an array is said to be a two-dimensional array.



The command
  int [][] A = new int [3][4];

declares a variable, A, of type int[][], and it initializes that variable to refer to a newly created object. That object is an array of arrays of ints. Here, the notation int[3][4] indicates that there are 3 arrays of ints in the array A, and that there are 4 ints in each of those arrays.

To process a two-dimensional array, we use nested for loops. We already know about for loop. A loop in a loop is called a Nested loop. That means we can run another loop in a loop. 

int[][] A = new int[5][4];
 // print array in rectangular form
 for (int i=0; i<A.length; i++) {
     for (int j=0; j<A[i].length; j++) {
         System.out.print(" " + A[i][j]);
     }
     System.out.println("");
 }

In this example, "int[][] A= new int[5][4];" notation shows a two-dimensional array. It declares a variable A of type int[][],and it initializes that variable to refer to a newly created object. The notation int[5][4] indicates that there are 10 arrays of ints in the array A, and that there are 5 ints in each of those arrays. 


Java version of this program is given bellow :
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package matrix;

import java.util.Scanner;

/**
 *
 * @author ACHCHUTHAN
 */
public class Matrix2Array {

    int r, c;
    int array[];
    int matrix[][];

    public Matrix2Array() {
        Scanner input = new Scanner(System.in);
        System.out.println("This program is Matrix(2D) "
                + "elements will convert to (1D)array");
        System.out.println("Enter the value of row =");
        r = input.nextInt();
        System.out.println("Enter the value of coloum =");
        c = input.nextInt();
        array = new int[r * c];
        matrix = new int[r][c];
    }

    public void put(int i, int j, int e) {
        array[(i) * c + j] = e;
    }

    public int get(int i, int j) {
        return array[(i) * c + j];
    }

    public void StoreMatrix() {
        for (int rr = 0; rr < r; rr++) {
            for (int cc = 0; cc < c; cc++) {
               int ee=(int) Math.round(Math.random()*89+10);
                matrix[rr][cc] =ee ;;
                put(rr, cc, ee);
            }
        }
    }

    public void PrintMatrix() {
        System.out.println("Element are Store in "
                + "Matrix in row " + r + " Coloum " + c);
        for (int rr = 0; rr < r; rr++) {
            for (int cc = 0; cc < c; cc++) {
                System.out.print(matrix[rr][cc] + "\t ");
            }
            System.out.println();
        }
    }

    public void PrintArray() {
        System.out.println("Element converted to array &"
                + " Array length=" + array.length);
        for (int rr = 0; rr < r; rr++) {
            for (int cc = 0; cc < c; cc++) {
                System.out.print(get(rr, cc) + " ");
            }
        }
    }

    public static void main(String args[]) {
        Matrix2Array M = new Matrix2Array();
        M.StoreMatrix();
        M.PrintMatrix();
        M.PrintArray();
    }
}

Output look like this :
run:
This program is Matrix(2D) elements will convert to (1D)array
Enter the value of row =
3
Enter the value of coloum =
4
Element are Store in Matrix in row 3 Coloum 4
59         11         18         80         
59         37         25         40         
84         44         27         17         
Element converted to array & Array length=12
59 11 18 80 59 37 25 40 84 44 27 17 
BUILD SUCCESSFUL (total time: 10 seconds)

About Java Examples

I’m passionate about Web Development and Programming and I go to extreme efforts to meet my passion. I’m a believer of learning the fundamentals first. I try to understand everything little bit more than the average.

Copyright © 2016 Java Examples ACHCHUTHAN.ORG. Designed by Templateism .