2017-12-02 242 views
2

我已經創建了一類的基團,其包含字符串和布爾對象的命名擴展「配件對象的ArrayList通過級Java

然後創建ArrayList類,然後將其添加到名爲「列表AccessoriesList「,從那裏輸入更多的數據。

然後我使用for循環創建了一個從ArrayList接收數據的附件對象。這仍然迴應爲空。

我環顧四周,發現最常見的問題是變量尚未初始化。所以,我想,仍然得到同樣的結果

所以這裏是配件

public static class Accessories { 

    Accessories(String Accessoriesname, boolean cold, boolean warm, boolean hot, boolean rain, boolean snow, boolean ice, boolean formal, boolean informal) { 
    } 
    String name =null ; boolean cold; boolean warm; boolean hot; boolean rain; boolean snow; boolean ice; boolean formal; boolean informal; 
} 

這裏是AccessoriesList

public ArrayList createAccessories() { 
    ArrayList<Accessories> Accessoriesist = new ArrayList<Accessories>(); 
    Accessoriesist.add(new Accessories("Bag", true, true, true, false, false, false, true, true)); 
    Accessoriesist.add(new Accessories("Gloves", true, false, false, true, true, true, true, true)); 
    Accessoriesist.add(new Accessories("Hat", true, false, false, true, true, true, false, true)); 
    Accessoriesist.add(new Accessories("Poncho", false, true, true, false, false, false, false, true)); 
    Accessoriesist.add(new Accessories("Scarf", true, true, false, true, true, true, true, true)); 
    Accessoriesist.add(new Accessories("Sunglasses", false, true, true, false, false, false, true, true)); 
    Accessoriesist.add(new Accessories("Tie", true, true, true, true, true, true, true, true)); 

    Accessories getAccessories =null; 
    String getname = null; 
    for (int i = 0; i < Accessoriesist.size(); i++) { 
     getAccessories = Accessoriesist.get(i); 
     getname = getAccessories.name; 
     System.out.println("this is the name : " + getname); 
     System.out.println("this is the Accessoriesist : " + Accessoriesist.get(i)); 
    } 
    return Accessoriesist; 
} 

取而代之的接收信息,我收到散列碼。

我想拋出一個配件對象(原始)從ArrayList,到另一個配件對象(新)。我試圖拉從附件對象(新)

回答

0

的數據有兩個問題:

首先,你的構造決不會複製你傳遞給它進級屬性: 配件(字符串Accessoriesname,布爾布爾型布爾型布爾型布爾型布爾型布爾型布爾型非正式)

將構造函數想象成具有可變參數的方法調用:在這種情況下,您不需要做任何事情大括號{}。 Java爲您提供了this關鍵字來引用類實例的屬性。所以,你需要的參數傳遞給你的構造函數明確地複製到性能你的類的實例:

Accessories(String Accessories name, boolean cold, boolean warm, boolean hot, boolean rain, boolean snow, boolean ice, boolean formal, boolean informal) { 
    this.name = name 
    this.cold = cold 
    this.warm = warm 
    this.hot = hot 
    this.rain = rain 
    this.snow = snow 
    this.ice = ice 
    this.formal = formal 
    this.informal = informal 
} 

其次,因爲該行的代碼連接字符串與對象,它會調用的ToString()方法,在你的附件對象:

System.out.println("this is the Accessoriesist : " + Accessoriesist.get(i)); 

.toString()方法的默認實現從對象超類繼承。如果您想覆蓋它,只是用同樣的方法添加簽名的方法,以類爲Object.toString()

public String toString() { 
    StringBuilder sb = new StringBuilder(this.name); 
    if (ice) { 
     sb.append(" (ice)") 
    } 
    // other properties 
    return sb.toString() 
} 

最後幾點注意事項:

  • 這是在Java中傳統使用的駝峯。對於課程,我們對首字母(MyClass)使用大寫字母 ,對於第一個 字母(myClass)使用小寫字母的類成員 變量和參數。所以你的ArrayList<Accessories> AccessoriesistArrayList<Accessories> accessoriesList遵循這個 約定。
  • 這可能是一個好主意,使你的所有不同 性能(冷,暖,冰,雪等)的enum叫什麼 像Properties,並讓您的配件類包含 列表。

歡迎來到Java!