Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print the following Multi-Dimensional array in Java Script?

Tags:

javascript

I have the following array (the code is written in Java) :

String[][] a = new String[3][2];
a[0][0] = "1"; 
a[0][1] = "2"; 

a[1][0] = "1";  
a[1][1] = "2"; 

a[2][0] = "1";
a[2][1] = "2"; 

and what I want to do is to print 111222 and I accomplished that in Java by doing this:

for (int i=0;i < a[i].length;i++){
    for(int j=0;j <a.length;j++){
        System.out.print(a[j][i]);
    }
}

What is the equivalent of this in JavaScript?

like image 318
Jimmy Avatar asked Oct 04 '11 18:10

Jimmy


People also ask

How to create multi-dimensional arrays in JavaScript?

Javascript has no multi-dimensional arrays syntax inbuilt. But you can create an array of arrays. where each array element is an array of values. In this array, Each element is a single element and elements are of strings or numbers, or objects. We can create one dimensional array with array literal syntax

How to print 2D array in Java?

Top 3 Methods to Print 2D Array in Java. Methods for printing 2d arrays in java are explained below: Method #1 – Using for Loop. For loop is used for iteration and hence we will use for loop to iterate elements of an array in java. Code: public class PrintUsingForLoop {public static void main(String[] args) {final int[][] ar = {{ 5, 9 }, { 2, 4 }};

How to remove an element from a multidimensional array in JavaScript?

Remove an element start, middle, and ending of a multidimensional array Javascript has no multi-dimensional arrays syntax inbuilt. But you can create an array of arrays. where each array element is an array of values. In this array, Each element is a single element and elements are of strings or numbers, or objects.

How to print array in matrix format in Java?

System.out.println (); //using this for new line to print array in matrix format. When a number of rows and columns are not equal in a multidimensional array in java, we call it a Jagged array in Java. Here the number of columns differs from that of its rows.


4 Answers

var arr =[
        [1,2,3],
        [4,5,6],
        [7,8,9]
        ],arrText='';

        for (var i = 0; i < arr.length; i++) {
            for (var j = 0; j < arr[i].length; j++) {
                arrText+=arr[i][j]+' ';
            }
            console.log(arrText);
            arrText='';
        }

Output: enter image description here

like image 194
Ram Pukar Avatar answered Nov 15 '22 14:11

Ram Pukar


Here is the equivalent code in Javascript (no space its not a script version of java)

! edit missed the particulars of the loops, fixed now

var a = [];
a.push(["1", "2"]);
a.push(["1", "2"]);
a.push(["1", "2"]);

for(var i = 0; i < a[i].length; i++) {
  for(var z = 0; z < a.length; z++) {
    console.log(a[z][i]);
  }
}
like image 25
megakorre Avatar answered Nov 15 '22 15:11

megakorre


  • In javascript you can create multi dimensional array using single dimensional arrays.
  • For every element in an array assign another array to make it multi dimensional.

    // first array equivalent to rows 
    let a = new Array(3);
    
    // inner array equivalent to columns
    for(i=0; i<a.length; i++) {
      a[i] = new Array(2);
    }
    
    // now assign values
    a[0][0] = "1"; 
    a[0][1] = "2"; 

    a[1][0] = "1";  
    a[1][1] = "2"; 

    a[2][0] = "1";
    a[2][1] = "2";
    
    /* console.log appends new line at end. So concatenate before     printing */
    
    let out="";
    for(let i=0; i<a.length; i++) {
      for(let j=0; j<a[i].length; j++) {
        out = out + a[i][j];
      }
    }
    console.log(out);
like image 35
nadeem Avatar answered Nov 15 '22 16:11

nadeem


for (i=0; i < a.length; i++) {
   for (j = 0; j < a[i].length; j++) { document.write(a[i][j]); }
}

Though it would be smarter to add all the strings together and the print them out as one (could add to an element or alert it out.)

like image 34
Madara's Ghost Avatar answered Nov 15 '22 16:11

Madara's Ghost