Arraylist<String> into ArrayList<string> in java
ArrayList<String> constraints_list=new ArrayList<String>();
public void setConstraints(ArrayList<String> c)
{
c =开发者_如何学C constraints_list;
constr = true;
}
i want to make ArrayList c to be ArrayList constraint_list... how to do that?
It's not totally clear to me what you want to do. However, object references are passed by value in Java, so setting the value of c
(as in your code) won't work. The typical way to set c
from an instance variable is to write a getter rather than a setter:
public ArrayList<String> getConstraints() {
return constraints_list;
}
You can then say:
ArrayList<String> c = getConstraints();
If on the other hand, you are trying to do the opposite (set constraints_list from the passed in parameter), then your assignment is the wrong way round:
public void setConstraints(ArrayList<String> c) {
constraints_list = c;
constr = true;
}
You should also consider whether it's better to make a copy of the list, in which case you could do:
public void setConstraints(ArrayList<String> c) {
constraints_list = new ArrayList<String>(c);
constr = true;
}
精彩评论