Home > Net >  Nested for loop to assign the elements of the parameter to the corresponding elements of the field
Nested for loop to assign the elements of the parameter to the corresponding elements of the field

Time:12-03

I'm very new to writing java code and could really use some help! I am doing a project where I'm stuck on how to write one of the methods...The setMatrixVals(mVals) setter method must make a copy of the parameter and the copy becomes a private field. I've been told to set up nested for loop and assign the elements of the mVals parameter to the corresponding elements of the matrixVals field (which is a 2D int array). And I'm very stuck on how to write it...

Here is some of my code before this method if that helps at all:

`

//make public class
public class Matrix
{
   //field for two dim arrays
   private int [][] matrixVals;

   //constructor
   public Matrix (int mVals[][])
   {
      //call the setMatrixVals( ) method to assign the correct information to the 
private field
      setMatrixVals(mVals);
   }

   //setter method
   public void setMatrixVals (int [][] mVals)
   {
      matrixVals = new int[mVals.length][mVals[0].length];
  
      //set up nested for loop and assign the elements of the mVals parameter to the 
     //coressonding elements of the matrixVals feild

   }

`

Thank you in advance!

CodePudding user response:

Here is your setMatrixVals method. Its pretty simple

public void setMatrixVals(int[][] mVals)
{
  int rows = mVals.length;
  int cols = mVals[0].length;
  matrixVals = new int[rows][cols];
  
  for(int i=0;i<rows;i  ){
    for(int j=0;j<cols;j  ){
      matrixVals[i][j] = mVals[i][j];
    }
  }

}

CodePudding user response:

Since array is Object in java you could use clone() method:

public void setMatrixVals (int [][] mVals)
{
    matrixVals = new int[mVals.length][];
    for (int i = 0; i < mVals.length; i  ) {
        matrixVals[i] = mVals[i].clone();
    }
}

CodePudding user response:

Here is a sample application, based on your code, that will hopefully help you to get acquainted with multidimensional arrays and for loops

public class Matrix {

    protected int[][] matrixVals;

    public Matrix (int matrixVals[][]) {
        setMatrixVals(matrixVals);
    }

    public void setMatrixVals (int[][] matrixVals) {
        this.matrixVals = new int[matrixVals.length][matrixVals[0].length];
        for (int a = 0; a < matrixVals.length; a  ) {
            for (int b = 0; b < matrixVals[0].length; b  ) {
                this.matrixVals[a][b] = matrixVals[a][b];
            }
        }
    }

    public int[][] getMatrixVals() {
        return matrixVals;
    }

    public static void main(String[] args) {

        int[][] matrixVals = {{1, 2, 3}, {4, 5, 6}};

        Matrix matrix = new Matrix(matrixVals);

        int[][] result = matrix.getMatrixVals();

        for (int a = 0; a < result.length; a  ) {
            for (int b = 0; b < result[0].length; b  ) {
                System.out.println(result[a][b]);
            }
        }

    }

}

Fabio

  •  Tags:  
  • java
  • Related