我得到 2 找不到符号错误,它似乎不是类路径的结果。我也无法从 listinterface.java 代码中获取我的 .add 方法以在我的 pex6.java 中工作。我不知道是不是因为我使用整数作为类型并且 add 方法是无效的,但我不确定。我已经使用 java 大约 6 个月了,所以我对它还是很陌生。让我知道是否缺少某些东西。
public interface ListInterface<T>
{
/**
* Should return the number of elements contained within this list:
*/`enter code here`
int size ();
/**
* Should return true if this list contains a copy of the given object:
*
* Comparisons should be performed by calling the equals(...) method of
* each element, passing the given object as an argument
*/
boolean contains ( T theObject );
/**
* Should remove the first element found within this list that exists as a
* copy of the given object and return true if such an element was found:
*/
boolean remove ( T theObject );
/**
* Should return a reference to the first element found within this list
* that exists as a copy of the given object or null if no such element was
* found:
*/
Object get ( T element );
/**
* Should return a nicely formatted string representation of this list:
*/
String toString ();
/**
* Should print the contents of this list to the screen:
*/
void writeLinkedList ();
/**
* Should initialize this list for iteration (use of the getNext() method):
*/
void reset ();
/**
* Should return a reference to the element located at the iterator's
* current position and increment the iterator:
*
* If the iterator is currently pointing to the last element in this list,
* the iterator should be reset to point to the first element in this list.
*
* @Preconditions:
* This list is not empty.
* This list has been reset.
* This list has not been modified since the last reset.
*/
T getNext ();
/**
* Should insert the given object onto the front of this list:
*/
void add ( T theObject );
}
public class PEX6
{
public static void main (String[] theArgs)
{
ListInterface<String> list = new RefList<T>();
for (int i = 1; i <= 20; i++);
{
int Random = ( ( int ) ( Math.random() * 4 ) );
list.add(new Integer(Random));
list.writeLinkedList();
}
}
private static int CountValue(ListInterface<T> theList,int theValue)
{
theList.clear();
Integer nFound = 20;
return nFound;
}
}
错误:
C:\Users\Linville\Documents\Assignment 6\PEX6.java:16: error: cannot find symbol
private static int CountValue(ListInterface<T> theList,int theValue)
^
symbol: class T
location: class PEX6
C:\Users\Linville\Documents\Assignment 6\PEX6.java:5: error: cannot find symbol
ListInterface<String> list = new RefList<T>();
^
symbol: class T
location: class PEX6
C:\Users\Linville\Documents\Assignment 6\PEX6.java:11: error: method add in interface ListInterface<T> cannot be applied to given types;
list.add(new Integer(Random));
^
required: String
found: Integer
reason: actual argument Integer cannot be converted to String by method invocation conversion
where T is a type-variable:
T extends Object declared in interface ListInterface
3 errors
Tool completed with exit code 1
顺便说一句,PEX6.java 并没有以任何方式完成,但我想继续正确编译代码,这样我完成后就不会出现很多错误需要修复。