如何访问属于arraylist中的对象的arraylist

时间:2014-01-20 15:40:01

标签: java arraylist

我已经坚持了几天同样的问题,并用Google搜索了我能想到的一切,我放弃了。我正在尝试编写一个程序,你应该首先创建一个人物对象,然后能够给那个人不同类型的物品。我把每个被创造的人都放在一个arraylist中,每个人都应该有自己的库存,这也是一个arraylist。我只是不明白如何将项目添加到特定人员arraylist以及如何打印该arraylist。每当我创建一个新人时,我是否必须创建一个新的arraylist'所有物'实例?以及如何访问某些人的arraylist?感谢任何反馈,因为我是超级菜鸟。

import java.util.ArrayList;


public class Sakregister extends Programskelett{

public static ArrayList<Person> personList = new ArrayList<Person>();


protected boolean nextCommand(){
    String command = readString("> ").toLowerCase();

    switch(command){
    case "print list":
        printAll();
        System.out.println("Program is running");
    break;

    case "print person":
        printUser();
        System.out.println("Program is running");
    break;

    case "create item":
        newItem();

    break;

    case "create user":
        newUser();
    break;

    case "print richest":
        printRichest();
        System.out.println("Program is running");
    break;

    case "crash":
        initCrash();

    break;

    case "quit":
        System.out.println("Program has terminated");
        return true;
    default:
        System.out.println("Not a valid command");
}
    return false;
}

private void printAll() {



    }
private void initCrash() {


    for (Person thisPerson : personList) {
        for (Item thisItem : thisPerson.belongings)
            if (thisItem.name == "Stock"){
                ((Stock) thisItem).setStockCrash(0);
            }
            }
            }

private void printRichest() {


}

private void newUser() {
    System.out.println("enter name: ");

    String name = keyboard.nextLine();

    Person newPerson = new Person(name);


    personList.add(newPerson);



    System.out.println("Person added to list");
}

private boolean newItem() {

    System.out.println("enter item type: ");

    String itemType = readString("> ").toLowerCase();

    switch(itemType){

    case "trinket":

        addTrinket();

        break;

    case "stock":

        addStock();

        break;

    case "appliance":

        addAppliance();

        return true;
    default:
        System.out.println("Not a valid item type");
}
    return false;
    }

private void addAppliance() {

    System.out.println("Enter name of appliance: ");

    String appName = keyboard.nextLine();

    System.out.println("Enter initial price: ");

    int appInitialPrice = keyboard.nextInt();

    System.out.println("Enter level of wear: ");

    int appWear = keyboard.nextInt();

    Appliance newAppliance = new Appliance(appName, appInitialPrice, appWear);

    System.out.println("Enter name of owner: ");

    Object owner = keyboard.nextLine();

    for(Person entry : personList)
               if(entry.equals(owner))
            entry.belongings.add(newAppliance);


}

private void addStock() {

    System.out.println("Enter name of stock entry: ");

    String stockName = keyboard.nextLine();

    System.out.println("Enter amount: ");

    int stockAmount = keyboard.nextInt();

    System.out.println("Enter price: ");

    int stockPrice = keyboard.nextInt();

    Stock newStock = new Stock(stockName, stockAmount, stockPrice);

    System.out.println("Enter name of owner: ");

    String owner = keyboard.nextLine();

    keyboard.nextLine();

    for(Person entry : personList) {
        if(entry.equals(owner)) {
            entry.belongings.add(newStock);

    }

    }
}

private void addTrinket() {

    System.out.println("Enter name of trinket: ");

    String trinketName = keyboard.nextLine();

    System.out.println("Enter number of gems: ");

    int trinketGems = keyboard.nextInt();

    System.out.println("Choose gold or silver: ");

    String trinketMineral = keyboard.nextLine();

    keyboard.nextLine();

    Trinket newTrinket = new Trinket(trinketName, trinketGems, trinketMineral);

    System.out.println("Enter name of owner: ");

    String owner = keyboard.nextLine();

    for(Person entry : personList)

        if(entry.equals(owner))
            entry.belongings.add(newTrinket);



}

private void printUser() {
    // TODO Auto-generated method stub

}

protected void printMainMenu(){
    System.out.println("Choose a command: ");
    System.out.println("start");
    System.out.println("quit");
}

public static void main(String[] args) {

    Sakregister registerProgram = new Sakregister();
    registerProgram.run();

}

}

public class Item{

protected String name;

public Item(String name){

    this.name = name;
}

public String getItemName() {
    return name;
}

    import java.util.ArrayList;



    public class Person{

public String name;
public String items;

public ArrayList<Item> belongings = new ArrayList<Item>();

public Person(String name){

    this.name = name;
}

public String getName(){
    return name;

}


public String toString() {
   return "Name: " + name;
}
    }

2 个答案:

答案 0 :(得分:0)

每次创建新的ArrayList<Item> belongings对象时,都会创建Person的新实例,因此您无需再在任何位置创建它。但是,目前没有belongings的getter,因此您需要编写一个来访问给定人员的所有物。你想要这样的东西:

 public ArrayList<Item> getBelongings() {
   return belongings;
 }

您需要编写一个公共方法,该方法接受Item个对象,并addbelongings Person。您不应该需要帮助编写另一个直接在所有物上调用System.out.println()的公共方法,或者遍历它们并打印出来。

答案 1 :(得分:0)

“我只是不明白如何将项目添加到特定的人物arraylist以及如何打印该arraylist”

你的人在personList中,所以我会用personList.get(n)来获得第n个人。 要将项目添加到belongings,您可以使用personList.get(n).belongings.add(item)

要打印一个arraylist,您可以使用正常的foreach循环:

for(Item i:personList.get(n).belongings){
System.out.println(i.getItemName());
}

每次创建新人时,是否必须创建arraylist'所有物'的新实例?

ArrayList belongingsPerson类中的一个字段。使用构造函数创建人员时,会在内存中创建其所有字段和变量。每次创建人员时都会发生这种情况,因此每个对象(在您的情况下是personList中的人员)都有自己的字段,如belongings列表。