Why Is My Iterator Not Iterating Over The List Correctly?
I am trying to learn about iterators and linked lists. So, as I typically do, I've been toying around with the code and I cannot figure out how the program is iterating over my list.
LinkedList<String> matchedList = new LinkedList<>();
matchedList.addFirst("Cowabunga");
ListIterator<String> iterator = matchedList.listIterator();
iterator.add("Reee");
iterator.add("Moo");
System.out.println(matchedList);
System.out.println(iterator.previous());
System.out.println(iterator.previous());
As output I get:
[Reee, Moo, Cowabunga]
java.util.LinkedList$ListItr@15db9742
Moo
Reee
But I am confused. I thought that the cursor was initially between the items: "Cowabunga" and "Ree" where Cowabunga was the first element in the list. This, being due to it is when I linked the lists together. However as output, I get Moo and then Ree when I go to the previous twice. Why is this?
java
add a comment |
I am trying to learn about iterators and linked lists. So, as I typically do, I've been toying around with the code and I cannot figure out how the program is iterating over my list.
LinkedList<String> matchedList = new LinkedList<>();
matchedList.addFirst("Cowabunga");
ListIterator<String> iterator = matchedList.listIterator();
iterator.add("Reee");
iterator.add("Moo");
System.out.println(matchedList);
System.out.println(iterator.previous());
System.out.println(iterator.previous());
As output I get:
[Reee, Moo, Cowabunga]
java.util.LinkedList$ListItr@15db9742
Moo
Reee
But I am confused. I thought that the cursor was initially between the items: "Cowabunga" and "Ree" where Cowabunga was the first element in the list. This, being due to it is when I linked the lists together. However as output, I get Moo and then Ree when I go to the previous twice. Why is this?
java
1
I thought that the cursor was initially between the items: "Cowabunga" and "Ree" ... No, not until you callnext()
. The cursor starts before the first element. Read the documentation.
– shmosel
Nov 6 '18 at 0:25
1
docs.oracle.com/javase/8/docs/api/java/util/ListIterator.html has a nice diagram showing the cursor position. As you can see, the first position is before the first element.
– JB Nizet
Nov 6 '18 at 0:25
To generalize and summarize rgettman's answer: don't change the length of an iterable object as you iterate over it--regardless of the programming language, exact data structure, or whether you're appending or removing elements. I just spent way more time than I care to admit trying to fix a bug in Python that was caused by me removing elements from a list as I iterated over it.
– Matthew Milone
Jan 2 at 7:00
add a comment |
I am trying to learn about iterators and linked lists. So, as I typically do, I've been toying around with the code and I cannot figure out how the program is iterating over my list.
LinkedList<String> matchedList = new LinkedList<>();
matchedList.addFirst("Cowabunga");
ListIterator<String> iterator = matchedList.listIterator();
iterator.add("Reee");
iterator.add("Moo");
System.out.println(matchedList);
System.out.println(iterator.previous());
System.out.println(iterator.previous());
As output I get:
[Reee, Moo, Cowabunga]
java.util.LinkedList$ListItr@15db9742
Moo
Reee
But I am confused. I thought that the cursor was initially between the items: "Cowabunga" and "Ree" where Cowabunga was the first element in the list. This, being due to it is when I linked the lists together. However as output, I get Moo and then Ree when I go to the previous twice. Why is this?
java
I am trying to learn about iterators and linked lists. So, as I typically do, I've been toying around with the code and I cannot figure out how the program is iterating over my list.
LinkedList<String> matchedList = new LinkedList<>();
matchedList.addFirst("Cowabunga");
ListIterator<String> iterator = matchedList.listIterator();
iterator.add("Reee");
iterator.add("Moo");
System.out.println(matchedList);
System.out.println(iterator.previous());
System.out.println(iterator.previous());
As output I get:
[Reee, Moo, Cowabunga]
java.util.LinkedList$ListItr@15db9742
Moo
Reee
But I am confused. I thought that the cursor was initially between the items: "Cowabunga" and "Ree" where Cowabunga was the first element in the list. This, being due to it is when I linked the lists together. However as output, I get Moo and then Ree when I go to the previous twice. Why is this?
java
java
asked Nov 6 '18 at 0:17
GorgochefGorgochef
495
495
1
I thought that the cursor was initially between the items: "Cowabunga" and "Ree" ... No, not until you callnext()
. The cursor starts before the first element. Read the documentation.
– shmosel
Nov 6 '18 at 0:25
1
docs.oracle.com/javase/8/docs/api/java/util/ListIterator.html has a nice diagram showing the cursor position. As you can see, the first position is before the first element.
– JB Nizet
Nov 6 '18 at 0:25
To generalize and summarize rgettman's answer: don't change the length of an iterable object as you iterate over it--regardless of the programming language, exact data structure, or whether you're appending or removing elements. I just spent way more time than I care to admit trying to fix a bug in Python that was caused by me removing elements from a list as I iterated over it.
– Matthew Milone
Jan 2 at 7:00
add a comment |
1
I thought that the cursor was initially between the items: "Cowabunga" and "Ree" ... No, not until you callnext()
. The cursor starts before the first element. Read the documentation.
– shmosel
Nov 6 '18 at 0:25
1
docs.oracle.com/javase/8/docs/api/java/util/ListIterator.html has a nice diagram showing the cursor position. As you can see, the first position is before the first element.
– JB Nizet
Nov 6 '18 at 0:25
To generalize and summarize rgettman's answer: don't change the length of an iterable object as you iterate over it--regardless of the programming language, exact data structure, or whether you're appending or removing elements. I just spent way more time than I care to admit trying to fix a bug in Python that was caused by me removing elements from a list as I iterated over it.
– Matthew Milone
Jan 2 at 7:00
1
1
I thought that the cursor was initially between the items: "Cowabunga" and "Ree" ... No, not until you call
next()
. The cursor starts before the first element. Read the documentation.– shmosel
Nov 6 '18 at 0:25
I thought that the cursor was initially between the items: "Cowabunga" and "Ree" ... No, not until you call
next()
. The cursor starts before the first element. Read the documentation.– shmosel
Nov 6 '18 at 0:25
1
1
docs.oracle.com/javase/8/docs/api/java/util/ListIterator.html has a nice diagram showing the cursor position. As you can see, the first position is before the first element.
– JB Nizet
Nov 6 '18 at 0:25
docs.oracle.com/javase/8/docs/api/java/util/ListIterator.html has a nice diagram showing the cursor position. As you can see, the first position is before the first element.
– JB Nizet
Nov 6 '18 at 0:25
To generalize and summarize rgettman's answer: don't change the length of an iterable object as you iterate over it--regardless of the programming language, exact data structure, or whether you're appending or removing elements. I just spent way more time than I care to admit trying to fix a bug in Python that was caused by me removing elements from a list as I iterated over it.
– Matthew Milone
Jan 2 at 7:00
To generalize and summarize rgettman's answer: don't change the length of an iterable object as you iterate over it--regardless of the programming language, exact data structure, or whether you're appending or removing elements. I just spent way more time than I care to admit trying to fix a bug in Python that was caused by me removing elements from a list as I iterated over it.
– Matthew Milone
Jan 2 at 7:00
add a comment |
2 Answers
2
active
oldest
votes
First, as an aside, you must have an extra print statement in your code where you printed your ListIterator
, which would explain the output java.util.LinkedList$ListItr@15db9742
.
Next, a ListIterator
(and an Iterator
) has its initial position before the first element, which is where the add
method adds the element.
Inserts the specified element into the list (optional operation). The element is inserted immediately before the element that would be returned by next(), if any, and after the element that would be returned by previous(), if any.
The new element is inserted before the implicit cursor: a subsequent call to next would be unaffected, and a subsequent call to previous would return the new element.
So when you create your ListIterator
, this is the state, with the "cursor" position of the iterator before the first element.
list: "Cowabunga"
itr ^
When you use the iterator to add elements, the position remains before the same element, even as more elements are added before the position:
list: "Reee" -> "Moo" -> "Cowabunga"
itr ^
Then the previous()
method returns the element before the position and advances it backwards.
list: "Reee" -> "Moo" -> "Cowabunga"
itr ^
list: "Reee" -> "Moo" -> "Cowabunga"
itr ^
add a comment |
see https://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html#add(E)
Inserts the specified element into the list (optional operation). The
element is inserted immediately before the element that would be
returned by next(), if any, and after the element that would be
returned by previous(), if any. (If the list contains no elements, the
new element becomes the sole element on the list.) The new element is
inserted before the implicit cursor: a subsequent call to next would
be unaffected, and a subsequent call to previous would return the new
element. (This call increases by one the value that would be returned
by a call to nextIndex or previousIndex.)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53164140%2fwhy-is-my-iterator-not-iterating-over-the-list-correctly%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
First, as an aside, you must have an extra print statement in your code where you printed your ListIterator
, which would explain the output java.util.LinkedList$ListItr@15db9742
.
Next, a ListIterator
(and an Iterator
) has its initial position before the first element, which is where the add
method adds the element.
Inserts the specified element into the list (optional operation). The element is inserted immediately before the element that would be returned by next(), if any, and after the element that would be returned by previous(), if any.
The new element is inserted before the implicit cursor: a subsequent call to next would be unaffected, and a subsequent call to previous would return the new element.
So when you create your ListIterator
, this is the state, with the "cursor" position of the iterator before the first element.
list: "Cowabunga"
itr ^
When you use the iterator to add elements, the position remains before the same element, even as more elements are added before the position:
list: "Reee" -> "Moo" -> "Cowabunga"
itr ^
Then the previous()
method returns the element before the position and advances it backwards.
list: "Reee" -> "Moo" -> "Cowabunga"
itr ^
list: "Reee" -> "Moo" -> "Cowabunga"
itr ^
add a comment |
First, as an aside, you must have an extra print statement in your code where you printed your ListIterator
, which would explain the output java.util.LinkedList$ListItr@15db9742
.
Next, a ListIterator
(and an Iterator
) has its initial position before the first element, which is where the add
method adds the element.
Inserts the specified element into the list (optional operation). The element is inserted immediately before the element that would be returned by next(), if any, and after the element that would be returned by previous(), if any.
The new element is inserted before the implicit cursor: a subsequent call to next would be unaffected, and a subsequent call to previous would return the new element.
So when you create your ListIterator
, this is the state, with the "cursor" position of the iterator before the first element.
list: "Cowabunga"
itr ^
When you use the iterator to add elements, the position remains before the same element, even as more elements are added before the position:
list: "Reee" -> "Moo" -> "Cowabunga"
itr ^
Then the previous()
method returns the element before the position and advances it backwards.
list: "Reee" -> "Moo" -> "Cowabunga"
itr ^
list: "Reee" -> "Moo" -> "Cowabunga"
itr ^
add a comment |
First, as an aside, you must have an extra print statement in your code where you printed your ListIterator
, which would explain the output java.util.LinkedList$ListItr@15db9742
.
Next, a ListIterator
(and an Iterator
) has its initial position before the first element, which is where the add
method adds the element.
Inserts the specified element into the list (optional operation). The element is inserted immediately before the element that would be returned by next(), if any, and after the element that would be returned by previous(), if any.
The new element is inserted before the implicit cursor: a subsequent call to next would be unaffected, and a subsequent call to previous would return the new element.
So when you create your ListIterator
, this is the state, with the "cursor" position of the iterator before the first element.
list: "Cowabunga"
itr ^
When you use the iterator to add elements, the position remains before the same element, even as more elements are added before the position:
list: "Reee" -> "Moo" -> "Cowabunga"
itr ^
Then the previous()
method returns the element before the position and advances it backwards.
list: "Reee" -> "Moo" -> "Cowabunga"
itr ^
list: "Reee" -> "Moo" -> "Cowabunga"
itr ^
First, as an aside, you must have an extra print statement in your code where you printed your ListIterator
, which would explain the output java.util.LinkedList$ListItr@15db9742
.
Next, a ListIterator
(and an Iterator
) has its initial position before the first element, which is where the add
method adds the element.
Inserts the specified element into the list (optional operation). The element is inserted immediately before the element that would be returned by next(), if any, and after the element that would be returned by previous(), if any.
The new element is inserted before the implicit cursor: a subsequent call to next would be unaffected, and a subsequent call to previous would return the new element.
So when you create your ListIterator
, this is the state, with the "cursor" position of the iterator before the first element.
list: "Cowabunga"
itr ^
When you use the iterator to add elements, the position remains before the same element, even as more elements are added before the position:
list: "Reee" -> "Moo" -> "Cowabunga"
itr ^
Then the previous()
method returns the element before the position and advances it backwards.
list: "Reee" -> "Moo" -> "Cowabunga"
itr ^
list: "Reee" -> "Moo" -> "Cowabunga"
itr ^
answered Nov 6 '18 at 0:29
rgettmanrgettman
151k21208293
151k21208293
add a comment |
add a comment |
see https://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html#add(E)
Inserts the specified element into the list (optional operation). The
element is inserted immediately before the element that would be
returned by next(), if any, and after the element that would be
returned by previous(), if any. (If the list contains no elements, the
new element becomes the sole element on the list.) The new element is
inserted before the implicit cursor: a subsequent call to next would
be unaffected, and a subsequent call to previous would return the new
element. (This call increases by one the value that would be returned
by a call to nextIndex or previousIndex.)
add a comment |
see https://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html#add(E)
Inserts the specified element into the list (optional operation). The
element is inserted immediately before the element that would be
returned by next(), if any, and after the element that would be
returned by previous(), if any. (If the list contains no elements, the
new element becomes the sole element on the list.) The new element is
inserted before the implicit cursor: a subsequent call to next would
be unaffected, and a subsequent call to previous would return the new
element. (This call increases by one the value that would be returned
by a call to nextIndex or previousIndex.)
add a comment |
see https://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html#add(E)
Inserts the specified element into the list (optional operation). The
element is inserted immediately before the element that would be
returned by next(), if any, and after the element that would be
returned by previous(), if any. (If the list contains no elements, the
new element becomes the sole element on the list.) The new element is
inserted before the implicit cursor: a subsequent call to next would
be unaffected, and a subsequent call to previous would return the new
element. (This call increases by one the value that would be returned
by a call to nextIndex or previousIndex.)
see https://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html#add(E)
Inserts the specified element into the list (optional operation). The
element is inserted immediately before the element that would be
returned by next(), if any, and after the element that would be
returned by previous(), if any. (If the list contains no elements, the
new element becomes the sole element on the list.) The new element is
inserted before the implicit cursor: a subsequent call to next would
be unaffected, and a subsequent call to previous would return the new
element. (This call increases by one the value that would be returned
by a call to nextIndex or previousIndex.)
answered Nov 6 '18 at 0:30
leonardkraemerleonardkraemer
3,45911633
3,45911633
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53164140%2fwhy-is-my-iterator-not-iterating-over-the-list-correctly%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
I thought that the cursor was initially between the items: "Cowabunga" and "Ree" ... No, not until you call
next()
. The cursor starts before the first element. Read the documentation.– shmosel
Nov 6 '18 at 0:25
1
docs.oracle.com/javase/8/docs/api/java/util/ListIterator.html has a nice diagram showing the cursor position. As you can see, the first position is before the first element.
– JB Nizet
Nov 6 '18 at 0:25
To generalize and summarize rgettman's answer: don't change the length of an iterable object as you iterate over it--regardless of the programming language, exact data structure, or whether you're appending or removing elements. I just spent way more time than I care to admit trying to fix a bug in Python that was caused by me removing elements from a list as I iterated over it.
– Matthew Milone
Jan 2 at 7:00