java - 这是我教科书上的示例代码。它不会编译

标签 java

当我运行教科书上的这段代码时。我收到以下错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    BagInterface cannot be resolved to a type
    ArrayBag cannot be resolved to a type
    ArrayBag cannot be resolved to a type
    at ArrayBagDemo1.main(ArrayBagDemo1.java:12)

这是未编译的代码:

/**
A test of the methods add, toArray, and isFull, as defined
in the first draft of the class ArrayBag.
@author Frank M. Carrano
*/
public class ArrayBagDemo1
{
public static void main(String[] args)
{
   // a bag that is not full
   BagInterface<String> aBag = new ArrayBag<String>();
   // tests on an empty bag
   testIsFull(aBag, false);
   // adding strings
   String[] contentsOfBag1 = {"A", "A", "B", "A", "C", "A"};
   testAdd(aBag, contentsOfBag1);
   testIsFull(aBag, false);
   // a bag that will be full
   aBag = new ArrayBag<String>(7);
   System.out.println("\nA new empty bag:");
   // tests on an empty bag
   testIsFull(aBag, false);
   // adding strings
   String[] contentsOfBag2 = {"A", "B", "A", "C", "B", "C", "D"};
   testAdd(aBag, contentsOfBag2);
   testIsFull(aBag, true);
} // end main
// Tests the method add.
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
// Tests the method isFull.
// correctResult indicates what isFull should return.
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
// Tests the method toArray while displaying the bag.
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
} // end ArrayBagDemo1

如何解决此问题以及该错误意味着什么?

该代码是对 add、toArray 和 isFull 方法的测试(如定义) 在 ArrayBag 类的初稿中。

最佳答案

我在网上找不到 BagInterface 接口(interface)或 ArrayBag 类,所以我假设它们在你书中的其他地方。我冒昧地编写了该接口(interface)和类的外观(请注意,这可能不适用于使用此接口(interface)和类的其他示例)。您可以将以下内容粘贴到 java 文件的底部(任何其他类之外)。

最后一点:将对象数组转换为泛型类型是不安全的。编译时您会看到一条警告。

interface BagInterface<T>{  
    public boolean isFull();
    public T[] toArray();
    public void add(T object);
}

class ArrayBag<T> implements BagInterface<T>{
    T[] bag;

    public ArrayBag()
    {
        bag = (T[]) new Object[10]; //some arbitrary default size
    }

    public ArrayBag(int size)
    {
        bag = (T[]) new Object[size];
    }

    public boolean isFull(){
        //Check that every slot is occupied
        for(int i = 0; i < bag.length; i++)
        {
            if(bag[i] == null)
                return false;
        }

        return true;
    }

    public T[] toArray(){       
        return bag;
    }

    public void add(T object){
        //Find first empty slot to add item
        for(int i = 0; i < bag.length; i++)
        {           
            if(bag[i] == null)
            {
                bag[i] = object;
                return;
            }
        }

        //otherwise bag is full
    }
}

关于java - 这是我教科书上的示例代码。它不会编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40923030/

相关文章:

java - 使用 if-else 防止 JTextfield 中的字符串为 null

java - 在 Java 中复制大型数组的最有效方法

java - 如何在java中实现将泛型类型数组转换为泛型类型二维数组的函数?

c# - 除以零 : int vs. float

java - 无法实例化 fragment 确保类名存在

java - Android 选项卡不工作

java - 如何按 querydsl 别名排序

java - 过滤文件名

java - 跳过并选择 java arraylist 的选项?

java - 解决方案中的重复规划实体