2014-06-08 61 views
1

首先,我的英语很抱歉,我是法语。如何为多维数组创建一个getter方法和一个setter方法?

以下是我正在试图通过一个数学符号写这个问题要解决的问题:

我有坐标的夫妻看起来像这样:(x, y)

An = {(Xn;Yn)}

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

在我的程序中,我需要为多维数组创建一个getter和setter。

这是我的代码:

//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 

这是我从日食出现错误:

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

+1

你不介意我翻译的意见成英文? Est-ce-queçavousdérangeraitsi je traduirais vos commentaires du code en Anglais? – Runemoro

+0

请提供可编辑的代码。 –

+0

@ Runemoro // Absolumt pas c'est avec grd plaisir!Traduit traduit et traduit encore:p –

回答

0

的代码行:

setPosObsc(posObsc [i][0]); 

是调用setPosObsc()方法与一个单一的float eleme nt从数组中排列。但是该方法需要一个数组数组。

为了使代码编译,你可以写:

setPosObsc(posObsc); 

虽然这可能不是你想要的!如果你正在尝试写在一个特定的点把一个浮动到阵列的方法,你需要这样的事情:

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

我在做什么你的数组包含胡乱猜测。作为一个方面说明,不要写评论来解释方法名称,你可以考虑使用更长或更精确的方法名称而不用缩写。在Java中,变量和方法名称的长度没有实际限制。

顺便说一下,当英语不是您的第一语言时,我们有勇气写下英文的StackOverflow。即使在Runemoro的编辑之前,我也没有理解你的问题。

+0

Big thk!这是我需要的确切答案!涂ge gars! –

+0

@Pif_Paf_Pouf:将节目添加到节目中,然后轻松调整节奏,按Ctrl + Shift + F,即可调整节目播放时间。 – Runemoro

+0

@ Runemoro // Sympa je savais pas.Merci a toi。 –

0

你的getter和setter方法很好。错误是因为在obscDeplacemt()的最后一行代码中,您正在调用setPosObsc(posObscp [i] [0])。 posObscp [i] [0]会给你一个浮点数,当你的setter方法在参数中需要一个float数组才能使其工作。祝你好运!

+0

Thk you.Can you give me a code exemple?请。 –

0

貌似你试图做的是:

posObsc[i][0] = ordObsc; 

有没有必要,如果你在同一类是使用setter方法。

如果你希望能够在外部设置(而不是覆盖整个阵列)的索引的元素值,有两种方法去做:

1)(不推荐)

既然你暴露在你的getter整个阵列,理论上可以调用

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

2)(推荐)

更改您的getter和setter到

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; 
} 

现在,您可以使用更新的setter与索引的元素:

setPostObsc(i, 0, ordObsc); 
+0

thk你,它工作正常,但它不是我的问题。 ;) –

+0

thk这是我需要的确切答案! T'es le meuilleur merci a toi :) –

相关问题