Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help me out with this....  RSS feed

 
Christian Nash
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have an arraylist of names : ashley, ashwin, deepak, mahesh, zahara

how do i program to get the following output:
ashley, ashwin, deepak, mahesh and zahara.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a loop, check if you have reached the last element of the array,
using size()-1.
If you have not reached it, output ", ". Otherwise, output " and ".

I won't post the code
 
Christian Nash
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I tried but i still don't get the o/p i want.
Here is the code:

for (Iterator iter = arraylist.iterator(); iter.hasNext() {

if (!iter.hasNext()) {
System.out.print(iter.next() + " , ");

} else {
System.out.print("and");
}
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case an Iterator is not an approritate solution. Iterate through the list using the indices of the elements and then follow Satou's advice.
[ February 20, 2006: Message edited by: Garrett Rowe ]
 
Christian Nash
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Sorry for the previous post.
I solved the problem mylsef. Thanks for all the help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!