java - 数组赋值问题

标签 java arrays floating-point

我在数组赋值时遇到以下问题...

我在 Global.java 中有一个全局 float 组,声明为...

public static float camObjCoord[] = new float[8000];

然后我将填充其内容。

public void addcube(float highx, float lowx, float highz, float lowz){
            //Constructing new cube...
    System.out.println("f = " + f);
            Global.cubes++;
            float y = 1.5f;
            System.out.println("highx = " + highx + "lowx = " + lowx + "highz = " +         highz + "lowz = " + lowz);
            //FRONT
            Global.camObjCoord[Global.i++] = highx;
            System.out.println("cube i = " + Global.i);
    }

我这样称呼这个方法......

addcube(-2.0f, 2.0f, -2.0f, 2.0f);

以下是调试打印..

         07-13 16:01:16.220: INFO/System.out(4837): highx = -2.0lowx = 2.0highz = -   2.0lowz = 2.0       //This is the values coming in to the class
         07-13 16:01:16.220: INFO/System.out(4837): 0.0 //This is the contents of index 0
         07-13 16:01:16.220: INFO/System.out(4837): cube i = 1
         07-13 16:01:16.220: INFO/System.out(4837): 0.0 //Contents of i 1
         07-13 16:01:16.220: INFO/System.out(4837): cube i = 2
         07-13 16:01:16.220: INFO/System.out(4837): 0.0 //Contents of i 2
         07-13 16:01:16.220: INFO/System.out(4837): cube i = 3
         07-13 16:01:16.230: INFO/System.out(4837): 0.0 //Contents of i 3

所以,正如你所看到的.. 当我分配值时,它们只是.. 没有被分配给索引.. 为什么?所有索引均保持等于 0.0

最佳答案

你那里有 i++ 。所以考虑一下这个

Global.cam[i] = highx;
DebugPrint("cube i = " + cam[i]);
i++;

否则,您每次都会引用下一个。

关于java - 数组赋值问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3238690/

相关文章:

java - 如何在我的 ANTLR4 Java 扩展监听器类中实现减法表达式?

java - 在 Android 上处理 gzip 内容

java - 如何使用 Jackson 解析大型嵌套 JSON 文件?

c - 显示分数以及每个分数获得的次数

c++ - 与 `std::chrono` 类型相比,为什么使用 `float` 进行时差测量会给出更多有效数字的 `double` 类型?

c++ - 如何在C++中写小数点后两位数?

java - DocumentBuilder 线程安全吗?

java - 从数组创建适配器(AutoCompleteTextView)

javascript - 如果重复则从数组中删除两个值 - JavaScript/jQuery

c - float 结果不正确