有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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) 个答案

  1. # 1 楼答案

    LinkedList类中没有insert方法

    只需使用add

    if (value > 0) {
       Intcoll5 object = new Intcall5();
       object.insert(value);
       P.add(object);
    }
    

    我相信您正在尝试调用Intcoll5#insert()方法,但为此,您需要引用Incoll5类的一个实例。请注意,您的P对象引用了一个LinkedList

    此外,Intcoll5类的构造函数对我来说似乎很奇怪,因为它没有使用它的i参数。将其更改为:

    public Intcoll5()
    {
       c = new LinkedList<Integer>();
    }