I have a function in which user inputs numbers and those numbers are added to an Arraylist. That list is passed to another class for process and the return value is printed out. But when the value is returned, it changes the value of the original Arraylist used to call the class.
System.out.println("NATIVE LIST : "+loN); // original value - [2,+,3]
Evaluate evaluate = new Evaluate();
if (loN.size() > 2) {
evaluate.setR(loN);
System.out.println(evaluate.start()); // prints 5.0
}
System.out.println(loN); // value gets changed [5.0]
code for Evaluate Class :
public void setR(ArrayList<String> r) {
question = r;
}
public Number start(){
if (question.size() == 1){
return Double.parseDouble(question.get(0));
}else{
test();
}
}
private void check_pos(String c){
if (question.contains(c)){
pos = question.indexOf(c);
}
}
int test(){
try {
String val = null;
if (question.contains("+")) {
check_pos("+");
val = String.valueOf(Double.parseDouble(question.get(pos - 1)) +
Double.parseDouble(question.get(pos + 1)));
}
if (pos != question.size()-1) {
question.set(pos - 1, val);
question.remove(pos + 1);
question.remove(pos);
start();
}
}catch (Exception e){
e.printStackTrace();
}
return 2709;
}