0

I have a List for example

{ "in" , "out", "rec", "auth" } 

... but the content of the list is not predictable.

When iterating the list list how can we know we have reached last element? I want to apply different logic for the last element.

slim
  • 40,215
  • 13
  • 94
  • 127
user2693404
  • 307
  • 1
  • 3
  • 9
  • while iterating list i have to add comma after every element except last list element .how can i do ? – user2693404 Dec 12 '13 at 12:43
  • I've edited your question so that it means what I think you meant. I'm not sure what you mean by "dynamic", and it's probably the wrong word, but I left it in just in case. – slim Dec 12 '13 at 12:57
  • values will be different in list , we won't be knowing prior iterating what is the value in list.so we can't hard code to check for last element. – user2693404 Dec 12 '13 at 13:28
  • I have to iterate through list and append list value in stringbuffer and after every string object in list i have to add comma except for the last string object in list i don't want add comma in stringbuffer.Is there any clean way to do this? – user2693404 Dec 13 '13 at 09:20
  • http://stackoverflow.com/questions/523871/best-way-to-concatenate-list-of-string-objects – slim Dec 13 '13 at 14:34

3 Answers3

0

Example : List list = new ArrayList be the list, you need not traverse to get the last( element, you can get it by list.get(list.size()-1) and perform the logic you wanted.

0

The "classic" way to iterate through a Java List is to use List.iterator() to obtain an Iterator, then use the Iterator's methods to step through the list values.

This works with anything that implements Iterable, not just Lists.

 // assuming myList implements Iterable<Type>
 Iterator<Type> iterator = myList.iterator();
 while(iterator.hasNext()) {
      doSomethingWith(iterator.next())
 }

Since JDK 1.5, there has been a shortcut in the language to achieve the same thing:

 // assuming myList implements Iterable<Type>
 for(Type item : myList) {
      doSomethingWith(item);
 }

However, while convenient in many situations, this syntax doesn't give you full access to all the information Iterator has.

If you want to treat the last element of the list specially, one method might be:

 Iterator<Type> iterator = myList.iterator();
 while(iterator.hasNext()) {
      Type item = iterator.next();
      if(iterator.hasNext() {
         doSomethingWith(item);
      } else {
         // last item
         doSomethingElseWith(item);
      }
 }

Your specific situation - creating a comma-separated string representation of the list, without a trailing comma:

 Iterator<String> iterator = myList.iterator();
 StringBuilder buffer = new StringBuilder();
 while(iterator.hasNext()) {
      buf.append(iterator.next());
      if(iterator.hasNext() {
         buf.append(",")
      } 
 }

All this assumes that there's a reason you want to avoid using list.size().

slim
  • 40,215
  • 13
  • 94
  • 127
0

You should consider using LinkedList instad of ArrayList. It has getLast() method.

Marcin Szymczak
  • 11,199
  • 5
  • 55
  • 63
  • You should consider giving reasons. – slim Dec 12 '13 at 12:38
  • how linkedlist will help in this situation? – user2693404 Dec 12 '13 at 12:48
  • 1
    because it has getLast() method, which returns last element. – Marcin Szymczak Dec 12 '13 at 14:00
  • @MarcinSzymczak interesting. I tend to avoid using the specialised features of `List` implementations, so that they can easily be swapped out, but there are situations where getLast() might be useful. However this question doesn't want to request the last item. He wants to know how to recognise the last item when his iteration reaches it. – slim Dec 13 '13 at 15:15
  • ... and of course you can get the last item of any non-empty `List` with `l.get(l.size() - 1)` – slim Dec 13 '13 at 15:17