|
Okay, so this is something I've really never done before. I'm creating a Crystal chest system and some of the rewards include more items than others.
So here's my array:
So what I need is to know how to loop it to add all the Item from each 'entry'?Code:private final static Item[][] items = { { new Item(995, 2000), new Item(1969, 1) }, // 2000 Coins, Spinach Roll {}, // Nothing { new Item(995, 1000), new Item(371, 5) }, // 1000 Coins, 5 Raw Swordfish { new Item(554, 50), new Item(555, 50), new Item(556, 50), new Item(557, 50), new Item(558, 50), new Item(559, 50), new Item(560, 10), new Item(561, 10), new Item(562, 10), new Item(563, 10), new Item(564, 10) }, // Misc. Runes { new Item(454, 100) }, // 100 Coal (Noted) { new Item(995, 750), new Item(TOOTH_HALF, 1) }, // 750 Coins, Tooth Half of Key { new Item(2363, 3) }, // 3 Runite Bars { new Item(995, 750), new Item(LOOP_HALF, 1) }, // 750 Coins, Loop Half of Key { new Item(441, 150) }, // 100 Iron Ore (Noted) { new Item(1183, 1) }, // Adamant Sq Shield { new Item(1079, 1) }, // 1 Rune Platelegs or Plateskirt based on gender };
pretty much draws a random number to determine which entry is being used
So let's say I am to be rewarded the first entry's items.
How do I determine the length of the entry (which is 2 item objects)
Format for my item adding method. Take Item object as parameter
Code:// For Loop ???? c.getItems().addOrDropItem(items[???][???]);
I know how to use arrays just not arrays with different amounts lol I think thye're called jagged arrays
TLDR: I need a functional way to loop through this jagged 2d array. Or I need a better way to do this other than 2d arrays
Thanks yeah I was reading into for each just now.
Is there any way to use the Item object as a parameter instead of 2 ints?
addOrDropItem(Item item)
vs.
addOrDropitem(int, itemId, int amount)
The problem was I don't know how to loop the elements within the addOrDropItem method using Item parameter instead of the int itemId, int amount
Sorry im absolutely shit at explaining things lol
It's probably worth noting that you could create an object to represent this array.
Code:public enum SomeItemEnumeration { COINS_AND_SPINACH(new Item(995, 2000), new Item(1969, 1)), NOTHING(); private final Item[] items; SomeItemEnumeration(Item... items) { this.items = items; } public Item[] getItems() { return items; } }Instead of worrying about index so much, which you don't have to at all, just worry about aggregating the items and iterating over them, consider an advanced-for loop or for-each loop.Code:SomeItemEnumeration.values().stream().forEach(sie -> Stream.of(sie.items).forEach(p -> p.addItem(..));
« Previous Thread | Next Thread » |
Thread Information |
Users Browsing this ThreadThere are currently 1 users browsing this thread. (0 members and 1 guests) |