0

I have an ArrayList of some elements path = [1, 2, 5, 7]. Now I want to get values from these elements like:

1,2 
2,5
5,7 

I used list iterator for this purpose, but what I am getting is:

1,2 
2,2     
5,7 
7,7

I have seen this answer: how to get all combination of an arraylist? But I don't need all the combinations like this.

My code is:

public class iterator {

public static void main(String[] args){
    ArrayList<String> path=new ArrayList<>();
    path.add("1");
    path.add("2");
    path.add("5");
    path.add("7");

    System.out.println(path);
    ListIterator<String> itr =  path.listIterator();  
    while(itr.hasNext())
    {
        System.out.println(itr.next()+ "," + itr.next());
        System.out.println(itr.previous()+ "," + itr.next());
    }

}

}

My question is how can I get my required result?

Community
  • 1
  • 1
Nitu08
  • 47
  • 8

3 Answers3

1
for (int i = 0; i < path.size() - 1; i++) {
    System.out.println(path.get(i) + "," + path.get(i + 1));
}
shmosel
  • 49,289
  • 6
  • 73
  • 138
1

You can try traditional for loop with counter to achieve this:

for (int i = 0; i < path.size() - 1; i++) {
    System.out.println(path.get(i) + ", " + path.get(i+1));
}
4castle
  • 32,613
  • 11
  • 69
  • 106
Vishal
  • 549
  • 3
  • 13
  • `Cannot find symbol path.length` – Tibrogargan May 02 '16 at 03:43
  • Thanks its working ...just need a little change ,instead of path.length, path.size() is working. – Nitu08 May 02 '16 at 03:47
  • @Tibrogargan People are not always on eclipse to compile what they post here. The one I posted was enough to explain what needs to be done. Just because I typed length and not size() shouldn't mean the answer needs to be downvoted, does it? – Vishal May 02 '16 at 03:52
  • @Vishal Kamat you got downvoted because your answer does not work. Make it work I'll remove the downvote – Tibrogargan May 02 '16 at 03:59
  • Ok looks like 4castle already fixed it. Thanks guys :) I love this forum – Vishal May 02 '16 at 04:01
  • @Tibrogargan You might be interested in reading [Why do you cast downvotes on answers?](http://meta.stackexchange.com/q/2451) I won't judge your interpretation of what merits a downvote, but you can accomplish much more with a comment. Using downvotes to hold people ransom is not polite. – 4castle May 02 '16 at 04:03
  • @ 4castle where's the beef? His answer was wrong. Seems a lot of people downvote for wrong. Now it's just identical to the answer of the person that posted before him. – Tibrogargan May 02 '16 at 04:08
  • @Tibrogargan It's fine. They probably came up with it independently, and the other guy was just faster. There's nothing wrong with duplicate answers. Especially if one explains it better than the other. – 4castle May 02 '16 at 04:17
-1

The issue is that .next() gets the next element and advances the cursor. To use your ListIterator, you would do it like this:

while (itr.nextIndex() + 1 < path.size()) {
    System.out.println(itr.next() + "," + itr.next());
    itr.previous(); // back up 1 to get the duplicate
}
4castle
  • 32,613
  • 11
  • 69
  • 106
  • Obviously this isn't the best answer, but just in case you wanted to know why the `ListIterator` wasn't working, this should be helpful. – 4castle May 02 '16 at 04:18