1

I have troubles returning a value from a loop in method.

I've tried this way: (which returns classname, my initialise String instead of classes.get(i).className)

public String getClassName(){
    String cName = "classname";
    for (int i=0; i<classes.size(); i++){
        cName = classes.get(i).className;
    }
return cName;
}    

and I've tried this as well: (which returns c instead of classes.get(i).className)

public String getClassName(){
    String cName = "classname";
    String c = "c";
    for (int i=0; i<classes.size(); i++){
        c = classes.get(i).className;
    }
    cName = c;
    return cName;
}    

Please tell me how to return classes.get(i).className!! Thanks a lot :P

Sue
  • 199
  • 1
  • 3
  • 10
  • Based on what do you need return the value?! Because in your case, the last value from the classes list is getting returned. – Rahul Oct 27 '13 at 10:56
  • Does it mean that my code has no problem but there's actually nothing stored in the ArrayList, so it returns the initialsed value? – Sue Oct 27 '13 at 11:00
  • Yes and no. Syntactically, your code is correct and if there is data in the list, you'd get a different output(not the `className` which you set by default). But logically, its not as it'll always return the last element from the list and I really doubt if you want that, cuz if that's the case, `return classes.get(classes.size() - 1).className;` should suffice. You needn't even iterate through the list. – Rahul Oct 27 '13 at 11:02
  • @R.J Thanks. but if the `ArrayList classes` has 0 items initially, I am afraid that using`return classes.get(classes.size() - 1).className` would get the `java.lang.ArrayIndexOutOfBoundsException: -1` error – Sue Oct 27 '13 at 11:07
  • You'd could have a null/empty check for that, before the return. As simple as that. Also, since its a list, you'll get the [`IndexOutOfBoundsException`](http://docs.oracle.com/javase/7/docs/api/java/lang/IndexOutOfBoundsException.html). – Rahul Oct 27 '13 at 11:08
  • Should I use `if (classes.size()<=0) return null; else return classes.get(classes.size() - 1).className;`? but I thought we can only return one value in a return method. Can this work? – Sue Oct 27 '13 at 11:12
  • Use `classes.isEmpty()` in that case and yes, you can return only 1 value from a method and the logic you gave will also return only 1 value from the method. So no issues. – Rahul Oct 27 '13 at 11:19
  • Thank you very much. Would you mind helping me out in http://stackoverflow.com/questions/19617492/failed-to-store-values-in-arraylist-of-class-object ? – Sue Oct 27 '13 at 11:27

4 Answers4

1

There is nothing stored in classes. Make sure you have in there what you think you do. Try putting this inside the for loop to see what's going on:

System.out.print("class name = " + cName);

If this never prints out, you know that classes is empty. At any time inside the for loop, you can call return, for example if the class name matches something you can test for in an if statement.

for (int i=0; i<classes.size(); i++){
    cName = classes.get(i).className;
    return cName;
}

Also, could you upvote, or mark someone as correct? (I'm new and would love to have some good answers marked as such :) )

Josh T
  • 564
  • 3
  • 12
0

I don't know what are you trying to do, but your code is equivalent to:

public String getClassName() {
    return classes.get(classes.size() - 1).className;
}

Are you sure this is what you want to return? You should be careful in case classes is empty as you might get ArrayIndexOutOfBoundsException.

Maroun
  • 94,125
  • 30
  • 188
  • 241
0

While Maroun is right bout the code only returning the last value in the classes collection, the reality is that in your example the collection is always empty, and thus the for loop does not execute even once. Make sure the classes collection contain what you think it should!

rolfl
  • 17,539
  • 7
  • 42
  • 76
0

That should be because you dont enter the for loop mainly because you list "classes" is empty. You can check it this way-

if(classes.size()==0)
   System.out.println("Classes is empty");
Ankit Rustagi
  • 5,539
  • 12
  • 39
  • 70