Syntaxis
Syntaxis

Reputation: 67

Variable changing value for unknown reason?

I should probably know this but why is this changing openPos[1] to 0? I know its got to do with the startPos[1] = 0 in the findCloseCurlBracket() method but why is it doing it it doesn't make sense.

int[] openPos = {1, 26};
    System.out.println(openPos[0]+", "+openPos[1]);
    int[] closePos = LuaTableParser.findCloseCurlBracket(stringList, openPos);
    System.out.println(openPos[0]+", "+openPos[1]);

findCloseCurlBracket() method:

public static int[] findCloseCurlBracket(List<String> text, int[] openPos) {
    int[] startPos = openPos;
    int counter = 1;
    for(int line = startPos[0]; line < text.size(); line++){
        for(int index = startPos[1]; index < text.get(line).length()-startPos[1]; index++){
            int[] curPos = {line, index};
            if(getStringListCharAt(text, curPos) == '{'){
                counter++;
            }else if(getStringListCharAt(text, curPos) == '}'){
                counter--;
            }
            if(counter == 0){
                startPos[1] = 5;
                return curPos;
            }
        }
        startPos[1] = 0;
    }
    return null;
}

Upvotes: 0

Views: 96

Answers (3)

Mircea Man
Mircea Man

Reputation: 98

Try cloning/copying the array.

int[] startPos = openPos; 

You're playing with the same reference here.

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1074495

Because openPos is a reference to (points to) an array. You're copying that reference (not the array, the reference to it) into findCloseCurlBracket as openPos. Then you're copying that reference into startPos so now the openPos variable, the openPos argument, and the startPos variable are all pointing to the same array in memory:

+-------------------+
| openPos variable  |--+
+-------------------+  |
                       |
+-------------------+  |     +-----------+
| openPos argument  |--+---->| the array |
+-------------------+  |     +-----------+
                       |     | 0: 1      |
+-------------------+  |     | 1: 26     |
| startPos variable |--+     +-----------+
+-------------------+

Then you do this:

startPos[1] = 5;

...which modifies the array:

+-------------------+
| openPos variable  |--+
+-------------------+  |
                       |
+-------------------+  |     +-----------+
| openPos argument  |--+---->| the array |
+-------------------+  |     +-----------+
                       |     | 0: 1      |
+-------------------+  |     | 1: 5      |
| startPos variable |--+     +-----------+
+-------------------+

Doesn't matter what reference you use, when you modify the state of what those references point to, you can see that change through all of them.

If you don't want to share the array, you'll need to make a copy of it, as Bathsheba shows in her answer (+1):

int[] startPos = openPos.clone();

Upvotes: 5

Bathsheba
Bathsheba

Reputation: 234715

startPos is referring to the same array as openPos: both things are references to an array of ints.

int[] startPos = openPos; does not take a deep copy of openPos.

Your writing startPos[1] = 5; is changing an element in the same array that openPos is referring to in the caller to findCloseCurlBracket.

One way to take a deep copy is to write int[] startPos = openPos.clone();.

Upvotes: 5

Related Questions