0

I am constructing a rather large program that holds a lot of hard-coded data from a game (DnD 4e). For the sake of organization, I have broken this up into different classes (separate .java files) to avoid a 10k+ line innavigable program. I am storing an ArrayList of objects called Item. Each Item contains a String name, String price, and String description.

The problem is that I have all of the items stored in one method, Item, and the ArrayList<Item> in another method, InvList. How can I print every Item in the ArrayList<Item>? I am more experienced in Arrays, and I realize I could use a for loop and just iterate through the Array, but I'm unaware of the method of doing so for ArrayLists.

Main Method:

public class DnD {
    . . .
    public static void main(String[] args) {
        . . .
        System.out.println("The inventory has "+inv.size()+" items.");
        inv.add(Item.Candle);
        printInv(inv);
    } //main()
} //DnD

InvList Method, to keep track of people's inventories and the methods to manage them:

public class InvList {
    public static ArrayList<Item> inv = new ArrayList<>();
    // I want to create multiples ArrayLists for multiple people, ...
    // ... so there will be several here.
} //InvList()

Item Method, housing all of the Item information and management methods:

public class Item {
    . . .
    public String name;
    public String price;
    public String description;
    public static Item Candle = new Item("Candle", 1, "Provides dim light for 2 squares, and lasts for 1 hour.");
    public Item(String _name, double _price, String _desc) {
        name = _name;
        price = priceConverter(_price); //separate method, 
        description = _desc;
    } //Item()
    . . .
} //Item

Any attempt I make just ends up print hashcodes or giving errors. I've looked through many posts here that talk about overwriting toString() and what not, but I have not found an example of how to do so from other classes; these examples all seem to demonstrate how to do it if everything is in the exact same class/file. Am I doomed to just making one large mega file for everything? Or can I use three different classes here to print this one ArrayList<Item>?

Side note: let me know if I'm overkilling the code words, I'm trying to get a sense of what's acceptable over-formatting here.

Seymour Guado
  • 246
  • 2
  • 13
  • [Ways to iterate over a List in java?](https://stackoverflow.com/questions/18410035/ways-to-iterate-over-a-list-in-java) goes over a few ways to loop over a `List`. – Obicere Dec 01 '17 at 21:20
  • `inv.stream().parallel().forEach(System.out::println);` – LowLevel Dec 01 '17 at 22:57
  • But if you do not have too much (huge collection of) items in your List, avoid parallel: just write: `inv.forEach(System.out::println);` – LowLevel Dec 01 '17 at 22:57

1 Answers1

0

As you mention a toString method can be used to convert the class hashcode to readable output:

public class Item {
    @Override
    public String toString() {
        return "Name: " + name + ", price: " + price + ", description: " + description;
    }
}

To print the items in the ArrayList:

public class InvList {
    public static ArrayList<Item> inv = new ArrayList<>();
    for (Item item: inv) {
        System.out.println(item);
    }
}
  • Thanks, worked like a charm! That for loop is exactly what I was looking for, wasn't sure how to parse through the ArrayList. Cheers! – Seymour Guado Dec 01 '17 at 23:48