I have an Arraylist of a class "Variable". Each "Variable" object has a name (String) and a value (int). Is there an efficient way to retrieve the object in the ArrayList that has a specific name? ie:
ArrayList<Variable> vars = new ArrayList<Variable>();
//Fill with values here//
for (int j = 0; j < vars.size(); j++) {
String nName = vars.get(j).getName();
//not real code, but what I would like
return vars.get(element.getName().equals(nName));
}
At the moment I am trying to accomplish this with lots of for loops and as well as looking ugly it isn't efficient. Thanks!
forloop is useless until you have anifstatement beforereturn. It breaks forj = 0at this moment.