Switching rows on 2d-array generated at random

时间:2016-02-03 03:35:36

标签: java arrays

This is a 2d array which is generated at random, and stores 20 integers from 100-1000. I got that part right, I'm trying to switch the rows of this 2d array and not having much luck, i tried the temp but for some reason it comes out as column array...

public static void main (String args[]){
    int [][] randArray = new int[2][10];
    Random rnd = new Random();
    for(int i=0; i<randArray.length; i++)
        for(int j=0; j<randArray[i].length; j++)
            randArray[i][j] = 100 + rnd.nextInt(900);

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

    System.out.println("Switch row ");
    int temp = 0;
        for (int j=0; j < randArray[0].length; j++){
            temp = randArray[0][j];
            randArray[0][j] = randArray[1][j];
            randArray[1][j] = temp;
        //System.out.println(randArray[i][j]+ "  ");
        }
    }
 }

Switch the rows as in row1 [1,2,3,4,5] row2 [6,7,8,9,10] to row1[6,7,8,9,10] row2[1,2,3,4,5]

2 个答案:

答案 0 :(得分:0)

You can use Arrays.deepToString(Object[]) to print the array. Display the array after you perform your swap. Something like,

int[][] randArray = new int[2][10];
Random rnd = new Random();
for (int i = 0; i < randArray.length; i++) {
    for (int j = 0; j < randArray[i].length; j++) {
        randArray[i][j] = 100 + rnd.nextInt(900);
    }
}
System.out.println(Arrays.deepToString(randArray));
System.out.println("Switch row ");
for (int j = 0; j < randArray[0].length; j++) {
    int temp = randArray[0][j];
    randArray[0][j] = randArray[1][j];
    randArray[1][j] = temp;
}
System.out.println(Arrays.deepToString(randArray));

答案 1 :(得分:0)

你的代码实际上我只修复了编译错误;

public static void main(String args[]) {
        int[][] randArray = new int[2][10];
        Random rnd = new Random();
        for (int i = 0; i < randArray.length; i++)
            for (int j = 0; j < randArray[i].length; j++)
                randArray[i][j] = 100 + rnd.nextInt(900);

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

        System.out.println("Switch row ");
        int temp = 0;
        for (int j = 0; j < randArray[0].length; j++) {
            temp = randArray[0][j];
            randArray[0][j] = randArray[1][j];
            randArray[1][j] = temp;
            //System.out.println(randArray[i][j]+ "  "); no i variable
        }
         //I added only below lines to print your result
        for (int i = 0; i < randArray.length; i++) {
            System.out.println("Row " + i);
            for (int j = 0; j < randArray[i].length; j++)
                System.out.print(randArray[i][j] + " ");
            System.out.println();
        }
}