I have 2 lists in my program; one of the list already has data. I need to create a new List and pass few data from previous list.
Following is the code I use :
List<Class1> result = function();
List<Class2> newList = new ArrayList<Class2>();
for(int i=0; i < result.size(); i++) {
newList.get(i).setValue(result.get(i).getValue());
//setValue() is setter function in Class2
//getValue() is getter function in Class1
}
return newList;
I get an error in this particular line when I try to run the code:
newList.get(i).setValue(result.get(i).getValue());
The error is :
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
java.util.ArrayList.rangeCheck(ArrayList.java:635)
java.util.ArrayList.get(ArrayList.java:411)
How do I resolve this issue ? Need some help with it.