2013-09-22 38 views
-1

瞭解它的工作原理,請參閱註釋。我無法從我的LinkedList類中調用客戶端中的方法

很明顯,它不僅僅在客戶端。我無法調用任何我創建的方法。對於我的任務,我應該在一個類中創建這些方法,然後在客戶端中實現它們。我甚至無法在最初的課程中調用這些方法。不知道爲什麼。

import 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

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>(); 
} 
+0

因此,如果我想將類實現到客戶端,我該怎麼做?因爲我有那個插入方法,所以我如何在客戶端調用它?謝謝。 – HelloKomputer

+0

查看我的更新回答, –

+0

'Intcoll5'表示一個整數列表。 'LinkedList '是'Intcoll5'的列表,因此是lists_的_list。你確定這就是你想要的嗎?如果是這樣,那麼它看起來像你想要在整數列表的_one_中插入一個整數,但是哪一個呢? – ajb