I have written such a simple code:
I have written this line in my class's constructor :
List element = new ArrayList();I have a variable named
costwhich its type isintone method will return three lists with different objects:
listOne, listTwo, listThreein another method I have written below code which this method will use those lists that are created in the method above.this method will be called three times for three lists above. each call for each list.
// begining of the method: int cost = 0; if(cost==0){ element = listOne; cost = 3; } if(cost<4){ element = listtwo; cost = 6; } // end System.out.println(element.toString());
Unfortunately, instead of printing listTwo it will print listThree (if we have 4 or more lists it will print the last one)!
Is there any problem with if-else condition?
thanks
EDIT: this is my main code but its condition is like the code above:
auxiliaryList in the code below is listOne or list Two or listThree respectively.
cost = 0;
public void method {
System.out.println(element.toString());//number one
if (cost== 0) {
element = auxiliaryList;
cost = 3;
}
else if( cost<4){
element =auxiliaryList;
cost = 6;
}
}
return;
}
}
also the line which is declared with //number one shows me that before going to the if/else condition ,the element list will be set to the current list in the method.