Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

can you help me rewirte this from array implementation to array list implementat

ID: 3887908 • Letter: C

Question

can you help me rewirte this from array implementation to array list implementation.

this is java.

package matrix;

/**
* This class provides a data representation for the AbstractMatrix
* implementation of the Matrix API.
* It uses a two-dimensional array of integers to store matrix elements.
* @author Your name and section here
*/
public class ArrayImplementation extends AbstractMatrix {
  
/**
* Creates an array representation of a matrix of integers.
* Elements of the array are initialized to zero.
* @param numRows the number of rows in the matrix
* @param numColumns the number of columns in the matrix
*/
public ArrayImplementation (int numRows, int numColumns) {
super(numRows, numColumns);
elements = new int[numRows][numColumns];
for (int r = 0; r < numRows; r++) {
for (int c = 0; c < numColumns; c++) {
elements[r][c] = 0;
}
}
}

/**
* Gets the element at the indicated row and column in this matrix.
*
* @param row the row position for the element.
* @param column the column position for the element.
* @return the element at the indicated row and column
* @throws MatrixException if row or column is out of range
*/
@Override
public int get(int row, int column) {
checkBounds(row, column);
return elements[row][column];
}

/**
* Sets the element at the indicated row and column in this matrix.
*
* @param row the row position for the element.
* @param column the column position for the element.
* @param element the value to set in the matrix
* @throws MatrixException if row or column is out of range
*/
@Override
public void set(int row, int column, int element) {
checkBounds(row, column);
elements[row][column] = element;
}

/**
* Adds this matrix to another.
*
* @param other the other matrix to add
* @return a new matrix that is the sum of this matrix and other
* @throws MatrixException if this matrix and the other matrix do not have
* the same dimensions
*/
@Override
public Matrix add(Matrix other) {
Matrix sum = new ArrayImplementation(getNumRows(), getNumColumns());
add(this, other, sum);
return sum;
}

/**
* Multiples this matrix by another.
*
* @param other the other matrix to multiply
* @return a new matrix that is the product of this matrix and other
* @throws MatrixException if the number of columns in this matrix does not
* match the number of rows in the other
*/
@Override
public Matrix multiply(Matrix other) {
Matrix product = new ArrayImplementation(getNumRows(), other.getNumColumns());
multiply(this, other, product);
return product;
}
  
private final int[][] elements;
  
}

Explanation / Answer

Hi,
A 2d array can be converted into array list using nested lists, like given below,i have added comments to help you understand,

package matrix;

/**

* This class provides a data representation for the AbstractMatrix

* implementation of the Matrix API.

* It uses a two-dimensional array of integers to store matrix elements.

* @author Your name and section here

*/

public class ArrayImplementation extends AbstractMatrix {

  

/**

* Creates an array representation of a matrix of integers.

* Elements of the array are initialized to zero.

* @param numRows the number of rows in the matrix

* @param numColumns the number of columns in the matrix

*/

public ArrayImplementation (int numRows, int numColumns) {

super(numRows, numColumns);

  

for (int r = 0; r < numRows; r++) {

List<Integer> columns = new ArrayList<Integer>();

for (int c = 0; c < numColumns; c++) {

columns.set(c)=0;

}

elementsNew.set(r)=columns; //setting individual rows

}

}

/**

* Gets the element at the indicated row and column in this matrix.

*

* @param row the row position for the element.

* @param column the column position for the element.

* @return the element at the indicated row and column

* @throws MatrixException if row or column is out of range

*/

@Override

public int get(int row, int column) {

checkBounds(row, column);

//get the desired row element and then desired column index

return elementsNew.get(row).get(column);

}

/**

* Sets the element at the indicated row and column in this matrix.

*

* @param row the row position for the element.

* @param column the column position for the element.

* @param element the value to set in the matrix

* @throws MatrixException if row or column is out of range

*/

@Override

public void set(int row, int column, int element) {

checkBounds(row, column);

//setting the given row and column element

elementsNew.get(row).set(column) = element;

}

/**

* Adds this matrix to another.

*

* @param other the other matrix to add

* @return a new matrix that is the sum of this matrix and other

* @throws MatrixException if this matrix and the other matrix do not have

* the same dimensions

*/

@Override

public Matrix add(Matrix other) {

Matrix sum = new ArrayImplementation(getNumRows(), getNumColumns());

add(this, other, sum);

return sum;

}

/**

* Multiples this matrix by another.

*

* @param other the other matrix to multiply

* @return a new matrix that is the product of this matrix and other

* @throws MatrixException if the number of columns in this matrix does not

* match the number of rows in the other

*/

@Override

public Matrix multiply(Matrix other) {

Matrix product = new ArrayImplementation(getNumRows(), other.getNumColumns());

multiply(this, other, product);

return product;

}

  

private final List<List<Integer>> elementsNew = new ArrayList<List<Integer>>(); //defining a 2d matrix as list of lists

  

}

Thumbs up if this was helpful, otherwise let me know in comments.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote