【问题标题】:Having a cannot find symbol error and one other有一个找不到符号错误和另一个
【发布时间】:2013-04-12 18:05:02
【问题描述】:

我得到 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 无论如何都没有完成,但我想继续正确编译代码,这样我完成后就不会出现很多错误需要修复.

【问题讨论】:

  • 提示:ListInterface&lt;String&gt; list = new RefList&lt;T&gt;(); 应该是 ListInterface&lt;String&gt; list = new RefList&lt;String&gt;();
  • 到目前为止你为 PEX6.java 写了什么?
  • 感谢您对 add 方法的帮助。现在唯一剩下的是找不到符号错误。为了回答你问 qparyani 的问题,我已经为 pex6 类编写了所有代码。我还没写完,因为我要写countvalue方法。
  • 好的,我已解决所有问题,谢谢您的帮助。

标签: java list methods


【解决方案1】:

PEX6 中没有T,但你指的是它:

ListInterface<String> list = new RefList<T>();
                                         ^ here

private static int CountValue(ListInterface<T> theList,int theValue)
                                            ^ and here

您需要将T 替换为StringInteger。无法判断两者中的哪一个是有意的:您将 list 声明为 ListInterface&lt;String&gt;,然后继续将 Integers 添加到其中。

【讨论】:

    【解决方案2】:

    你需要为 T 传入一个类型。 例如。如果您希望 ListInterface 是字符串列表

    private static int CountValue(ListInterface&lt;String&gt; theList,int theValue)

    【讨论】:

      猜你喜欢
      • 2014-06-11
      • 2014-08-26
      • 2013-11-14
      • 1970-01-01
      • 2016-10-02
      • 2016-01-08
      • 2011-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多