• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArrayList with Iterator new*

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 + " " );
}
}
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic