-
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.
-
You may read about When to use an array, and when to use ArrayList/List or AbstractList.
-
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.
-
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.
-
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....
-
Yep, binary search is the best. As sukatoa said if you have large number of elements this can be a real mess.
-
Binary search if the elements are ordered... Which means every time you add something - you need to search for its place... :(
-
Yes, why is that difficult. That is the best way to handle dynamically change the size of the array. Actually a list.