Mylane
Mylane

Reputation: 55

How to use the index of an array in another array?

I have a problem with my code, as far as I know, I could use the index of an array for another array of the same size, but different type. But this one only let's me work with the first's one type.

The objective is : If in an array there is an occupied slot, it's index will move to another array, which will mark with an X the occupied place.(The first one uses int, and the second, char.)

Thank you for your help.

public static char consolaej(int[][] ejcedula, char[][] matrixej){
  for (int i=0; i<ejcedula.length; i++){
    for(int j=0; j<4; j++){
        if(ejcedula[i][j]!=0){
            matrixej[i][j]=x;
        }
    }

 }
 return matrixej;   
}

Upvotes: 2

Views: 554

Answers (1)

ninja.coder
ninja.coder

Reputation: 9648

You should replace matrixej[i][j]=x; with matrixej[i][j]='x';. Also, return type of function should be char[][] and not just char.

Here is the code snippet:

public static void main (String[] args)
{
    int[][] ejcedula = {{1,0,1,2},{0,0,1,2},{2,3,4,0}};
    char[][] matrixej = new char[ejcedula.length][4];
    matrixej = consolaej(ejcedula,matrixej);

    /* Print Matrixej */
    for (int i = 0; i < ejcedula.length; i++) {
        for(int j = 0; j < 4; j++) {
            System.out.print(" " + matrixej[i][j]);
        }
        System.out.println();
    }
}

public static char[][] consolaej(int[][] ejcedula, char[][] matrixej) {
    for (int i = 0; i < ejcedula.length; i++) {
        for(int j = 0; j < 4; j++) {
            if(ejcedula[i][j] != 0) {
                matrixej[i][j] = 'x';
            }
        }
    }
    return matrixej;   
}

Output:

 x  x x
   x x
 x x x 

Upvotes: 3

Related Questions