gpt4 book ai didi

java - 使用列表迭代器

转载 作者:行者123 更新时间:2023-11-29 08:36:26 28 4
gpt4 key购买 nike

我有一项作业很难完成。这是一个由两部分组成的实验,演示了 LinkedList 和 Queue 类。我必须使用 offer() 方法和迭代器来完成我遇到问题的部分。我编写了一个名为 BasketBallPlayer 的类,并使用 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());

这是该片段中的整个类。

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 类。此类包含作业的第 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这样做。

此功能称为“快速失败”迭代器,您可以查阅该术语。

因此,从您实例化迭代器的那一刻起,直到您停止使用它的那一刻,您都不能修改创建迭代器的集合。

你能找出代码中你这样做的地方吗?

另外,关于你的教授说的话:要么你不明白你教授说的,要么他们说的是错的。提供在迭代时修改集合的方法的迭代器确实有效。例如,您可以执行 iterator.remove() 来删除当前元素。这是可行的,因为如果您通过迭代器修改集合,那么迭代器将负责,因此它可以采取任何必要的措施来防止数据损坏。不起作用的是,当迭代器在集合上处于 Activity 状态时,通过直接在集合本身上调用任何增变器方法来修改集合,因为在这种情况下,迭代器不知道集合正在被修改的事实集合被修改。迭代器稍后抛出 ConcurrentModificationException,当它被调用做任何事情时,它发现集合已同时被修改。

关于java - 使用列表迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43811808/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com