使用列表迭代器
Using a ListIterator
我有一项作业很难完成。这是一个由两部分组成的实验,演示了 LinkedList 和 Queue classes。我必须使用 offer() 方法和迭代器来完成我遇到问题的部分。我编写了一个名为 BasketBallPlayer 的 class,并使用 add 添加了该类型的几个对象到 linkedList。然后我使用迭代器列出这些对象。但是,当我使用 offer() 方法时,我无法使用我的代码列出对象。
这是抛出并发修改异常的片段
LinkedList<BasketBallPlayer> list =
new LinkedList<BasketBallPlayer>();
Iterator listIterator = list.iterator();
list.offer(kyrie);
list.offer(kat) ;
list.offer(julius);
list.offer(kawhi) ;
list.offer(devin);
while(listIterator.hasNext())
System.out.println(listIterator.next());
这是该片段的完整 class。
import java.util.Iterator;
public class Queue {
public static void main (String[] args)
{
BasketBallPlayer kyrie = new BasketBallPlayer(
"Kyrie Irving", 1, "Cavaliers");
BasketBallPlayer kat = new BasketBallPlayer(
"Karl Anthony Towns", 5, "Timberwolves");
BasketBallPlayer julius = new BasketBallPlayer(
"Julius Randle", "Power Forward", "Lakers");
BasketBallPlayer kawhi = new BasketBallPlayer(
"Kawhi Leanord", "Small Forward", "Spurs");
BasketBallPlayer devin = new BasketBallPlayer(
"Devin Booker", "Shooting Guard", "Suns");
System.out.println(kyrie);
LinkedList<BasketBallPlayer> list =
new LinkedList<BasketBallPlayer>();
Iterator listIterator = list.iterator();
list.offer(kyrie);
list.offer(kat) ;
list.offer(julius);
list.offer(kawhi) ;
list.offer(devin);
Iterator iterator = list.iterator();
while(iterator.hasNext())
{
Object player = listIterator.next();
System.out.println(player);
}
System.out.println("Which player is first?");
System.out.println(list.peek());
for(Object player: list)
{
System.out.println(list.getFirst());
list.poll();
}
}
}
最后是 BasketballPlayer class。 class 包含作业的第 1 部分。这里我演示了 linkedList 方法。这部分有效。我不确定为什么第 1 部分中的迭代器没有抛出异常,而第 2 部分中的迭代器尽管使用几乎完全相同。谁能教我如何改正错误?
import java.util.Locale;
import java.util.LinkedList;
import java.util.ListIterator;
public class BasketBallPlayer
{
private String name = "" , position = ""; String team = "";
private int positionNumber = 0;
public BasketBallPlayer()
{
name = "noName";
position = "noPosition";
team = "noTeam";
positionNumber = 0;
}
public BasketBallPlayer(String playersName, int thePositionNumber,
String theTeam)
{
setName(playersName); setPositionNumber(thePositionNumber);
setTeam(theTeam);
}
public BasketBallPlayer(String playersName, String playersPosition,
String theTeam)
{
setName(playersName);
setPosition(playersPosition);
setTeam(theTeam
);
}
public void setName(String theName)
{
this.name = theName;
}
public void setTeam(String theTeam)
{
this.team = theTeam;
}
public void setPosition(String playerPosition)
{
this.position = playerPosition;
if(playerPosition.contains("oint"))
this.positionNumber = 1;
else if(playerPosition.contains("hoot"))
this.positionNumber = 2;
else if(playerPosition.contains("mall"))
this.positionNumber = 3;
else if(playerPosition.contains("ower"))
this.positionNumber = 4;
else if(playerPosition.contains("enter"))
this.positionNumber = 5;
}
public void setPositionNumber(int thePositionNumber)
{
this.positionNumber = thePositionNumber;
switch(thePositionNumber){
case 1: setPosition("Point Guard");
break;
case 2: setPosition("Shooting Guard");
break;
case 3: setPosition("Small Forward");
break;
case 4: setPosition("Power Forward");
break;
case 5: setPosition("Center");
break;
}
}
public String getName()
{
return name;
}
public String getPosition()
{
return position;
}
public int getPositionNumber()
{
return positionNumber;
}
public String getTeam()
{
return team;
}
public boolean equals(Object other)
{
BasketBallPlayer objectToCompare;
if(other != null && other.getClass() == getClass())
objectToCompare = (BasketBallPlayer) other;
else
return false;
return(
(getPositionNumber() == objectToCompare.getPositionNumber()) &&
(getName() == objectToCompare.getName()) &&
getTeam() == objectToCompare.getTeam()) ;
}
public String toString()
{
if(getTeam().equals("Retired"))
return getName() + " is retired.";
else
return getName()+ " plays for the " + getTeam();
}
public static void main (String[] args)
{
// five basketball player objects
BasketBallPlayer kobe = new BasketBallPlayer("Kobe Bryant",
"Shooting Guard", "Retired");
BasketBallPlayer ben = new BasketBallPlayer(
"Ben Wallace", 5, "Retired");
BasketBallPlayer otto = new BasketBallPlayer("Otto Porter", 3,
"Wizards");
BasketBallPlayer andre = new BasketBallPlayer("Andre Drummond",
"Center", "Pistons");
BasketBallPlayer thomas = new BasketBallPlayer("Isaiah Thomas",
1, "Celtics");
BasketBallPlayer isaiah = new BasketBallPlayer("Isaiah Thomas",
"Point Guard", "Pistons");
// initialize LinkedList and add three players
LinkedList list = new LinkedList();
list.add(kobe); list.add(otto);
list.add(thomas);
// display the first one
System.out.println("First player on the list");
System.out.println(list.peek());
System.out.println();
System.out.println(kobe.getName() +
" is retired so let's remove him.");
// remove object at index 0
list.remove(0);
System.out.println();
// add andre to the top of the list
list.addFirst(andre);
// add ben to the end
list.addLast(ben);
System.out.println("New first player on the list");
System.out.println(list.peek());
// first create an Object[] which acts as a BasketBallPlayer[]
System.out.println(
"Any other retired players? Printing entire list:");
// using toArray()
Object[] other = list.toArray();
for(Object player: other)
// display each player in the array
System.out.println(player);
// demonstrate contains()
if(list.contains(ben))
list.remove(ben);
System.out.println();
System.out.println("Let's remove the retired player");
System.out.println("Is " + ben.getName() +
" still on the list?");
if(list.contains(ben))
System.out.println(ben.getName() + " is still on the list");
else
{
System.out.println(ben.getName() + " is not on the list");
System.out.println("How many players after removing? "
+ list.size());
}
System.out.println();
System.out.println(otto.getName() + " is " +
// demonstrate indexOf()
(list.indexOf(otto) + 1) + " on the list");
// create an iterator
ListIterator listIterator = list.listIterator();
System.out.println("Printing list using iterator:");
// print out the list using the iterator
while(listIterator.hasNext())
{
Object player = listIterator.next();
System.out.println(player);
}
}
java(以及 C#)中标准集合的一个问题是您可能无法在迭代它们时修改它们。
如果没有采取额外的步骤,如果你运气不好,你可能不会出现异常,只是程序行为不稳定。为了解决这个问题,java 实际上采取了一个额外的步骤:迭代器实际上检查以确保您在迭代时没有修改集合,如果您这样做,则在之后尽快抛出一个 ConcurrentModificationException
你这样做。
此功能称为 "fail-fast" 迭代器,这是一个您可以查找的术语。
因此,从您实例化迭代器的那一刻起,直到您停止使用它的那一刻,您都不能修改创建迭代器的集合。
你能找出代码中你这样做的地方吗?
还有,关于你教授说的话:要么你没听懂你教授说的,要么他们说的不对。提供在迭代时修改集合的方法的迭代器确实有效。例如,您可以执行 iterator.remove()
来删除当前元素。这是可行的,因为如果您通过迭代器修改集合,那么迭代器将负责,因此它可以采取任何必要的措施来防止数据损坏。不起作用的是,当迭代器在集合上处于活动状态时,通过直接在集合本身上调用任何增变器方法来修改集合,因为在这种情况下,迭代器不知道集合正在被修改的事实集合被修改。迭代器稍后抛出一个 ConcurrentModificationException
,当它被调用做任何事情时,它发现集合已同时被修改。
我有一项作业很难完成。这是一个由两部分组成的实验,演示了 LinkedList 和 Queue classes。我必须使用 offer() 方法和迭代器来完成我遇到问题的部分。我编写了一个名为 BasketBallPlayer 的 class,并使用 add 添加了该类型的几个对象到 linkedList。然后我使用迭代器列出这些对象。但是,当我使用 offer() 方法时,我无法使用我的代码列出对象。 这是抛出并发修改异常的片段
LinkedList<BasketBallPlayer> list =
new LinkedList<BasketBallPlayer>();
Iterator listIterator = list.iterator();
list.offer(kyrie);
list.offer(kat) ;
list.offer(julius);
list.offer(kawhi) ;
list.offer(devin);
while(listIterator.hasNext())
System.out.println(listIterator.next());
这是该片段的完整 class。
import java.util.Iterator;
public class Queue {
public static void main (String[] args)
{
BasketBallPlayer kyrie = new BasketBallPlayer(
"Kyrie Irving", 1, "Cavaliers");
BasketBallPlayer kat = new BasketBallPlayer(
"Karl Anthony Towns", 5, "Timberwolves");
BasketBallPlayer julius = new BasketBallPlayer(
"Julius Randle", "Power Forward", "Lakers");
BasketBallPlayer kawhi = new BasketBallPlayer(
"Kawhi Leanord", "Small Forward", "Spurs");
BasketBallPlayer devin = new BasketBallPlayer(
"Devin Booker", "Shooting Guard", "Suns");
System.out.println(kyrie);
LinkedList<BasketBallPlayer> list =
new LinkedList<BasketBallPlayer>();
Iterator listIterator = list.iterator();
list.offer(kyrie);
list.offer(kat) ;
list.offer(julius);
list.offer(kawhi) ;
list.offer(devin);
Iterator iterator = list.iterator();
while(iterator.hasNext())
{
Object player = listIterator.next();
System.out.println(player);
}
System.out.println("Which player is first?");
System.out.println(list.peek());
for(Object player: list)
{
System.out.println(list.getFirst());
list.poll();
}
}
}
最后是 BasketballPlayer class。 class 包含作业的第 1 部分。这里我演示了 linkedList 方法。这部分有效。我不确定为什么第 1 部分中的迭代器没有抛出异常,而第 2 部分中的迭代器尽管使用几乎完全相同。谁能教我如何改正错误?
import java.util.Locale;
import java.util.LinkedList;
import java.util.ListIterator;
public class BasketBallPlayer
{
private String name = "" , position = ""; String team = "";
private int positionNumber = 0;
public BasketBallPlayer()
{
name = "noName";
position = "noPosition";
team = "noTeam";
positionNumber = 0;
}
public BasketBallPlayer(String playersName, int thePositionNumber,
String theTeam)
{
setName(playersName); setPositionNumber(thePositionNumber);
setTeam(theTeam);
}
public BasketBallPlayer(String playersName, String playersPosition,
String theTeam)
{
setName(playersName);
setPosition(playersPosition);
setTeam(theTeam
);
}
public void setName(String theName)
{
this.name = theName;
}
public void setTeam(String theTeam)
{
this.team = theTeam;
}
public void setPosition(String playerPosition)
{
this.position = playerPosition;
if(playerPosition.contains("oint"))
this.positionNumber = 1;
else if(playerPosition.contains("hoot"))
this.positionNumber = 2;
else if(playerPosition.contains("mall"))
this.positionNumber = 3;
else if(playerPosition.contains("ower"))
this.positionNumber = 4;
else if(playerPosition.contains("enter"))
this.positionNumber = 5;
}
public void setPositionNumber(int thePositionNumber)
{
this.positionNumber = thePositionNumber;
switch(thePositionNumber){
case 1: setPosition("Point Guard");
break;
case 2: setPosition("Shooting Guard");
break;
case 3: setPosition("Small Forward");
break;
case 4: setPosition("Power Forward");
break;
case 5: setPosition("Center");
break;
}
}
public String getName()
{
return name;
}
public String getPosition()
{
return position;
}
public int getPositionNumber()
{
return positionNumber;
}
public String getTeam()
{
return team;
}
public boolean equals(Object other)
{
BasketBallPlayer objectToCompare;
if(other != null && other.getClass() == getClass())
objectToCompare = (BasketBallPlayer) other;
else
return false;
return(
(getPositionNumber() == objectToCompare.getPositionNumber()) &&
(getName() == objectToCompare.getName()) &&
getTeam() == objectToCompare.getTeam()) ;
}
public String toString()
{
if(getTeam().equals("Retired"))
return getName() + " is retired.";
else
return getName()+ " plays for the " + getTeam();
}
public static void main (String[] args)
{
// five basketball player objects
BasketBallPlayer kobe = new BasketBallPlayer("Kobe Bryant",
"Shooting Guard", "Retired");
BasketBallPlayer ben = new BasketBallPlayer(
"Ben Wallace", 5, "Retired");
BasketBallPlayer otto = new BasketBallPlayer("Otto Porter", 3,
"Wizards");
BasketBallPlayer andre = new BasketBallPlayer("Andre Drummond",
"Center", "Pistons");
BasketBallPlayer thomas = new BasketBallPlayer("Isaiah Thomas",
1, "Celtics");
BasketBallPlayer isaiah = new BasketBallPlayer("Isaiah Thomas",
"Point Guard", "Pistons");
// initialize LinkedList and add three players
LinkedList list = new LinkedList();
list.add(kobe); list.add(otto);
list.add(thomas);
// display the first one
System.out.println("First player on the list");
System.out.println(list.peek());
System.out.println();
System.out.println(kobe.getName() +
" is retired so let's remove him.");
// remove object at index 0
list.remove(0);
System.out.println();
// add andre to the top of the list
list.addFirst(andre);
// add ben to the end
list.addLast(ben);
System.out.println("New first player on the list");
System.out.println(list.peek());
// first create an Object[] which acts as a BasketBallPlayer[]
System.out.println(
"Any other retired players? Printing entire list:");
// using toArray()
Object[] other = list.toArray();
for(Object player: other)
// display each player in the array
System.out.println(player);
// demonstrate contains()
if(list.contains(ben))
list.remove(ben);
System.out.println();
System.out.println("Let's remove the retired player");
System.out.println("Is " + ben.getName() +
" still on the list?");
if(list.contains(ben))
System.out.println(ben.getName() + " is still on the list");
else
{
System.out.println(ben.getName() + " is not on the list");
System.out.println("How many players after removing? "
+ list.size());
}
System.out.println();
System.out.println(otto.getName() + " is " +
// demonstrate indexOf()
(list.indexOf(otto) + 1) + " on the list");
// create an iterator
ListIterator listIterator = list.listIterator();
System.out.println("Printing list using iterator:");
// print out the list using the iterator
while(listIterator.hasNext())
{
Object player = listIterator.next();
System.out.println(player);
}
}
java(以及 C#)中标准集合的一个问题是您可能无法在迭代它们时修改它们。
如果没有采取额外的步骤,如果你运气不好,你可能不会出现异常,只是程序行为不稳定。为了解决这个问题,java 实际上采取了一个额外的步骤:迭代器实际上检查以确保您在迭代时没有修改集合,如果您这样做,则在之后尽快抛出一个 ConcurrentModificationException
你这样做。
此功能称为 "fail-fast" 迭代器,这是一个您可以查找的术语。
因此,从您实例化迭代器的那一刻起,直到您停止使用它的那一刻,您都不能修改创建迭代器的集合。
你能找出代码中你这样做的地方吗?
还有,关于你教授说的话:要么你没听懂你教授说的,要么他们说的不对。提供在迭代时修改集合的方法的迭代器确实有效。例如,您可以执行 iterator.remove()
来删除当前元素。这是可行的,因为如果您通过迭代器修改集合,那么迭代器将负责,因此它可以采取任何必要的措施来防止数据损坏。不起作用的是,当迭代器在集合上处于活动状态时,通过直接在集合本身上调用任何增变器方法来修改集合,因为在这种情况下,迭代器不知道集合正在被修改的事实集合被修改。迭代器稍后抛出一个 ConcurrentModificationException
,当它被调用做任何事情时,它发现集合已同时被修改。