我正在尝试使我的程序正常运行,以便每当选择一个类时,它将其删除,它可以是随机的,也可以是有序的(此时我只是想使其正常工作)。但是,当我运行当前程序时,它只会不断循环而不删除类。任何帮助表示赞赏。无论我做什么,我似乎都无法使程序删除任何类并输出它们。我不知道为什么这些班级没有从清单中删除。
package labs.lab2;
public class TestDriver {
public static void main(String[] args)
{
ArrayStringBag bag = new ArrayStringBag("My bag of courses", 6);
bag.insert("CIS110 Object-Oriented Programming");
bag.insert("CS120 Data Structures and Algorithms");
bag.insert("CIS210 Database Design and Implementation");
bag.insert("CS340 Advanced Techniques in Application Development");
bag.insert("CIS345 Introduction to XML");
bag.insert("CIS345/WDMD345 Hot Topics in .NET Programming");
System.out.println(bag);
while(!bag.isEmpty())
{
System.out.println("Removing course: "+bag.remove());
if(bag.isEmpty())
System.out.println("My bag of courses is empty!");
else
System.out.println(bag);
}
}
package labs.lab2;
public interface StringBagInterface
{
public void insert(String element);
// Precondition: This StringBag is not full.
//
// Places element into this StringBag.
public boolean isFull();
// Returns true if this StringBag is full, otherwise returns false.
public boolean isEmpty();
// Returns true if this StringBag is empty, otherwise returns false.
public int size();
// Returns the number of Strings in this StringBag.
public String remove();
// Precondition: The StringBag is not empty.
//
// Removes a random string from this StringBag; then fills in the hole
// with the last element in the bag.
public void clear();
// Makes this StringBag empty.
public String getName();
// Returns the name of this StringBag.
public String toString();
// Returns a nicely formatted string representing this StringBag.
}
package labs.lab2;
public class ArrayStringBag implements StringBagInterface
{
protected String name;
// name of this StringLog
protected String[] log;
// array that holds strings
protected int lastIndex = -1;
// index of last string in array
public ArrayStringBag(String name, int maxSize)
// Precondition: maxSize > 0
//
// Instantiates and returns a reference to an empty ArrayStringLog
// object with name "name" and room for maxSize strings.
{
log = new String[maxSize];
this.name = name;
}
public ArrayStringBag(String name)
// Instantiates and returns a reference to an empty ArrayStringLog
// object with name "name" and room for 100 strings.
{
log = new String[100];
this.name = name;
}
public void insert(String element)
// Precondition: This StringLog is not full.
//
// Places element into this StringLog.
{
lastIndex++;
log[lastIndex] = element;
}
public boolean isFull()
// Returns true if this StringLog is full, otherwise returns false.
{
if (lastIndex == (log.length - 1))
return true;
else
return false;
}
public int size()
// Returns the number of Strings in this StringLog.
{
return (lastIndex + 1);
}
public boolean contains(String element)
// Returns true if element is in this StringLog,
// otherwise returns false.
// Ignores case differences when doing string comparison.
{
int location = 0;
while (location <= lastIndex)
{
if (element.equalsIgnoreCase(log[location])) // if they match
return true;
else
location++;
}
return false;
}
public void clear()
// Makes this StringLog empty.
{
for (int i = 0; i <= lastIndex; i++)
log[i] = null;
lastIndex = -1;
}
public String getName()
// Returns the name of this StringLog.
{
return name;
}
public String toString()
// Returns a nicely formatted string representing this StringLog.
{
String logString = "Log: " + name + "\n\n";
for (int i = 0; i <= lastIndex; i++)
logString = logString + (i+1) + ". " + log[i] + "\n";
return logString;
}
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
public String remove() {
// TODO Auto-generated method stub
return null;
}
}
答案 0 :(得分:0)
您需要实现这些方法。
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
public String remove() {
// TODO Auto-generated method stub
return null;
}