When attempting to initialize an ArrayList with an index as a value, I encounter the error message "local variables referenced from an inner class must be final or effectively final at <add(index);>"
int index=0;
for (int i:nums){
if (!map.containsKey(i)){
ArrayList<Integer> al1=new ArrayList<Integer>(){{
add(index);
}};
map.put(i,al1);
}
index+=1;
}
I know there are possible walkarounds where I can just simply declare arraylist then add value to it separately, this works totally fine.
ArrayList<Integer> al1=new ArrayList<Integer>();
al1.add(index);
map.put(i,al1);
But I want to understand whether there's any way to achieve it during initialization itself. Please help me with this. Thanks in advance!