Java 矩阵输出问题

Java 矩阵输出问题,java,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

这是我的代码,显示我没有任何错误,它可以工作,但我的东西有一点输出问题

    // 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(""); 
    } 
}
}
但它需要这样输出:

    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
请帮助

第二个矩阵(
m3
)失败,因为您尚未初始化它,就像您对
m2
所做的那样。您从未在代码中按照
m3.setValue(…)
的思路编写过内容。或者,要使用默认值初始化矩阵,您需要在
矩阵的构造函数中取消注释这些行:

for(int i = 0; i < listSize; i++)
    list.add(new Integer(value)); 
for(int i=0;i
Why
public void setValue(int r,int c,int value,int pos)
使用第四个参数
pos
?应按照
getValue
中的逻辑计算位置。如
getValue
中所述:
int position=(r*colLength)+c;列表。设置(位置、值)。另外,您不需要创建
新的整数(值)显式。在web上搜索“java自动装箱”。
for(int i = 0; i < listSize; i++)
    list.add(new Integer(value));