d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > What Do These Do?
Add Reply New Topic New Poll
Member
Posts: 31,293
Joined: Mar 25 2009
Gold: 0.00
Feb 1 2019 11:19pm
What does Iterator and ListIterator do?


Also, have some question about my code.....:

Code
package com.company;

import javax.swing.text.html.HTMLDocument;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;

public class Demo {

public static void main(String[] args) {


LinkedList<String> placesToVisit = new LinkedList<String>();
placesToVisit.add("Sydney");
placesToVisit.add("Melbourne");
placesToVisit.add("Brisbane");
placesToVisit.add("Perth");
placesToVisit.add("Canberra");
placesToVisit.add("Adelaide");
placesToVisit.add("Darwin");

printList(placesToVisit);


placesToVisit.add(1, "Alice Springs");
printList(placesToVisit);

placesToVisit.remove(4);
printList(placesToVisit);



}





private static void printList(LinkedList<String> linkedList) {
Iterator<String> i = linkedList.iterator();
while(i.hasNext()) {
System.out.println("Now visiting " + i.next());
}
System.out.println("=================================================");
}




private static boolean addInOrder(LinkedList<String> linkedList, String newCity) {
ListIterator<String> stringListIterator = linkedList.listIterator();

while(stringListIterator.hasNext()) {
int comparison = stringListIterator.next().compareTo(newCity);
if(comparison == 0) {
// equal, do not add
System.out.println(newCity + " is already included as a destination");
return false;
} else if(comparison > 0) {
// new city should appear before this one
// Brisbane --> Adelaide
stringListIterator.previous();
stringListIterator.add(newCity);
return true;
} else if(comparison < 0) {
// move on to next city

}
}

stringListIterator.add(newCity);

}




}




wondering what this part does:

Code
ListIterator<String> stringListIterator = linkedList.listIterator();


Thanks guys!!

This post was edited by ferf on Feb 1 2019 11:21pm
Member
Posts: 12,703
Joined: May 17 2013
Gold: 12,935.00
Feb 2 2019 04:04am
You should learn to look up this stuff instead of asking here.

It doesn't make you learn at all, if your first inclination is to ask someone else instead of looking it up. You can get an explanation for that exact method by just googling it.
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Feb 2 2019 11:06am
Quote (Klexmoo @ Feb 2 2019 05:04am)
You should learn to look up this stuff instead of asking here.

It doesn't make you learn at all, if your first inclination is to ask someone else instead of looking it up. You can get an explanation for that exact method by just googling it.


This. They make javadocs for a reason. Familiarize yourself with them.
Google: java listiterator
1st Result (which you should look at because it answers your question): https://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll