gpt4 book ai didi

java - 从链表中删除未指定的项目

转载 作者:行者123 更新时间:2023-11-29 04:41:01 26 4
gpt4 key购买 nike

我正在尝试从链表或可以这么说的“包”中删除一个随机节点。请引用我的方法“T remove()”。如果不抛出 NullPointerException 错误,我无法让它删除节点。

首先,如果列表中没有任何内容开始,我让方法返回 NULL。如果一个节点存在,那么我循环遍历 numberofNodes,找到一个随机节点并尝试删除该节点。我做了一个节点,临时指向下一个节点的数据,让前一个节点指向currentNode之后的节点,就好像它不存在一样。

我的逻辑错了吗?

private class Node{

private T entry;
private Node next;

private Node(T entryPortion)
{
this(entryPortion, null);
}

private Node(T entryPortion, Node nextNode)
{
entry = entryPortion;
next = nextNode;
}

}

private Node node1;
private Node lastNode;
private int numItems;

public LinkedBag() //Establishes an empty bag
{
node1 = null;
numItems = 0;
}

@Override
public int getCurrentSize()
{
// Gets Size of Bag
return numItems;
}

@Override
public boolean isFull()
{
//Checks to see if bag is full, however since it is linked list there is no specified max size. Although maximum memory can be reached
return true;
}

@Override
public boolean isEmpty() {
// Checks to see if bag is empty
return node1 == null;
}

@Override
public boolean add(T newItem) {
// Adds something to the bag
Node newNode = new Node(newItem);
newNode.next = node1;

node1 = newNode;
numItems++;
return true;
}

@Override
public T remove() {
// Removes random item from bag

if(node1.equals(null))
{
return null;
}

else
{
int randItem = new Random().nextInt(numItems);
Node currentNode = node1;
Node previousNode = node1;
for (int i = 0; i < randItem; i++)
{
previousNode = currentNode;
currentNode = currentNode.next;
}
previousNode.next = currentNode.next;
currentNode.next = null;
numItems--;

return null;

}

/*if (numItems == 0)

return null;
}
else
{
Node temp = node1;
node1 = node1.next;
numItems--;
//if(node1 == null)
//lastNode = null;
return temp.entry;

}*/

}

@Override
public boolean remove(T anItem) {
// TODO Auto-generated method stub
return false;
}

@Override
public void clear() {


}

@Override
public int getFrequencyOf(T anItem) {
// TODO Auto-generated method stub
return 0;
}

@Override
public boolean contains(T anItem) {
// TODO Auto-generated method stub
return false;
}

@Override
public T[] toArray() {
// Converts items in linked list to an array for easy displaying
@SuppressWarnings("unchecked")
T[] result = (T[])new Object [numItems];

int i = 0;
Node currentNode = node1;
while((i<numItems)&&(currentNode != null))
{
result[i] = currentNode.entry;
i++;
currentNode = currentNode.next;
}
return result;
}




}

这是我使用的测试程序。 “testRemove 是从我的构造函数类中调用我的‘remove()’方法的方法

public class LinkedBagTest {

public static void main(String[] args) {

System.out.println ("Creating an empty bag.");
BagInterface < String > aBag = new LinkedBag < String > ();
displayBag (aBag);
testNumItems(aBag);
testRemove(aBag);
String [] contentsOfBag = {"A", "D", "B", "A", "C", "A", "D"};
testAdd (aBag, contentsOfBag);
testNumItems(aBag);
testRemove(aBag);
displayBag (aBag);
testRemove(aBag);
displayBag (aBag);
//testIsFull(aBag, false);


}
private static void testAdd (BagInterface < String > aBag,
String [] content)
{
System.out.print ("Adding to the bag: ");
for (int index = 0 ; index < content.length ; index++)
{
aBag.add (content [index]);
System.out.print (content [index] + " ");
} // end for
System.out.println ();
displayBag (aBag);
} // end testAdd

private static void displayBag (BagInterface < String > aBag)
{
System.out.println ("The bag contains the following string(s):");
Object [] bagArray = aBag.toArray ();
for (int index = 0 ; index < bagArray.length ; index++)
{
System.out.print (bagArray [index] + " ");
} // end for
System.out.println ();
} // end displayBag

private static void testIsFull (BagInterface < String > aBag,
boolean correctResult)
{
System.out.print ("\nTesting the method isFull with ");
if (correctResult)
System.out.println ("a full bag:");
else
System.out.println ("a bag that is not full:");
System.out.print ("isFull finds the bag ");
if (correctResult && aBag.isFull ())
System.out.println ("full: OK.");
else if (correctResult)
System.out.println ("not full, but it is full: ERROR.");
else if (!correctResult && aBag.isFull ())
System.out.println ("full, but it is not full: ERROR.");
else
System.out.println ("not full: OK.");
} // end testIsFull are here.

private static void testNumItems (BagInterface <String> aBag)
{
int items = aBag.getCurrentSize();
System.out.println("There are " + items + " items in the bag");
}

private static void testRemove (BagInterface <String> aBag)
{
aBag.remove();
System.out.println("An Item was removed");

testNumItems(aBag);



}
}

最佳答案

您的 null 检查应采用以下格式:node1 == null。当您将其检查为 node1.equals(null),而 node1 实际上为 null 时,您正在尝试调用一个对象的方法,该对象为 null 并且自然会抛出 NullPointerException。

关于java - 从链表中删除未指定的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39195256/

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