Hi I am new to java and am trying to understand the arraylist. I am using Arraylist theList and dummyValues. I read the values from theList and update the float values in dummyValues. My code snippet is as follows `
public static void generateValues(ArrayList<Float> theList) {
for (int j = 0; j < theList.size(); j++) {
if (dummyValues.size()==0)
dummyValues.add(j, theList.get(j));
else
dummyValues.set(j, theList.get(j));
}
}
I trying add the values to the ArrayList dummyValues in the first condition and in the second condition if the size dummyValues is greater than 0 just update the values of dummyValues. I have considered this method to avoid duplicate copies.
But, when I execute it I get the following error :
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
The error occurs here dummyValues.set(j, theList.get(j));
I know this is a trivial concept, any kind of help is appreciated
dummValuesandtheListdo have the same size? otherwise you might be ignoring values or you get your exception. In this particular case it seems thattheListis greater thendummyList.dummyValueshas only one element .but you call set for index 1 and etc...since there is only one element you can only set for index 0