简体   繁体   中英

How can I create a getter method and a setter method for a multidimensionnal array?

To begin, sorry for my English, I'm French.

Here's the problem I'm trying to solve by writing this problem in a mathematical notation:

I have couples of coordinates that look like this: (x, y)

An = {(Xn;Yn)}

array[An][(Xn;Yn)] = {{X1;Y1}{X2;Y2}...{Xz;Yz}};

In my program, I need to create a getter and setter for the multidimensionnal array.

This is my code:

    //Infos for animations of objects

    //Sorting
    Random random1 = new Random();
    int obscMin=0, obscMax=4; //I sort them to know how many obstacles will have to be created. obsc is the obstacle
    int nbreObsc = obscMin + random1.nextInt(obscMax - obscMin); //nbreObsc is the number of obstacles
    //End of sorting

    /*Here's the model of a table:
     A couple: An={(Xn;Yn)}
     Tableau1[An][(Xn;Yn)]={{X1;Y1}{X2;Y2}...{Xz;Yz}};*/

    float posObsc [] []=new float [nbreObsc] [2]; //New table, which will contain the positions of the obstacles

    //Obstacle position getter and setter
    public float[][] getPosObsc(){//getters
        return posObsc;
    }
    public void setPosObsc(float[][] posObsc){//setters
        this.posObsc=posObsc;
    }
    //End of obstacle position getter and setter

    protected boolean detruireObsc=false; //"detruireObsc" means "destroyObstacle"

    //Algorithm that defines the movement of obstacles
    protected void obscDeplacemt(){
        for(int i=1;i<=nbreObsc;i++){
        //Sorting to determine the Xs
        float ordMin=0,ordMax=width;
        float ordObsc = ordMin + (float)Math.random() * (ordMax - ordMin); //ordObsc means obstacleXPosition
        setPosObsc( posObsc [i][0]);
        //End of sorting
        }
    }
  //End of obstacle movement algorithm

Here's the error I get from eclipse:

The method setPosObsc(float[][]) in the type Activity01Jeux.RenderViewJoueur is not applicable for the arguments (float)

The line of code:

setPosObsc( posObsc [i][0]);

Is calling the setPosObsc() method with a single float element from the array of arrays. But the method takes an array of arrays.

To make the code compile, you could write:

setPosObsc(posObsc);

Though that may not be what you want! If you are trying to write a method that puts a float into the array at a particular point, you will need something like this:

void setObstacleAt(int obstacleIndex, int boundaryIndex, float shiftDistance) {
    posObsc[obstacleIndex][boundaryIndex] = shiftDistance;
}

I'm making a wild guess at what your array contains.

As a side note, rather than writing comments to explain the method names, you might consider using longer or more precise method names without abbreviations. In Java there is no practical limit to the length of variable and method names.

By the way, well done for having the courage to write to StackOverflow in English when it's not your first language. I had no trouble understanding your question even before Runemoro's edits.

Your getter and setter methods are fine. The error is because in the last line of code in obscDeplacemt(), you are calling setPosObsc(posObscp[i][0]). posObscp[i][0] will give you a float, when your setter method needs an array of float in the parameter in order for it to work. Good luck!

Looks like what you're trying to do is:

posObsc[i][0] = ordObsc;

There's no need to use a setter if you're in the same class.

If you want to be able to externally set an element value by index (instead of overwriting the whole array), there are two ways to go about it:

1) (Not recommended)

Since you're exposing the whole array in your getter, you can theoretically call

getPostObsc()[i][0] = ordObsc;

2) (Recommended)

Change your getter and setter to

public float getPosObsc(int x, int y){
    return posObsc[x][y];
}
public void setPosObsc(int x, int y, float val){
    this.posObsc[x][y] = val;
}

Now you can update an element using the setter with an index:

setPostObsc(i, 0, ordObsc);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM