Thread: Iterating through 2d array with different lengths

Page 1 of 2 12 LastLast
Results 1 to 10 of 19
  1. #1 Iterating through 2d array with different lengths 
    Registered Member Lil Peep's Avatar
    Join Date
    Oct 2012
    Posts
    881
    Thanks given
    234
    Thanks received
    58
    Rep Power
    0
    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:
    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
    
    	};
    So what I need is to know how to loop it to add all the Item from each 'entry'?

    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
    Attached image Attached image Attached image Attached image Attached image
    Reply With Quote  
     

  2. #2  
    Respected Member


    Join Date
    Jan 2009
    Posts
    5,743
    Thanks given
    1,162
    Thanks received
    3,603
    Rep Power
    5000
    Quote Originally Posted by Matchbox 20 View Post
    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:
    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
    
    	};
    So what I need is to know how to loop it to add all the Item from each 'entry'?

    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
    not very clear what you actually want, so heres my guess at what you want to achieve

    Code:
    int winningIndex = Misc.random(items.length - 1);
    for(Item i : items[winningIndex]) {
        c.getItems().addOrDropItem(item.id, item.amount);
    }
    Reply With Quote  
     

  3. Thankful users:


  4. #3  
    Registered Member Lil Peep's Avatar
    Join Date
    Oct 2012
    Posts
    881
    Thanks given
    234
    Thanks received
    58
    Rep Power
    0
    Quote Originally Posted by Stuart View Post
    not very clear what you actually want, so heres my guess at what you want to achieve

    Code:
    int winningIndex = Misc.random(items.length - 1);
    for(Item i : items[winningIndex]) {
        c.getItems().addOrDropItem(item.id, item.amount);
    }
    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
    Attached image Attached image Attached image Attached image Attached image
    Reply With Quote  
     

  5. #4  
    Registered Member
    Join Date
    Apr 2016
    Posts
    148
    Thanks given
    46
    Thanks received
    30
    Rep Power
    19
    Quote Originally Posted by Matchbox 20 View Post
    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


    Code:
    int winningIndex = Misc.random(items.length - 1);
    for(Item i : items[winningIndex]) {
        c.getItems().addOrDropItem(i);
    }
    Reply With Quote  
     

  6. Thankful user:


  7. #5  
    Donator

    Jason's Avatar
    Join Date
    Aug 2009
    Posts
    6,092
    Thanks given
    2,402
    Thanks received
    2,823
    Rep Power
    4550
    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; }
        }
    Code:
    SomeItemEnumeration.values().stream().forEach(sie -> Stream.of(sie.items).forEach(p -> p.addItem(..));
    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.
    Reply With Quote  
     

  8. Thankful user:


  9. #6  
    Registered Member Lil Peep's Avatar
    Join Date
    Oct 2012
    Posts
    881
    Thanks given
    234
    Thanks received
    58
    Rep Power
    0
    Quote Originally Posted by Jason View Post
    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; }
        }
    Code:
    SomeItemEnumeration.values().stream().forEach(sie -> Stream.of(sie.items).forEach(p -> p.addItem(..));
    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.
    Thank you Jason! I actually didn't know you could use varargs with an enum... Otherwise I would have went this route. Rep + thanks to u sir
    Attached image Attached image Attached image Attached image Attached image
    Reply With Quote  
     

  10. #7  
    Donator

    Jason's Avatar
    Join Date
    Aug 2009
    Posts
    6,092
    Thanks given
    2,402
    Thanks received
    2,823
    Rep Power
    4550
    Quote Originally Posted by Matchbox 20 View Post
    Thank you Jason! I actually didn't know you could use varargs with an enum... Otherwise I would have went this route. Rep + thanks to u sir
    You can use varargs anywhere you're defining a parameter, however there are some rules with varargs too so its always a good idea to read up. No problem at all, anytime.
    Reply With Quote  
     

  11. Thankful user:


  12. #8  
    Registered Member Lil Peep's Avatar
    Join Date
    Oct 2012
    Posts
    881
    Thanks given
    234
    Thanks received
    58
    Rep Power
    0
    Quote Originally Posted by Jason View Post
    You can use varargs anywhere you're defining a parameter, however there are some rules with varargs too so its always a good idea to read up. No problem at all, anytime.
    Hey sorry, one more thing..

    Can you explain what the code highlighted in red is doing please
    Code:
    SomeItemEnumeration.values().stream().forEach(sie -> Stream.of(sie.items).forEach(p -> p.addItem(..));
    Attached image Attached image Attached image Attached image Attached image
    Reply With Quote  
     

  13. #9  
    Registered Member
    hc747's Avatar
    Join Date
    Dec 2013
    Age
    26
    Posts
    1,474
    Thanks given
    3,312
    Thanks received
    691
    Rep Power
    1098
    Quote Originally Posted by Matchbox 20 View Post
    Hey sorry, one more thing..

    Can you explain what the code highlighted in red is doing please
    Code:
    SomeItemEnumeration.values().stream().forEach(sie -> Stream.of(sie.items).forEach(p -> p.addItem(..));
    It provides the equivalent of:
    Code:
    for (SomeItemEnumeration sie : values()) {
    for (Item item : sie.items) {
    player.addItem(item);
    }
    }
    in a cleaner syntax.
    Forgive the formatting.
    Reply With Quote  
     

  14. Thankful user:


  15. #10  
    Registered Member Lil Peep's Avatar
    Join Date
    Oct 2012
    Posts
    881
    Thanks given
    234
    Thanks received
    58
    Rep Power
    0
    Quote Originally Posted by hc747 View Post
    It provides the equivalent of:
    Code:
    for (SomeItemEnumeration sie : values()) {
    for (Item item : sie.items) {
    player.addItem(item);
    }
    }
    in a cleaner syntax.
    Forgive the formatting.
    awesome than you
    Attached image Attached image Attached image Attached image Attached image
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 5
    Last Post: 08-14-2015, 03:33 PM
  2. Help with using an iterator for an array
    By No Entry in forum Help
    Replies: 2
    Last Post: 04-02-2015, 04:10 AM
  3. Replies: 3
    Last Post: 09-13-2009, 07:00 PM
  4. 2d array herblore
    By Lighten in forum Snippets
    Replies: 6
    Last Post: 08-24-2009, 05:25 AM
  5. Save arrays with little clutter
    By Mrquarterx in forum Snippets
    Replies: 8
    Last Post: 04-17-2009, 07:28 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •