矩阵输出问题java

时间:2012-03-19 13:56:31

标签: java

继承我的代码,显示我完全没有错误并且它有效但我的东西有一点输出问题

    // Lab13TEXT03st.java
// This is the student starting version of the Lab13 assignment.
// Testing <main> methods are provided for the 80-point and 100-point versions.
// This means that this version will not compile as provided.


import java.util.ArrayList;


public class Lab13TEXT03st
{
    public static void main(String[] args) 
    {
        System.out.println("\nLAB26A 80-POINT VERSION\n"); 

        Matrix m2 = new Matrix(3, 5, 100); 
        //m2.displayMatrix("Matrix m2 3 X 5 Display"); 
        System.out.println(); 
        int count = 100; 

        System.out.println("Matrix m1 Default Display"); // 
        System.out.println("Matrix has no elements"); // 
        System.out.println("");

        int pos = 0;

        for (int r = 0; r < m2.getRows(); r++) 
        { 
        //System.out.println("r = " + r); // 

        for (int c = 0; c < m2.getCols(); c++) 
        { 
            m2.setValue(r,c,count, pos);
            pos++;
            count++; 
            //System.out.println("r = " + r + " c = " + c + " and count = " + count); // 
        } 
            //System.out.println(""); // 
        } 



        m2.displayMatrix("Matrix m2 3 x 5 Consecutive Integers Display"); 
        System.out.println(); 



        Matrix m3 = new Matrix(3,3,100);                
        m3.displayMatrix("Matrix m3 3 X 3 Initialized to 100 Display");
        System.out.println();
    }


}




    class Matrix {

    private ArrayList list; // one-dimensional array stores matrix values 
    private int listSize; // total number of elements in the matrix 
    private int numRows; // number of rows in the matrix 
    private int numCols; // number of cols in the matrix 

    public Matrix(int r, int c, int value){ 
        list = new ArrayList(); 
        numRows = r; 
        numCols = c; 
        listSize = r * c; 

    //for(int i = 0; i < listSize; i++) 
    // list.add(new Integer(value)); 
    } 

    public int getRows(){ 
        return numRows; 
    } 

    public int getCols(){ 
        return numCols; 
    } 

    public int getSize()
    { 
        return listSize; 
    } 

    public int getValue(int r, int c)
    { 
        int rowLength = getRows(); 
        int colLength = getCols();
        //System.out.println("r in get = " + r);
        int position = (r * colLength) + c;
        //int position = ((r + 1) * rowLength) - rowLength + c; // one to multi-dimensional array index conversion
        //System.out.print("Pos get = " + position + "; ");
        return ((Integer)list.get(position)).intValue(); 
    } 

    public void setValue(int r, int c, int value, int pos)
    { 
        int rowLength = getRows(); 
        //int colLength = getCols(); 
        //int position = ((r + 1) * rowLength) - rowLength + c; // one to multi-dimensional array index conversion 
        //System.out.println("Pos set = " + position + "; ");
        list.add(pos,new Integer(value)); 
    } 

    public void displayMatrix(String str)
    { 

    System.out.println(str); 

    for(int j = 0; j < getRows(); j++){ 
    for(int i = 0; i < getCols(); i++){ 
    System.out.print(getValue(j, i) + " "); 
    } 
        System.out.println(""); 
    } 
}
}

我的输出错了,我需要一些帮助

我没有错误......这是我的输出

    LAB26A 80-POINT VERSION


Matrix m1 Default Display
Matrix has no elements

Matrix m2 3 x 5 Consecutive Integers Display
100 101 102 103 104 
105 106 107 108 109 
110 111 112 113 114 

Matrix m3 3 X 3 Initialized to 100 Display
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.RangeCheck(ArrayList.java:547)
    at java.util.ArrayList.get(ArrayList.java:322)
    at Matrix.getValue(Lab13TEXT03st.java:128)
    at Matrix.displayMatrix(Lab13TEXT03st.java:147)
    at Lab13TEXT03st.main(Lab13TEXT03st.java:49)

Process completed.

它需要像这样输出:

    LAB13TEXT03 80-POINT VERSION

Matrix m1 Default Display
Matrix has no elements

Matrix m2 3 X 5 Display
0  0  0  0  0
0  0  0  0  0
0  0  0  0  0

Matrix m2 3 X 5 Consecutive Integers Display
100  101  102  103  104
105  106  107  108  109
110  111  112  113  114

Matrix m3 3 X 3 Initialized to 100 Display
100  100  100
100  100  100
100  100  100

请帮忙

1 个答案:

答案 0 :(得分:2)

第二个矩阵(m3)失败,因为您尚未初始化它,就像使用m2一样。你从来没有在代码中的m3.setValue(...)行写一些东西。或者,要使用默认值初始化矩阵,您需要在Matrix的构造函数中取消注释这些行:

for(int i = 0; i < listSize; i++)
    list.add(new Integer(value)); 
相关问题