当我向其添加数字时,为什么我的索引变量不会增加?

时间:2010-07-13 11:23:55

标签: java arrays variables

我将以下float数组public static float camObjCoord[] = new float[8000];声明为全局变量。然后我通过调用以下方法向其添加内容:

public void addcube(float highx, float lowx, float highz, float lowz){
    //Constructing new cube...
    Global.cubes = Global.cubes + 1;
    float highy = 4.5f;
    float lowy = 2.5f;

    System.out.println("ADDING A CUBE!!");

    //FRONT
    Global.camObjCoord[Global.i] = highx;
    Global.camObjCoord[Global.i+1] = lowy;
    Global.camObjCoord[Global.i+2] = lowz;

    Global.camObjCoord[Global.i+3] = highx;
    Global.camObjCoord[Global.i+4] = lowy;
    Global.camObjCoord[Global.i+5] = lowz;

    Global.camObjCoord[Global.i+6] = highx;
    Global.camObjCoord[Global.i+7] = highy;
    Global.camObjCoord[Global.i+8] = lowz;

    Global.camObjCoord[Global.i+9] = highx;
    Global.camObjCoord[Global.i+10] = highy;
    Global.camObjCoord[Global.i+11] = lowz;

    //BACK
    Global.camObjCoord[Global.i+12] = highx;
    Global.camObjCoord[Global.i+13] = lowy;
    Global.camObjCoord[Global.i+14] = highz;

    Global.camObjCoord[Global.i+15] = highx;
    Global.camObjCoord[Global.i+16] = highy;
    Global.camObjCoord[Global.i+17] = highz;

    Global.camObjCoord[Global.i+18] = highx;
    Global.camObjCoord[Global.i+19] = lowy;
    Global.camObjCoord[Global.i+20] = highz;

    Global.camObjCoord[Global.i+21] = highx;
    Global.camObjCoord[Global.i+22] = highy;
    Global.camObjCoord[Global.i+23] = highz;

    //LEFT
    Global.camObjCoord[Global.i+24] = highx;
    Global.camObjCoord[Global.i+25] = lowy;
    Global.camObjCoord[Global.i+26] = lowz;

    Global.camObjCoord[Global.i+27] = highx;
    Global.camObjCoord[Global.i+28] = highy;
    Global.camObjCoord[Global.i+29] = lowz;

    Global.camObjCoord[Global.i+30] = highx;
    Global.camObjCoord[Global.i+31] = lowy;
    Global.camObjCoord[Global.i+32] = highz;

    Global.camObjCoord[Global.i+33] = highx;
    Global.camObjCoord[Global.i+34] = highy;
    Global.camObjCoord[Global.i+35] = highz;

    //RIGHT
    Global.camObjCoord[Global.i+36] = highx;
    Global.camObjCoord[Global.i+37] = lowy;
    Global.camObjCoord[Global.i+38] = highz;

    Global.camObjCoord[Global.i+39] = highx;
    Global.camObjCoord[Global.i+40] = highy;
    Global.camObjCoord[Global.i+41] = highz;

    Global.camObjCoord[Global.i+42] = highx;
    Global.camObjCoord[Global.i+43] = lowy;
    Global.camObjCoord[Global.i+44] = lowz;

    Global.camObjCoord[Global.i+45] = highx;
    Global.camObjCoord[Global.i+46] = highy;
    Global.camObjCoord[Global.i+47] = lowz;

    //TOP
    Global.camObjCoord[Global.i+48] = highx;
    Global.camObjCoord[Global.i+49] = highy;
    Global.camObjCoord[Global.i+50] = lowz;

    Global.camObjCoord[Global.i+51] = highx;
    Global.camObjCoord[Global.i+52] = highy;
    Global.camObjCoord[Global.i+53] = lowz;

    Global.camObjCoord[Global.i+54] = highx;
    Global.camObjCoord[Global.i+55] = highy;
    Global.camObjCoord[Global.i+56] = lowz;

    Global.camObjCoord[Global.i+57] = highx;
    Global.camObjCoord[Global.i+58] = highy;
    Global.camObjCoord[Global.i+59] = highz;

    //BOTTOM
    Global.camObjCoord[Global.i+60] = highx;
    Global.camObjCoord[Global.i+61] = lowy;
    Global.camObjCoord[Global.i+62] = lowz;

    Global.camObjCoord[Global.i+63] = highx;
    Global.camObjCoord[Global.i+64] = lowy;
    Global.camObjCoord[Global.i+65] = highz;

    Global.camObjCoord[Global.i+66] = highx;
    Global.camObjCoord[Global.i+67] = lowy;
    Global.camObjCoord[Global.i+68] = lowz;

    Global.camObjCoord[Global.i+69] = highx;
    Global.camObjCoord[Global.i+70] = lowy;
    Global.camObjCoord[Global.i+71] = highz;
  }

我将Global中的i定义为0,并且始终 0 <为什么?我在打电话:

GLLayer layers = new GLLayer(this);
    layers.addcube(-6, -2, 2, 6);

......在另一个班级。

3 个答案:

答案 0 :(得分:4)

(我不敢相信我这么说,但是......)

添加多维数据集后,您是否忘记了Global.i += 72;

或者,您也可以为每个作业完成Global.camObjCoord[Global.i++] = ...。这是非常轻微的“改进”,因为至少您不必写+1+2+3,...然后{ {1}}最后。

但是,我担心这个建议弊大于利。如果可能的话,应该立即重新设计。一般来说,有比使用+=Global更好的选择,但鉴于这是针对OpenGL,这些选项可能适用也可能不适用。

答案 1 :(得分:2)

的Bleh。如果所有内容都允许访问相同的,可变的,全局(!)变量,那么这段代码将会出现问题。如下所示,以封装你想要的东西:

public class Cube
{
    private final float highX;
    private final float lowX;
    private final float highY;
    private final float lowY;
    private final float highZ;
    private final float lowZ;

    // TODO give domain-meaningful names to magic defaults for Y axis
    public Cube(float highx, float lowx, float highz, float lowz)
    {
        this(highx, lowx, 4.5f, 2.5f, highz, lowz);
    }

    public Cube(float highx, float lowx, float highy, float lowy, float highz, float lowz)
    {
       // TODO - assert the "high" parameters are strictly greater than the "lows"
       this.highX = highx;
       this.lowX = lowx;
       this.highY = highy;
       this.lowY = lowy;
       this.highZ = highz;
       this.lowZ = lowZ;
    }

   // TODO add relevant getter methods, and interesting methods like:
   public float getVolume() 
   {
       return (highX - lowX) * (highY - lowY) * (highZ - lowZ);
   }

   ...
}


public void addcube(Collection<? super Cube> cubes, float highx, float lowx, float highz, float lowz){
   //Constructing new cube...
   final Cube cube = new Cube(highx, lowx, highz, lowz);
   System.out.println("ADDING A CUBE!!"); // FIXME is this really necessary?

   // Note, adding to a specific collection of cubes rather than a
   // hard-coded global variable
   cubes.add(cube);
}
当你长时间盯着它时,“立方体”是一个奇怪的词。

答案 2 :(得分:1)

i + 1是表达式,而不是赋值。

考虑一下:

int i = 0; int x = i + 1; // <-- i does not increase by 1