tags:

views:

73

answers:

2

Hey guys,

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

         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

+5  A: 

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.

glowcoder
+1 beat me to it :-)
Péter Török
The point is, by the time you look at cam[i], you have already incremented i, because the line above includes i++. In case that wasn't clear.
Carl Manaster
Yep. Global.i++ is the same as something like `{old_i = Global.i; Global.i = Global.i+1; return old_i;}`
extraneon
@Carl thanks for that clarification. That's exactly my point. Honestly, raise your hand if you've never been bit by this? :-)
glowcoder
@glowcoder: My hand certainly isn't up!
Carl Manaster
+1  A: 

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

Related questions

These questions are related to the post-increment operator:

polygenelubricants
Which actually suggests an alternative (not one I'd recommend, but one that might help explain what's going on). Instead of the post-increment operator, use the pre-increment operator (and initialize your counter to -1 instead of 0). That way, the index you're assigning and the index you're printing will be the same. But it's un-idiomatic and probably a bad idea.
Carl Manaster
@Carl: I thought about that too, but based on the trends of OP's question I'd say that OP is overwhelmed by many things already.
polygenelubricants