-2

I have an array of strings like so:

public ModeValue mode = new ModeValue("test_mode", "dog", new String[] {
        "cat", "dog", "rabbit", "horse", "bear", "fish", "bird", "snake"
}, this);

I would like to know how to treat each string as an integer. For example: cat = 1, dog = 2, rabbit = 3, etc. And then I would like to access each string by using that integer. This will be used in a list of string that can be clicked upon to have the modes switched.

Kayoki
  • 1
  • which array is like this? have never seen this in Java – Seek Addo Jul 22 '16 at 21:00
  • 1
    I'm not sure if I understand your question correctly, but can't you simply use the index of the array? If for some reason this is not an option for you then then just convert the array to a HashMap. – sinclair Jul 22 '16 at 21:02
  • Please read: http://stackoverflow.com/documentation/java/99/arrays/408/accessing-contents-of-an-array . If that doesn't help edit your question ald clarify your problem. – Radek Postołowicz Jul 23 '16 at 00:06

2 Answers2

0

An array of strings already lets you map integers with strings. To get the string associated with an integer, just look in the array at the index indicated by the integer (e.g. MyArray[myInteger]). To get the integer associated with a string, search the array and find the index at which the desired string appears. Please be careful to not allow strings to appear more than once in the array, as that would prevent 1..1 association between ints and strings.

You don't have to use an array for this - other indexable data structures such as lists and hashtables are also usable.

This kind of data structure is called a lookup table, and isn't uncommon.

Robert Columbia
  • 6,012
  • 14
  • 28
  • 36
0

Assuming that your String array is this and that you are initializing a ModeValue object with it:

new String[] {
    "cat", "dog", "rabbit", "horse", "bear", "fish", "bird", "snake"
}

Right now, it is declared anonymously. You have to declare it earlier with a name to access it using its name and index.

Ex:

String[] animals = new String[] {"cat", "dog", "rabbit", "horse", "bear", "fish", "bird", "snake"};
System.out.print(animals[0]); //cat
public ModeValue mode = new ModeValue("test_mode", "dog", animals, this);

Without more detail about ModeValue, I'm not sure how to help with the switching or how to access the array within the mode object. You might implement a get method in ModeValue to return the array, which will allow you to access it as normal.

Example of how you would declare a get method in the ModeValue class

public class ModeValue
{
    private String[] animals;
    public getAnimals()
    {
         return animals;
    }
}

And get animals like in your main method like so:

String[] mainAnimals = mode.getAnimals();
orccrusher99
  • 194
  • 2
  • 10
  • I wanted to somehow cast the current string mode into an integer as so: (Integer) this.mode.getValue() This however gives a casting error. Any further advice? – Kayoki Jul 22 '16 at 22:30
  • Oh. You're trying to search the array for "dog" and return the index of "dog"? – orccrusher99 Jul 22 '16 at 23:37