2013-04-20 30 views
0

我在學習Java方面還很新,我需要一種方法將從某個類構造而來的每個對象放入一個ArrayList中,以便稍後訪問。自動添加構造到ArrayList中的每個Class對象

這裏是我Item類:

import java.util.ArrayList; 
import java.util.*; 
public class Item 
{ 
    private String name; 
    private int quantity; 
    private ArrayList<Item> allItems = new ArrayList<Item>(); //the ArrayList I'm trying to fill with every Item object 

    /** 
    * Constructs an item of a given name and quantity 
    * @param name Item name 
    * @param quantity How much of an item the player has 
    */ 
    public Item(String name, int quantity) 
    { 
     this.name = name; 
     this.quantity = quantity; 
    } 

    /** 
    * @return Item name 
    */ 
    public String getItemName() 
    { 
     return name; 
    } 

    /** 
    * @return Quantity of the item 
    */ 
    public int getQuantity() 
    { 
     return quantity; 
    } 

    /** 
    * @return A list of items that have a quantity > 0 
    */ 
    public ArrayList<Item> getInventory() 
    { 
     ArrayList<Item> inventory = new ArrayList<Item>(); 
     for(Item i : allItems) 
     { 
      if(i.getQuantity() > 0) 
       inventory.add(i); 
     } 
     return inventory; 
    } 
} 

我怎樣才能每一次添加一個Item對象allItems構建的是?

+1

作爲友好評論,總是試圖使用接口,而不是實現,所以不用返回ArrayList的返回列表,並且在創建,任務列表 myList中=新的ArrayList ()。原因?如果將來您使用列表接口的不同實現(如LinkedList),則只需更改幾行即可。 – 2013-04-20 12:25:38

回答

3

首先,所以它是所有實例之間共享的arraylis必須是靜態的。 否則,每個實例都會有不同的變量。

有關實例/班級成員here的更多信息。

private String name; 
private int quantity; 
private static ArrayList<Item> allItems = new ArrayList<Item>(); 

然後,您可以在構造函數中添加創建的實例,將其稱爲「this」。

About the 'this' keyword

public Item(String name, int quantity) 
{ 
    this.name = name; 
    this.quantity = quantity; 
    allItems.add(this); 
} 
+0

upvoted這只是因爲它是唯一一個(除了我的)這解釋了什麼'靜態'關鍵字實際上做。 – Philipp 2013-04-20 10:19:09

+0

謝謝,它工作完美 – colinbr96 2013-04-20 10:42:44

0

一個可能的解決方案是申報的項目列表作爲靜態這樣的:另外在你的構造函數,你需要

Item.allItems // example would be System.out.println(Item.allItems) 

public static List<Item> allItems = new ArrayList<Item>(); 

之後,你可以使用下面的代碼片斷訪問以下代碼:

public Item(String name, int quantity) { 
    this.name = name; 
    this.quantity = quantity; 
    this.allItems.add(this); 
} 

但是請謹慎使用此方法,因爲它會添加列出所創建的每個項目,可能會導致可能的內存泄漏。

1

您當然希望爲所有項目列出一個列表,而不是每個項目都攜帶並維護其自己的所有項目的副本列表。如果這是你的意圖,你應該定義列表爲static

private static ArrayList<Item> allItems = new ArrayList<Item>() 

靜態變量是由類的所有實例共享。

在項目的構造函數中,只需將this添加到所有項目的列表中。

allItems.add(this); 
0

您需要一個靜態列表,然後您可以使用類構造函數將此對象添加到列表中。

import java.util.ArrayList; 
import java.util.*; 
public class Item 
{ 
    private String name; 
    private int quantity; 
    private static ArrayList<Item> allItems = new ArrayList<Item>(); //the ArrayList I'm trying to fill with every Item object 

    /** 
    * Constructs an item of a given name and quantity 
    * @param name Item name 
    * @param quantity How much of an item the player has 
    */ 
    public Item(String name, int quantity) 
    { 
     this.name = name; 
     this.quantity = quantity; 
     allItems.add(this); 
    } 

    /** 
    * @return Item name 
    */ 
    public String getItemName() 
    { 
     return name; 
    } 

    /** 
    * @return Quantity of the item 
    */ 
    public int getQuantity() 
    { 
     return quantity; 
    } 

    /** 
    * @return A list of items that have a quantity > 0 
    */ 
    public static ArrayList<Item> getInventory() 
    { 
     ArrayList<Item> inventory = new ArrayList<Item>(); 
     for(Item i : allItems) 
     { 
      if(i.getQuantity() > 0) 
       inventory.add(i); 
     } 
     return inventory; 
    } 
}