二维数组及其转置

时间:2015-02-09 05:16:05

标签: java arrays

所以我刚刚完成了我的程序,但问题是每次我输入我希望我的数组的元素,例如,如果我输入一个3X2数组,它打印出一个3X3数组及其转置。现在它工作得很好,除了当它输出正确的输出时,我在输出结束时得到一个outof bounds错误。

import java.util.Random;
import java.util.Scanner;


public class Prog2c {

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int userChoice;
        int userChoice2;



        System.out.println("Program 2c, Christian Villa, masc1854");
        System.out.print("please enter the first dimension of your array: ");
        userChoice = in.nextInt();
        System.out.print("Please enter the second dimension of your array: ");
        userChoice2 = in.nextInt();



        int[][] list = randArray(userChoice,userChoice2);
        printList(list);



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

    }


    /*--------------------------------------------------------------
    Method name: randArray
    Description: Creates a random array of n number of elements that is assigned by the user
    input:    a double array
    Output:        none
    Returns:      a randomly generated array 
    ----------------------------------------------------------------
    */
    public static int[][] randArray(int userChoice,int userChoice2)
    {
        int[][] list = new int[userChoice][userChoice2];
        Random random = new Random();
        for(int i = 0; i < userChoice; i++)
            for(int j = 0; j < userChoice2; j++)
        {
               list[i][j] = random.nextInt((99-10)+1)+10; 
        }
        return list;
     }
    /*--------------------------------------------------------------
    Method name: printList
    Description: Prints the contents of an integer 2D array.
    input:    an integer 2D array
    Output:       display an integer 2D array
    Returns:      none
    ----------------------------------------------------------------
    */
    public static void printList(int[][] list)
    {
        for(int i = 0; i < list.length; i++)
        {
            for(int j = 0; j < list.length; j++)
            {
                System.out.print(list[i][j]);
                System.out.print("  ");
            }
            System.out.println();
        }
        System.out.println();
    }

}

1 个答案:

答案 0 :(得分:1)

在主程序中, 要打印矩阵,可以省略if。第一个索引是行索引,第二个索引是列。使用行数组的长度来打印行。

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

要打印转置,请交换行和列:

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

但计算转置矩阵可能更好:

int[][] transpose( int[][] list ){
    int[][] t = new int[list[0].length][list.length];
    for(int i = 0; i < list.length; i++){
       for(int j = 0; j < list[0].length; j++){
           t[j][i] = list[i][j];
       }
   }
   return t;
}

现在你可以重复使用print:

printMatrix( list );
int[][] listT = transpose( list );
printMatrix( listT );