Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

help me

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public static Iterator reverse(List list) {
Collections.reverse(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 + ", ");
}
What is the result?

result is Compilation fails. how?
 
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is you are returning Iterator object from reverse() method. To the for-each loop you have to provide collection, which in this case List.
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Al Mamun

Please can you explain the above concept in detail
bcoz i am not getting what actualy is being returned?
and why we
cant traverse in a reverse order ?although we are
inserting some elements in List...........
It will be kinda of you ................


Thanks in advance............
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
foreach syntax:
for(variable:collection)

works for Collections and arrays
 
Abdullah Mamun
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dhwani

Originally posted by jone wiilum:
public static Iterator reverse(List list) {
Collections.reverse(list);
return list.iterator();
}



Look at the bold lines in the above code. The return type is Iterator which is using to traverse the list, but for-each construct only allows collection or array.

Originally posted by dhwani mathur:
...why we cant traverse in a reverse order ?...


There is no problem regarding traverse in a reverse order in this example.
[ August 23, 2007: Message edited by: Al Mamun ]
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Al Mamun!!


Thanks for your explanation
now i am clear with the concept.....



Preparing SCJP 1.5
reply
    Bookmark Topic Watch Topic
  • New Topic