Search code examples
androidarraylistfinal

Android final variable is no variable?


I have a void like this:

public static void checkStateSignal(final int counter) {
    for (int l = 0; l < counter; l += 10) {
        if (counter - l < 9) {

            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                ArraySignal.LoggerStatus.get(counter) = separated[1];
                }
            },150);
        } 
    }
}

in which ArraySignal is a class and Loggerstatus is an Arraylist. I get an error on

ArraySignal.LoggerStatus.get(counter) = separated[1];

which is variable expected. So for some reason a final variable is not a variable and ArrayList.get doesn't want to get at a value position instead of a variable position which seems very illogical to me since you can get at a constant. Am i missing something? Also a solution for the problem would be very welcome.


Solution

  • This is because you are using the accesor .get(index) for the ArrayList and that return a value, you need to use the .add(index,elem) to add an element at the given position. Check this link to look around Add object to ArrayList at specified index