假设变量和2个示例中的数字具有相同的数字......这两种填充数组的不同方式的最终内容是否有任何差异?我错了,假设它是一样的吗?
基本上我试图用OpenGL渲染图形,第一个例子不起作用,但第二个例子不行。
示例1 ......
全球声明是...... public static final 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;
}
示例2 ......
final static float camObjCoord[] = new float[] {
// FRONT
-2.0f, -1.5f, 2.0f,
2.0f, -1.5f, 2.0f,
-2.0f, 1.5f, 2.0f,
2.0f, 1.5f, 2.0f,
// BACK
-2.0f, -1.5f, -2.0f,
-2.0f, 1.5f, -2.0f,
2.0f, -1.5f, -2.0f,
2.0f, 1.5f, -2.0f,
// LEFT
-2.0f, -1.5f, 2.0f,
-2.0f, 1.5f, 2.0f,
-2.0f, -1.5f, -2.0f,
-2.0f, 1.5f, -2.0f,
// RIGHT
2.0f, -1.5f, -2.0f,
2.0f, 1.5f, -2.0f,
2.0f, -1.5f, 2.0f,
2.0f, 1.5f, 2.0f,
// TOP
-2.0f, 1.5f, 2.0f,
2.0f, 1.5f, 2.0f,
-2.0f, 1.5f, -2.0f,
2.0f, 1.5f, -2.0f,
// BOTTOM
-2.0f, -1.5f, 2.0f,
-2.0f, -1.5f, -2.0f,
2.0f, -1.5f, 2.0f,
2.0f, -1.5f, -2.0f,
};
//编辑我将addcube(6, 2, 6, 2);
添加到第一个示例,ARRAY等于.. [F@4499e290
。为什么阵列分配会搞乱?
答案 0 :(得分:1)
您的第一个示例假设在调用addcube
时Global.i
被初始化为某个合理值。如果Global.i
发生更改,多次调用addcube可能会初始化同一数组的不同部分。 addcube
方法还会修改您在第二个示例中没有执行的Glocal.cubes
的值。
您的第二个示例直接创建了一个所需大小的数组,而不使用Global.i
的值。这使得它更简单,更健壮,但也更不易配置。
答案 1 :(得分:0)
在第二个例子中(确实更少输入),数组的大小也是“按需”包含元素 - 而不是在第一个例子中是8000浮点数 - 这可能是第一个例子不起作用的原因
答案 2 :(得分:0)
我很确定我没有确切的说法,但如果我试图建立一个这样的方块,我可能会去:
for(float x : new float[]{-2.0f, 2.0f} )
for(float y : new float[]{-1.5f, 1.5f} )
create new Point(x,y);
可以很容易地看出给-2,-1.5; -2,1.5; 2,-1.5& 2,1.5应该是正方形的4个角点。
似乎添加一个z轴只是在y之后添加一个z级别(我会稍微清理一下语法):
float[] xpts=new float[]{-2.0f, 2.0f};
float[] ypts=new float[]{-1.5f, 1.5f};
float[] zpts=new float[]{-2.0f, 2.0f}
for(float x : xpts )
for(float y : ypts )
for(float z : zpts )
create_new_3D_Point(x,y,z);
我实际上在考虑了一段时间之后给了它很好的工作机会。如果有人试过,请告诉我。
我不希望列出这样的数字,重复是容易出错和麻木麻木的。将其缩小为模式可以消除错字式错误,并帮助您识别模式以进一步优化。
答案 3 :(得分:0)
我将
addcube(6, 2, 6, 2);
添加到第一个示例,ARRAY等于..[F@4499e290
。为什么阵列分配会搞乱?
它没有弄乱!如果你试图打印一个你得到的那种数组。
[F@4499e290
是在float[]
对象上调用Object.toString()的正常结果。Object.toString()
方法。类似于字符串连接。如果要打印数组内容,则需要编写一些额外的代码来执行此操作。
顺便说一下,当你弄清楚你的问题是什么时,你应该尝试让你的应用程序设计更加面向对象。所有使用全局/静态的都是丑陋的,如果你曾经(甚至)希望在同一个JVM中运行多个应用程序实例,那么会导致你遇到麻烦。