java我无法从LinkedList类调用客户端中的方法
运行正常,请参阅评论
很明显,这不只是客户的问题。我根本无法调用我创建的任何方法。在我的任务中,我应该在类中创建这些方法,然后在客户机中实现它们。我甚至不能在初始类中调用方法。不知道为什么
导入java。util.*
public class Driver5
{
public static final int SENTINEL = 0;
public static void main(String[] args)
{
int value = 1;
Scanner keyboard = new Scanner(System.in);
LinkedList<Intcoll5> P = new LinkedList<Intcoll5>();
while(value != SENTINEL)
{
if (value > 0)
{
P.**insert**(value);
}
}
}
}
还在研究一些方法,只是尝试调用insert()
import java.util.LinkedList;
import java.util.*;
public class Intcoll5
{
LinkedList<Integer> c = new LinkedList<Integer>();
ListIterator<Integer> I = c.listIterator();
public Intcoll5(int i)
{
c = new LinkedList<Integer>();
}
public void insert(int i)
{
Integer I = new Integer(i);
if (!c.contains(I))
{
c.addFirst(I);
}
}
public void copy(Intcoll5 obj)
{
while (I.hasNext())
{
}
}
public boolean belongs(Integer i)
{
return true;
}
public void omit(Integer i)
{
if (c.contains(i))
{
c.remove(i);
}
}
public int get_howmany()
{
int i = 0;
while (I.hasNext())
{
i++;
}
return i;
}
public void print()
{
while (I.hasNext())
{
Integer n = I.next();
System.out.println(n.intValue());
}
}
public boolean equals(Intcoll5 obj)
{
return true;
}
}
客户机中只有“插入”下划线,错误是:“找不到符号”
# 1 楼答案
在
LinkedList
类中没有insert
方法只需使用
add
我相信您正在尝试调用
Intcoll5#insert()
方法,但为此,您需要引用Incoll5
类的一个实例。请注意,您的P
对象引用了一个LinkedList
此外,
Intcoll5
类的构造函数对我来说似乎很奇怪,因为它没有使用它的i
参数。将其更改为: