开发者

Problem with array assignment

I've got the following problem with an array assignment...

I've got a global float array in Global.java declared as...

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

I'm then filling its contents with.

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

I call this method like so...

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

The following is a debug print.开发者_如何学JAVA.

         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

So, as you can see.. When I'm assigning the values they just.. don't get assigned to the indexes.. why? All indexes remain equal to 0.0


You have i++ in there. So consider this

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

Otherwise, you're referencing the NEXT one every time.


This is related to a previous answer of mine (link to the question; make sure to also check the first revision), so I'm partly responsible for the confusion.

Let's step back and consider the following snippet:

    char[] arr = new char[3];
    int index;

    index = 0;
    arr[index]   = 'A';
    arr[index+1] = 'B';
    arr[index+2] = 'C';
    System.out.println(arr); // "ABC"

    index = 0;
    arr[index++] = '1';
    arr[index++] = '2';
    arr[index++] = '3';
    System.out.println(arr); // "123"

In the first part of the snippet, index remains the same, and we manually write index+1, +2, etc as necessary. In the second snippet, we simply use index++. Both technique "work" in assigning elements to the array in increasing indices, but arguably the ++ is slightly "better", because the programmer doesn't have to manually do the increment. Moreover, the index++ version allows for instant reordering of the statements, whereas the explicit index+n version would need renumbering.

Depending on what needs to be done, there are better alternatives to either of the above, of course.

In the most ideal scenario, you can use the special array initializer syntax:

    char[] arr = { 'X', 'Y', 'Z' };
    System.out.println(arr); // "XYZ"

On the post-increment operator

Perhaps this snippet will shed some light:

    char[] arr = { '1', '2', '3' };
    int index = 0;

    System.out.println(index);        // "0"
    arr[index++] = 'X';
    System.out.println(index);        // "1"

    System.out.println(arr[index]);   // "2"
    System.out.println(arr[index-1]); // "X"
    System.out.println(arr[0]);       // "X"

The middle part is the key to understand the problem: after the assignment, index has been incremented by 1. This is why arr[index] is 2, which is the old value of arr[1]. The new value X is properly assigned to arr[0], or more generally, arr[index-1] immediately following the assignment.

References

  • JLS 15.14.2 Postfix Increment Operator ++

Related questions

These questions are related to the post-increment operator:

  • post increment operator java
  • unable to make out this assignment in java
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜