This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList with Iterator new*

 
Ektaa Gargg
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What changes should i make in the below code so that it compiles & runs successfully :

Also i am looking forward to use only for-loop both normal and the enhanced for loop and not the while-loop to traverse through the list.

class x {
public static Iterator reverse(List list){
return list.iterator();
}

public static void main(String [] args){
List list = new ArrayList();
list.add("1");list.add("2");list.add("3");
for(Object obj:reverse(list))
System.out.print(obj + " " );
}
}
 
Ashok Kumar Babu
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

public interface Iterable<T>

Implementing this interface allows an object to be the target of the "foreach" statement.


Check this implementation

 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ektaa Gargg:
What changes should i make in the below code so that it compiles & runs successfully :


Did you try to compile it?
If so, did it compile?
If not, what error message(s) did the compiler return?

If you did compile it, did you try to run it?
If so, did it work?
If not, what exceptions were thrown?


We're happy to help but first we like to see some effort on your part.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic