2013-02-25 20 views
0

我一直在用我的setInner方法遇到這個問題,如果我保持它告訴我,「InnerList類型中的方法setInner(GList)不適用於參數(整數)「。這對我來說似乎很奇怪,因爲它看起來像一個Integer,可能適用於GList。有人能幫我弄清楚我做錯了什麼嗎?在類型InnerList不適用於參數(整數)

我的內部類將存儲整數內部列表。

public class InnerList { 
private String name; 
private GList<Integer> inner = new GList<Integer>(); 
public String getName() { 
    return name; 
} 
public void setName(String name) { 
    this.name = name; 
} 
public GList<Integer> getInner() { 
    return inner; 
} 
public void setInner(GList<Integer> inner) { 
    this.inner = inner; 
} 
} 

,是造成我的問題,我的主要方法中的我的公共類的部分:

GList<InnerList> list = new GList<InnerList>(); 
    InnerList iList = new InnerList(); 
    Scanner sc = new Scanner(System.in); 
    String answer; 
while (true) { 
      System.out.println("Do you want to enter a number (y/n)?"); 
      answer = sc.nextLine(); 
      if (answer.equals("y")) { 
       System.out.println("Enter Number: "); 
       answer = sc.nextLine(); 
       try { 
        Integer num1 = Integer.valueOf(answer);        
        if (list.isEmpty() == true) {       
         iList.setInner(num1); //ERROR IS HERE 
         list.insertFirstItem(iList); 
        } else { 
         iList = new InnerList(); 
         iList.setInner(num1); //AND HERE 
         list.insertNext(iList); 
        } 
       } catch (NumberFormatException e) { 
        System.out.println("You must enter an number! " + e); 
       } 
       continue; 
      } else { 
       break first; 
      } 
     } 
+0

您還沒有理解類型。一個整數不是一個GList。如果它擴展了GList類,它將是一個GList。而事實並非如此。即使在概念上,我也沒有看到任何人都可以想象一個整數是一個列表。 – 2013-02-25 22:11:58

+0

不知道你想做什麼,但你的'setInner'方法想要一個Integer對象的列表,並且你正在傳遞一個Integer對象。 – NominSim 2013-02-25 22:12:12

+0

所以基本上我需要另一個列表GList nList = new GList (); ? – Chris 2013-02-25 22:17:36

回答

2

錯誤消息是很清楚的。您正在使用

iList.setInner(num1); 

時,你應該做

iList.setInner(myIntegerGlist); 

匹配方法的預期參數類型。

+0

列表是應存儲值的位置;但我爲SNG做了改變,錯誤也是一樣的。 – Chris 2013-02-25 22:14:34

+0

是的,你需要創建一個新的'GList '。在上面的代碼中沒有看到一個。 – Reimeus 2013-02-25 22:19:35

+0

我已經把它打開了;而我卻遇到了我遇到的其他問題。沒有意識到這是這個原因。謝謝 – Chris 2013-02-25 22:20:05

相關問題