You are adding the same ArrayState object every time. You should create a new ArrayState object every time in the while loop to avoid it getting changed every time. This is because by default objects are always passed by reference in Java.
Try doing this:
ArrayList arrayListIdle = new ArrayList();
while(rs.next){
state = new State();
List<State> arrayState = new ArrayList<State>();
state.updateStateArray(arrayState);//This function mods the elements of (arrayState);//This
state.setArrayStates(arrayState);//add a list of arrayState to the object state
arrayListIdle.addAll(state);
}
Answer from Sapan Diwakar on Stack OverflowVideos
You are adding the same ArrayState object every time. You should create a new ArrayState object every time in the while loop to avoid it getting changed every time. This is because by default objects are always passed by reference in Java.
Try doing this:
ArrayList arrayListIdle = new ArrayList();
while(rs.next){
state = new State();
List<State> arrayState = new ArrayList<State>();
state.updateStateArray(arrayState);//This function mods the elements of (arrayState);//This
state.setArrayStates(arrayState);//add a list of arrayState to the object state
arrayListIdle.addAll(state);
}
The problem here is that you have one "arrayState" object and all of the state objects reference the same one.
One way to solve that here is to move the object creation inside loop so that a different object is created every time.
while(rs.next) {
List<State> arrayState = new ArrayList<State>();
...
}
Arrays.asList returns a fixed sized list backed by an array, and you can't add elements to it.
You can create a modifiable list to make addAll work :
List<String> supportedTypes = new ArrayList<String>(Arrays.asList("6500", "7600", "8700"));
This error also happens when the list is initialized with Collections.emptyList(), which is immutable:
List<String> myList = Collections.emptyList();
Instead, initialize it with a mutable list. For example
List<String> myList = new ArrayList<>();
Arrays.asList(coins) gives you a brand new list, but its not the usual type of ArrayList. It's a special type of list with a fixed size array underneath, which means you can't add or remove elements in it. It's stuck at the original size of coins although you can modify the values in it.
addAll(something) adds the coins to a list you've already got, which can be any kind of List. You can call it on an ArrayList that you've already created, and the number of elements will actually increase.
The thing you pass to addAll can't be an array though, it has to be some kind of Collection, because that's the way addAll is defined. In most cases, the simplest thing to do is to pass it one of the unresizeable lists that you get from Array.asList, so something like
myList.addAll(Arrays.asList(coins));
because addAll is defined as:
addAll(Collection<? extends E> c)
it can only take a collection as an argument. coins is not a collection.
when you do:
for(Coin coin : coins)
myCoins.add(coin)
This is fine because you're adding a Coin reference to a List<Coin>.