Results 1 to 8 of 8
  1. #1
    f_the_cook's Avatar
    f_the_cook is offline Member
    Join Date
    May 2008
    Posts
    21
    Rep Power
    0

    Exclamation help with an array.

    ok so i am making a text based rpg (i know not that great) and i am having a few problems with the array for my equipment class. i dont know how to start it. could anyone help me? i want to be able to store the weapons and armor and some items.



    thank you.
    Last edited by f_the_cook; 05-30-2008 at 11:05 PM.

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    You may read about When to use an array, and when to use ArrayList/List or AbstractList.
    freedom exists in the world of ideas

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes, first you should go through on each of them and get a brief idea. Depend on your requirement can choose a best one. Think that, your collection is dynamically changed all the time.

  4. #4
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    Very short lesson (if im correct God willing) would be like this:

    String[] armorArray = {"Gold Armor", "Bronze Armor", "Leather Armor", "Poet's Drapery"};

    That would set 4 values being those 4 armor types to an array called "armorArray"

    String[] can be transliterated as "String Array"

    Anyway once you have it set they will be set at index values of 0-3 (4 armors, but we start with 0 representing Gold Armor)

    Then you would look into writing code on calling them, im not too sure on that part but it might be like
    {if (userInput.equals("Gold Armor"))
    String armorType = armorArray[0]}

    Someone correct me on this if im wrong.

  5. #5
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    that may be good, if the length of that array is just less than 10....

    how about 5000?

    Will you compare them one by one?

    Searching through loop maybe good.... if you will use binary search algorithm....

    Another lines of code.... :(

    ArrayList will do the rest....
    freedom exists in the world of ideas

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Yep, binary search is the best. As sukatoa said if you have large number of elements this can be a real mess.

  7. #7
    chaster is offline Member
    Join Date
    Mar 2008
    Posts
    6
    Rep Power
    0

    Default

    Binary search if the elements are ordered... Which means every time you add something - you need to search for its place... :(

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Array Reflection: Multi Array Reflection
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:08 PM
  2. Array Help
    By bluegreen7hi in forum New To Java
    Replies: 2
    Last Post: 03-28-2008, 03:25 AM
  3. can anyone help... 2d Array
    By Mark1989 in forum New To Java
    Replies: 2
    Last Post: 03-12-2008, 09:59 PM
  4. Would appreciate your help with 2d Array..
    By cloudkicker in forum New To Java
    Replies: 1
    Last Post: 02-11-2008, 03:34 PM

Posting Permissions

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