/*
* Iterator Example
* Example of deleting data called days, months, and days in a list
* There are hasnext, next, and remove in the literator. There's a method that does those functions.
*/
package iteator;
import java.util.ArrayList; import java.util.Iterator;
public class IteratorPractice { public static void main(String[] args) { ArrayList list = new ArrayList(); list.add("일"); list.add("월"); list.add("수");
Iterator iter = list.iterator();
while (iter.hasNext()==true) {
String day = (String) iter.next();
if(day=="number") {
iter.remove();
} } System.out.println("Day: "+ day);
}
System.out.println("=============================");
iter = list.iterator();
while (iter.hasNext() == true) {
String day = (String)iter.next();
System.out.println("Day: " + day);
}
}
}
Question: System.out.println("============================="); on the bottom line Why put iter = list.iterator(); again? Iterator = list.iterator(); on top. And if I have to write it, does it matter if both names are it?
java
I think it's good to make a habit of referring to docs for packages, classes, and functions that you usually see for the first time.
https://docs.oracle.com/javase/8/docs/api/index.html?java/util/ListIterator.html
In the case of the list.iterator() method, an ArrayList type Iterator object is created, and the itter.next() method keeps moving the index of the itteration.
In this case, if you don't recreate the indicator object as you asked, the index of the indicator object is already referring to the last index, so the repetition below becomes a meaningless repetition, so it regenerates the object.
© 2024 OneMinuteCode. All rights reserved.