Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.9k views
in Technique[技术] by (71.8m points)

java - How do I getValues and display a Dynamic ArrayList Matrix?

public class DynamicMatrixTest {

/**
 * @param args
 */
public static void main(String[] args) {
    System.out.println("Dynamic Matrix Lab
");

    DynamicMatrix m1 = new DynamicMatrix();
    m1.displayMatrix("Matrix m1 Default Display");
    System.out.println();

    DynamicMatrix m2 = new DynamicMatrix(3,5);
    m2.displayMatrix("Matrix m2 3 X 5 Display");
    System.out.println();
    int count = 100;
    for (int r = 0; r < m2.getRows(); r++)
    {
        for (int c = 0; c < m2.getCols(); c++)
        {
            m2.setValue(r,c,count);
            count++;
        }
    }
    m2.displayMatrix("Matrix m2 3 X 5 Consecutive Integers Display");
    System.out.println();

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

}

class DynamicMatrix{
    private ArrayList<ArrayList<Integer>> list;
    private int numRows; 
    private int numCols;
    
/**
 * Constructs and empty ArrayList object and sets all values to 0
 */
public DynamicMatrix(){
    numRows = 0;
    numCols = 0;
    list = new ArrayList<ArrayList<Integer>>();
}
/**
 * Constructs rows x cols matrix with all elements initialized to 0
 * @param rows the number of rows in the list
 * @param cols the number of cols in the list
 */
public DynamicMatrix(int rows, int cols){
    numRows = rows;
    numCols = cols;
    list = new ArrayList<ArrayList<Integer>>();
    
    for(int row = 0; row < numRows; rows++) {
        ArrayList myRow = new ArrayList<Integer>();
        for(int col = 0; col < numCols; col++) {
            myRow.add(0);
        }
        list.add(myRow);
    }
}
/**
 * Constructs rows x cols matrix with all elements initialized to value
 * as an integer object
 * @param rows the number of rows in the list
 * @param cols the number of columns in the list
 * @param value the value of the integer objects in the list
 */
public DynamicMatrix(int rows, int cols, int value){
    numRows = rows;
    numCols = cols;
    list = new ArrayList<ArrayList<Integer>>();
    
    for(int row = 0; row < numRows; rows++) {
        ArrayList myRow = new ArrayList<Integer>();
        for(int col = 0; col < numCols; col++) {
            myRow.add(value);
        }
        list.add(myRow);
    }
}
/**
 * returns the number of rows
 * @return numRows attribute
 */
public int getRows(){   
    return numRows;
}

/**
 * returns the number of columns
 * @return numCols attribute
 */
public int getCols(){   
    return numCols;
}

/**
 * returns the value from list at the r, c position
 * @param r row index of value to return 
 * @param c column index of value to return
 * @return primative int value of integer object at r,c
 */
public int getValue(int r, int c){
    
}

/**
 * changes the value in list at r,c to an integer object with 
 * with the passed value
 * @param r row index of the value to change
 * @param c column index of the value to change
 * @param value the value of the integer object to change too
 */
public void setValue(int r, int c, int value){
    
}

/**
 * Displays the matrix in the appropriate format
 * @param s title string for the display
 */
public void displayMatrix(String s){
    System.out.println(s); //Keep this as first line of code
    
}

here is all my java code so far, I am unsure how to write getValue, setValue, & DisplayMatrix. I also would like to get feedback if I typed the getRows & getCols correctly.I started off with just a simple return statement here but I can not test if they are used correctly because I am unsure on how to code the rest of the program in order to run & test it out. Much help would be appreciated, cheers! side note: the expected results would be:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The implementation of getValue and setValue is pretty straightforward, given that you do not allow setting values that are out-of-range:

public int getValue(int r, int c){
    return this.list.get(r).get(c);
}
public void setValue(int r, int c, int value){
    this.list.get(r).set(c, value);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...