Results 1 to 18 of 18
  1. #1
    angrylolrus is offline Member
    Join Date
    Nov 2012
    Posts
    12
    Rep Power
    0

    Exclamation Array item retrieval problems

    I'm trying to write a game, its got a player, and the player should be able to equip items from his inventory. The only items that can be equipped are weapons and armor, both subclasses of item. All items are saved inside of an array of items (which allows the armor/weapons to be stored in it), which is very convenient.
    The problem happens here. to equip an item, you send it to the equip(Item) equip(Weapon) or equip(Armor) methods. When I equip an item, I try to do equip(inventory[(user input)]), but that always gets an item, since it is an array of items.
    What happens next is that what is pulled out of an array is always an item (which is not equippable) so it always sends that back. What can I do with my code to make it so that there is a single array for the inventory, but will be able to return weapons or armor when something from the array is called.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Array item retrieval problems

    Are Weapon and Armor the only subclasses of Item? Are both Equipable? If so, why not also make Item Equipable? Else, perhaps you will want to use a List<Equipable> (if it is an interface).

  3. #3
    angrylolrus is offline Member
    Join Date
    Nov 2012
    Posts
    12
    Rep Power
    0

    Default Re: Array item retrieval problems

    Right now, weapon and armor are both subclasses of Equipment (which adds nothing, just orginization) and Equipment is a subclass of Item. I think what you're suggesting is that I add a boolean equipable = false to regular items. I tried that, and when it sends, it can't use the armor and weapon's getEquipSlot() method, since items don't have that method

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Array item retrieval problems

    Then perhaps you want to keep a List<Equipment> and put all Equipment objects in that list. An ArrayList<Equipment> would work well for this.

  5. #5
    angrylolrus is offline Member
    Join Date
    Nov 2012
    Posts
    12
    Rep Power
    0

    Default Re: Array item retrieval problems

    What is the difference between an ArrayList and a regular Array?
    Also, I don't know that that would work either, since the equipment class's only method is the constructor which is just super();
    I was hoping to keep all the items in a single array so that they would be in whatever order they've been picked up in, and so I don't have to deal with parsing the input for which item they want to equip/inspect/use when they select it from the list.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Array item retrieval problems

    Think of an ArrayList as a flexible array as it's almost an array of variable size. You don't use array indices with it [...] but instead use the add(...) and get(...) methods. Google for a tutorial on it as it is quite a useful tool.

    Note you can have your class hold several ArrayLists and this won't increase overhead too much since you don't duplicate objects if you put them in several lists since you're only putting the object reference in the list.

    Also note that if all the subclasses of a class such as Equipment share a certain behavior, then the super class or interface ought to define it.

  7. #7
    angrylolrus is offline Member
    Join Date
    Nov 2012
    Posts
    12
    Rep Power
    0

    Default Re: Array item retrieval problems

    Well, the Weapons don't actually have an equipSlot, that is only for the armor.
    If I'm understanding correctly, an Array makes copies of an object( so an item can be instantiated right into the array), while an ArrayList just points to the object itself and the object has to be constructed elsewhere?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Array item retrieval problems

    Quote Originally Posted by angrylolrus View Post
    Well, the Weapons don't actually have an equipSlot, that is only for the armor.
    Only shared behavior should be implemented as shared methods of the parent type.

    If I'm understanding correctly, an Array makes copies of an object( so an item can be instantiated right into the array), while an ArrayList just points to the object itself and the object has to be constructed elsewhere?
    No, not quite. An array holds references (if an array of reference type and not an array of primitive) just the same as an ArrayList. In fact internally an ArrayList holds objects in an array.

  9. #9
    angrylolrus is offline Member
    Join Date
    Nov 2012
    Posts
    12
    Rep Power
    0

    Default Re: Array item retrieval problems

    Alright, so I started writing a test program to see if using the array list is what I want. Problem is, it says that it cannot find the ArrayList class, it throws this compilation error. Probably a dumb question, but if oyu know it saves me some time searching. So far as I could see, there were no packages that had to be imported and no special preparations made.

    ArrayListTest.java:11: error: cannot find symbol
    ArrayList<Item> inventory = new ArrayList<Item>();
    ^
    symbol: class ArrayList
    location: class ArrayListTest

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Array item retrieval problems

    Are you importing the class if you're using it? You've got the import java.util.ArrayList; at the top of the class, right?

  11. #11
    angrylolrus is offline Member
    Join Date
    Nov 2012
    Posts
    12
    Rep Power
    0

    Default Re: Array item retrieval problems

    Oh, alright. The tutorial I found online didn't mention that it needs to be imported, and I didn't see that it was part of any package on the API

  12. #12
    angrylolrus is offline Member
    Join Date
    Nov 2012
    Posts
    12
    Rep Power
    0

    Default Re: Array item retrieval problems

    With the test file I made, this all came out looking like its what I need. Thanks a ton for your help. If I ever need any other help figuring out Java, I now know where to go for it.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  14. #14
    angrylolrus is offline Member
    Join Date
    Nov 2012
    Posts
    12
    Rep Power
    0

    Default Re: Array item retrieval problems

    Alright, I thought that the array list would fix my problem, but it turns out it didn't. I'll post the code I've got, and maybe somebody canhelp me with. I've only included the important bits of it here, there are still tons of other irrelevant methods.
    Java Code:
    import java.util.*;
    
    public class player
    {
    private ArrayList<Item> inventory = new ArrayList<Item>();
    
    public void equipItem(int x)
          {
             equip(inventory.get(x));
          }
    
    public void equip(Item item)
          {
             System.out.println("You cannot equip that item");
          }
    
    public void equip(Weapon weapon)
          {
             inventory.add(equipped);
             equipped = weapon;
             
          }
    }
    The problem is that, every time I call the equipItem() method, it sends along an item, which it decides can't be equipped, even if the item stored there is a weapon. Getting the item there and printing it out with the toString() returns the weapon's toString() instead of the item's, so I'm really not sure what to do at this point. Any help would be cool.

  15. #15
    angrylolrus is offline Member
    Join Date
    Nov 2012
    Posts
    12
    Rep Power
    0

    Default Re: Array item retrieval problems

    Alright, I think I've got everything in working order. I made a String type. On created items, it is equal to item. Whenever you make a Weapon or piece of armor, it sets that pieces type to "weapon" or "armor", respectively. When it recieves the item out of the ArrayList, it asks for its type, and then casts it based on that, so
    Java Code:
    public void equipItem(int x)
    {
    Item temp = inventory.get(x);
    if(temp.getType().equals("armor"))
    equip((Armor)temp);
    else if(temp.getType().equals("weapon"))
    equip((Weapon)temp);
    else
    equip(item);
    }

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,413
    Rep Power
    20

    Default Re: Array item retrieval problems

    Quote Originally Posted by angrylolrus View Post
    Alright, I think I've got everything in working order. I made a String type. On created items, it is equal to item. Whenever you make a Weapon or piece of armor, it sets that pieces type to "weapon" or "armor", respectively. When it recieves the item out of the ArrayList, it asks for its type, and then casts it based on that
    I think you missed the point Fubarable made earlier.
    Quote Originally Posted by Fubarable View Post
    Also note that if all the subclasses of a class such as Equipment share a certain behavior, then the super class or interface ought to define it.
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  17. #17
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,413
    Rep Power
    20

    Default Re: Array item retrieval problems

    Also, and less important but as a matter of good form, I would generally expect a method equipItem(...) to take a parameter of type Item; a better name for your method that takes an int parameter might be equipItemAt(...).

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  18. #18
    angrylolrus is offline Member
    Join Date
    Nov 2012
    Posts
    12
    Rep Power
    0

    Default Re: Array item retrieval problems

    Quote Originally Posted by DarrylBurke View Post
    I think you missed the point Fubarable made earlier.
    Yeah, I think what he suggested was what I did, but i just missed exactly what he had meant by it.

    Quote Originally Posted by DarrylBurke View Post
    Also, and less important but as a matter of good form, I would generally expect a method equipItem(...) to take a parameter of type Item; a better name for your method that takes an int parameter might be equipItemAt(...).
    When the player gets a list of their inventory, it puts them in a list, with a number at the beginning, and I had already defined equip, so I did the next logical thing and just made it equipItem. At some point, I'll also make an equipItem(String itemName) that checks and finds an item by name, rather than by its place in the inventory.

    Thanks for all your help, if I find myself wanting of more knowledge, I'll be sure to return to this forum.

Similar Threads

  1. Replies: 1
    Last Post: 11-14-2012, 07:53 PM
  2. Displays every second item off array
    By Johanis in forum New To Java
    Replies: 2
    Last Post: 07-30-2012, 10:03 PM
  3. Removing an Item from an array
    By chris1 in forum New To Java
    Replies: 3
    Last Post: 04-25-2011, 06:59 AM
  4. access array list item from draw
    By trishtren in forum New To Java
    Replies: 8
    Last Post: 04-19-2011, 12:34 PM
  5. Getting an item from an array
    By ile4 in forum New To Java
    Replies: 9
    Last Post: 01-13-2011, 03:42 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •