0

I have an int array and I have assigned a value from it with a random index to a int variable. I wanted to know if there is a way to check the index from the array that it came from from the variable it was assigned.

Random rand = new Random();

private int[] cards = {2, 3, 4, 5, 6, 7, 8, 9, 10};

int a = cards[rand.nextInt(8)];

so I assigned a value from the array to variable a but I want to know if there is a way to check the variable a to see what the index was it came from

2
  • 4
    Just store your rand.nextInt(8) in an own variable, then you know your index ... quite obvious, right? Commented Nov 25, 2015 at 0:16
  • 1
    Very simple question. Though i have answered it but still the user has not given a thought before questioning. Commented Nov 25, 2015 at 0:20

4 Answers 4

2

You can store the value of random before the use.

int randomValue = rand.nextInt(8);
int a = cards[randomValue];
Sign up to request clarification or add additional context in comments.

1 Comment

thanks this helps... I'm somewhat new to Java and am creating a blackjack program for a class project. this was a scaled down version I posted here and my array has a total of 52 index's and some share the same value so the search option didn't really suit what I was doing. I'm sure there are much more efficient methods of doing what my program is attempting to do but this helps a lot... thanks!
0

You know the index number as you are already getting the value in the index position of the array.

int index = rand.nextInt(8);

It is always advised to use ArrayList. And if you had used that there is an easy method called "indexOf" to find out the index of the value in the list.

5 Comments

To be precise indexOf doesn't tell you where your element comes from, it just tells you where an "equal" instance (according to the equals implementation) is placed in the list. It could still be a different instance.
Sorry i did not get what you saying. But, say you have a value and you want to know the index of it, then indexOf function from ArrayList gives you that choice.
indexOf returns the first index where an equal object is located and equal is determined by the equals implementation. So this method might not return the index of the instance you're looking. For example: String a = new String("a"); String b = new String("a"); List<String> l = new ArrayList<>(Arrays.asList(a, b)); System.out.println(l.indexOf(b));. This prints "0", because on index 0 there is an equal instance, but is refers to a different object in the memory and is therefore not the one you're looking for, just a "similar" one.
This is not a problem, if you have unique values in your list or (boxed) primitive types, but one should mind that detail.
Absolutely right tom, but here we have integers so much details are not kept in mind. We can implement equals method if we want to compare Objects. Thanks for the details.
0

Try this:

int index= rand.nextInt(8);
int a = cards[index];

Comments

-1

With a simple array, you have to iterate to find out what position a given value is in. For example:

int findSlot(int a)
{
    for (int i = 0, n = cards.length; i < n; i++)
    {
        if (cards[i] == a) return i;
    }
}

However, if you use an ArrayList, you can make use of the method indexOf().

As noted by other folks who've answered, the index returned (in both cases) will be the first matching instance.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.