java - LinkedList 头尾问题
问题描述
我试图弄清楚为什么 InventoryList.java 中的某些循环不起作用,例如所有的删除方法。不工作,我的意思是我实现的循环不起作用,我正在寻找如何修复它的解决方案。另一个不太重要的问题是 InventoryList.java 中的 toString() 设置不正确,但我想知道为什么我的循环不起作用。有关更多信息,以下是我用来实现这些类的文档: http: //cs300-www.cs.wisc.edu/wp/wp-content/uploads/2020/12/p7/doc/package-summary .html
感谢您阅读
/**
* This class models a Box used for inventory
*
* @author mouna
*
*/
public class Box {
private static int nextInventoryNumber = 1; // generator of unique inventory numbers
private Color color; // color of this box
private final int INVENTORY_NUMBER; // unique inventory number of this box
/**
* Creates a new Box and initializes its instance fields color and unique inventory number
*
* @param color color to be assigned of this box. It can be any of the constant color values
* defined in the enum Color: Color.BLUE, Color.BROWN, or Color.YELLOW
*/
public Box(Color color) {
this.color = color;
this.INVENTORY_NUMBER = nextInventoryNumber++;
}
/**
* Returns the color of this box
*
* @return the color of this box
*/
public Color getColor() {
return color;
}
/**
* returns the inventory number of this box
*
* @return the unique inventory number of this box
*/
public int getInventoryNumber() {
return this.INVENTORY_NUMBER;
}
/**
* Returns a String representation of this box in the format "color INVENTORY_NUMBER"
*
* @return a String representation of this box
*/
@Override
public String toString() {
return this.color + " " + this.INVENTORY_NUMBER;
}
/**
* This method sets the nextInventoryNumber to 1. This method must be used in your tester methods
* only.
*/
public static void restartNextInventoryNumber() {
nextInventoryNumber = 1;
}
}
/**
* This Enumeration groups the name of constants representing the three colors BLUE, BROWN, and YELLOW
*
*/
public enum Color{
BLUE, BROWN, YELLOW;
}
public class LinkedBox {
private Box box;
private LinkedBox next;
//constructors
public LinkedBox(Box box) {
this.box = box;
}
public LinkedBox(Box box, LinkedBox next) {
this.box = box;
this.next = next;
}
//getters
public Box getBox() {
return box;
}
public LinkedBox getNext() {
return next;
}
//setters
public void setNext(LinkedBox next) {
this.next = next;
}
public String toString() {
if (next == null) return box.toString() + " -> END"; //one arg
return box.toString() + " -> " + this.next; //two+ args
}
}
public class InventoryList {
private LinkedBox head;
private LinkedBox tail;
private int size;
private int yellowCount;
private int blueCount;
private int brownCount;
//TODO remove these 2 methods later
public LinkedBox getHead() {
return this.head;
}
public LinkedBox getTail() {
return this.tail;
}
//getters
public int getBlueCount() {
return this.blueCount;
}
public int getBrownCount() {
return this.brownCount;
}
public int getYellowCount() {
return this.yellowCount;
}
public Box get(int index) {
int count = 0;
Box box = new Box(Color.BLUE);
while (head != null) {
if (count == index) box = this.head.getBox();
head = head.getNext();
count++;
}
return box;
}
public int size() {
return this.size;
}
//setters
public void addBlue(Box blueBox) {
if (head.getBox().getColor() != Color.YELLOW || tail.getBox().getColor() == Color.BROWN) head = new LinkedBox(blueBox, head); this.blueCount++; this.size++;
}
public void addBrown(Box brownBox) {
tail = new LinkedBox(brownBox, tail);
this.brownCount++; this.size++;
}
public void addYellow(Box yellowBox) {
head = new LinkedBox(yellowBox, head);
this.yellowCount++; this.size++;
}
//removers
public Box removeBox(int inventoryNumber) {
Box box = new Box(Color.BLUE);
int index = 0;
while (head != null) {
if (inventoryNumber == index) {
box = head.getBox();
head = null;
}
index++;
head = head.getNext();
}
return box;
}
public Box removeBrown() {
Box box;
if (tail.getBox().getColor() == Color.BROWN) {
box = tail.getBox();
tail = null;
return box;
}
return new Box(Color.BROWN);
}
public Box removeYellow() {
Box box;
if (head.getBox().getColor() == Color.YELLOW) {
box = head.getBox();
head = null;
return box;
}
return new Box(Color.YELLOW);
}
public void clear() {
while (head != null) {
head = null;
head = head.getNext();
}
}
//miscellaneous
public boolean isEmpty() {
if (head == null && tail == null) return true;
return false;
}
public String toString() {
if (head == null || tail == null) return "";
return head+" "+tail+" <-TO STRING";
}
}
解决方案
如果您没有使用任何集成开发环境 (IDE),请使用一个,例如Eclipse。要了解代码是如何工作的,最好学习如何调试。
您的代码有多个问题。我会给你一些提示,剩下的就交给你了。首先修复toString()
方法,以便您知道发生了什么。
在LinkedBox#toString()
方法中,代码进行递归并打印整个链表。因为this.next.toString()
被递归调用。将最后一行更改为:
return box.toString() + " -> " + this.next.getBox(); //
然后InventoryList#toString()
像这样实现:
public String toString() {
if (head == null || tail == null) return "";
LinkedBox temporaryLinkedBox = head; //<-- use temporary variable
StringBuilder result = new StringBuilder();
while(temporaryLinkedBox!=null) {
result.append(temporaryLinkedBox.toString()).append(" | ");
temporaryLinkedBox = temporaryLinkedBox.getNext();
}
result.append(" <-TO STRING");
return result.toString();
}
请注意我们如何不使用head
迭代列表。我们正在使用临时变量temporaryLinedBox
进行迭代。
同样的情况,InventoryList#get
andInventoryList#removeBox
和 get 方法不使用 head 来迭代列表。使用临时变量:
public Box removeBox(int inventoryNumber) {
Box box = null;
int index = 0;
LinkedBox temporaryLinkedBox = head;
while (temporaryLinkedBox != null) {
if (inventoryNumber == index) {
box = temporaryLinkedBox.getBox();
break; //<-- use 'break' to exit loop
}
index++;
temporaryLinkedBox = temporaryLinkedBox.getNext();
}
return box;
}
注意:这只是一个示例代码,如果有任何错误,您必须调试和修复错误。
推荐阅读
- c++ - G++ 编译器找不到 iostream 所需的标头
- json - 在同一文件夹级别的 JSON 文件上导入错误 - 打字稿
- javascript - 在 ASP.Net MVC 5 中将 javascript 数组从视图传递到控制器
- python - 视图中的 Django 模型数据作为变量
- oracle - OracleDB:如何知道一个表行事务的 CSN?
- javascript - 根据选择的值在数据库中获取不同的值
- c# - BizTalk - 使用具有多个命名空间的 xpath 提取元素值
- laravel - Laravel - BelongsToMany 仅检索当前相关记录
- powershell - ADInvalidCredentialException 与 Exchange 2010 的 PowerShell 远程管理
- python - 获取特定条目的所有列