原始类型/ T无法解析

时间:2021-04-02 20:51:42

I started working with generics for Java and seem to be missing a key component.

我开始使用Java的泛型,似乎缺少一个关键组件。

First off, I did some reading on raw type needing parameters and realize there is not much to do since they are generic but my problem is the interaction between BagInterface and LinkedBag:

首先,我对需要参数的原始类型进行了一些阅读,并意识到没有太多事情要做,因为它们是通用的,但我的问题是BagInterface和LinkedBag之间的交互:

package Chapter3;

public interface BagInterface<T> {

/** Gets the current number of entries in the bag.
* @return the integer number of entries in the bag. */
public int getCurrentSize();

/** Sees whether this bag is full.
*@return true if the bag is full, or false if not. */
public boolean isFull();

/** Sees whether the bag is empty.
*@return true if bag is empty, or false if not. */
public boolean isEmpty();

/** Adds new entry to this bag.
*@param newEntry the object to be added as a new entry
*@return if the addition was successful, or false if not. */
public boolean add(T newEntry);

/** Removes one unspecified entry from this bag, if possible.
*@return either the removed entry, if the removal was successful, or null. */
public T remove();

/** Removes one occurrence of a given entry from this bag.
*@param anEntry the entry to be removed
*@return true id the removal was successful, or false if not. */
public boolean removal(T anEntry);

/** Removes all entries from this bag. */
public void clear();

/** Counts the number of times a given entry appears in this bag.
*@param anEntry the entry to be counted
*@return the number of times anEntry appears in the bag. */
public int getFrequencyOf(T anEntry);

/** Tests whether this bag contains a given entry.
*@param anEntry the entry to locate
*@return true if this bag contains anEntry, or false if not. */
public boolean contains(T anEntry);

/**Retrieves all entries that are in this bag.
*@return a newly allocated array of all the entries in the bag */
public T[] toArray();
}

the two errors have to do with T not being resolved

这两个错误与T无法解决有关

package Chapter3;

public class LinkedBag  implements BagInterface { 

// reference to first node
private Node firstNode;
private int numberOfEntries;

// default constructor
public LinkedBag() {

firstNode = null;
numberOfEntries = 0;
}

// second constructor
(error occurs here) public LinkedBag(T[] item, int numberOfItems) {
this();
for(int index = 0; index < numberOfItems; index++)
add(item[index]);
}`

the other being being with get.data but i believe that also has to do with T not resolving

另一个是与get.data,但我相信这也与T没有解决

(error occurs here) result[index] = currentNode.getData();
index++;
currentNode = currentNode.getNextNode();
}// end while
return result;
}// end is full

I have the full .java files transcribed to note if more info is needed but I tried to keep it specific and concise.

如果需要更多信息,我会将完整的.java文件转录为注意,但我试图保持其具体和简洁。

1 个解决方案

#1


2  

LinkedBag in the code you shared, implements the raw BagInterface. If you want to refer to its type specification, you should add type argument to LinkedBag too, and have it refer to the BagInterface type somehow. E.g.:

您共享的代码中的LinkedBag实现了原始的BagInterface。如果你想引用它的类型规范,你也应该将类型参数添加到LinkedBag,并让它以某种方式引用BagInterface类型。例如。:

public class LinkedBag<T>  implements BagInterface<T> { 
// Here --------------^---------------------------^

#1


2  

LinkedBag in the code you shared, implements the raw BagInterface. If you want to refer to its type specification, you should add type argument to LinkedBag too, and have it refer to the BagInterface type somehow. E.g.:

您共享的代码中的LinkedBag实现了原始的BagInterface。如果你想引用它的类型规范,你也应该将类型参数添加到LinkedBag,并让它以某种方式引用BagInterface类型。例如。:

public class LinkedBag<T>  implements BagInterface<T> { 
// Here --------------^---------------------------^