In my java function i want to use a list as parameter, like List 1 = (a,b,c). Where every value is a value from another list, List 2 = (1, 2, 3). Now i want to call my function with every possible combination to fill it.
Like a=1, b=1, c=1; a=1, b=2, c=1; a=1, b=3, c=1, etc.
Problem is the list length for either is changed every time the function is called.
Usually i would use two for loops, or a simple function with recursion. But i dont know how exactly to deal with the changing list length. Perhaps my question was to broad, my problem is in not knowing how to change just one value in the list, until every combination is called.
The function i imagine looks something like this: (i know the function with a for loop will not work, i must use an iterator)
List 1 = (a,b,c);
List 2 = (1,2,3);
list1.set(a, 1);
list1.set(b, 1);
list1.set(c, 1);
functionFillList(List list1){
for(Element e : list1){
//do something
//in some cases add elements to list2
}
//change one value of list1
functionFillList(list1);
//if every combination was called -> end function
}
答案 0 :(得分:0)
Using loop for changing the data of the list will throw concurrent modification error, use Iterator
instead of loop to change the list data. Iterator allows to modify the data.
void functionFillList(List list1){
Iterator<String> iter = list1.iterator();
while(iter.hasNext()){
//do something
}
//change one value of list1
functionFillList(list1);
//if every combination was called -> end function
}