8

I'm working on my first Android app, a math game for my kid, and am learning Java in the process. I have two ArrayLists, one of integers 0..9 and one strings of possible operations, at present, just + and -.

I would like to write a method that returns a random index into an ArrayList, so I can select a random element. What I'm running into is that I need two methods, one for each type of ArrayList, even though the code is identical. Is there a way to do this in a single method?

What I use now:

Random randomGenerator = new Random();

  . . .

n = randomIndexInt(possibleOperands);
int op1 = possibleOperands.get(n);
n = randomIndexInt(possibleOperands);
int op2 = possibleOperands.get(n);
n = randomIndexStr(possibleOperations);
String operation = possibleOperations.get(n);

    . . .

int randomIndexInt(ArrayList<Integer> a){
    int n = randomGenerator.nextInt(a.size());
    return n;
}

int randomIndexStr(ArrayList<String> a){
    int n = randomGenerator.nextInt(a.size());
    return n;
}

What I'd like to do is collapse randomIndexInt and randomIndexStr into a single method.

4
  • You can write a method that will take A (of type int) as an argument and return any random number between 0 and A and call this method passing your array list sizes. Commented Jul 31, 2012 at 7:18
  • Remember to accept an answer, and maybe +1 if you like someone else as well :) Commented Jul 31, 2012 at 7:43
  • 1
    +1 for coding a math game for your kid. Commented Jul 31, 2012 at 8:47
  • Thanks, all, with the help I got here I was able to improve the method further, and my code now looks like this: int op1 = (Integer) getRandomElement(possibleOperands); int op2 = (Integer) getRandomElement(possibleOperands); String operation = (String) getRandomElement(possibleOperations); . . . Object getRandomElement(ArrayList<?> a){ int n = randomGenerator.nextInt(a.size()); return a.get(n); } Apologies, I can't get this comment to format correctly. Commented Jul 31, 2012 at 17:34

7 Answers 7

18

declare your method as int randomIndexInt(ArrayList<?> a)

Sign up to request clarification or add additional context in comments.

1 Comment

It could just be Collection<?> since that's what declares size().
7

You need only the size of the array right? so do it:

int randomIndex(int size){
    int n = randomGenerator.nextInt(size);
    return n;
}

1 Comment

Thanks. That's another approach, and one I'd done earlier, but I actually wanted to push as much common code as possible (including getting the Array size) into the method.
5

with this code you can pass any type of list

int randomIndex(List<?> list){
    int n = randomGenerator.nextInt(list.size());
    return n;
}

3 Comments

Thanks; that worked okay with the array, but the int array chokes it as a primitive type. I suppose I could use Integer instead of int, but the <?> approach fixed it nicely.
You wouldn't be able to pass a List<Integer> or a List<String> into this method.
Ups, I forget about generics cast. Thanks @PaulBellora.
2

just make it:

private int randomIndex(int size){
return randomGenerator(size);
}

And then call them with randomIndex(yourArray.size());

Comments

2

More generic is to use List than ArrayList in method signature.

int your_method(List<?> a){
//your code
}

Comments

1

you can use Generics as follows

declare your function as below

private <T> int randomIndex(ArrayList<T> a){
    int n = randomGenerator.nextInt(a.size());
    return n;
}

now you can pass ArrayList<String> or ArrayList<Integer> to this function without any issue like this

ArrayList<String> strList = new ArrayList<String>();
strList.add("on1");
System.out.println(randomIndex(strList));
ArrayList<Integer> intList = new ArrayList<Integer>();
intList.add(1);
System.out.println(randomIndex(intList));

Comments

0
int randomIndex1(ArrayList<?> a)
{
   int n = randomGenerator.nextInt(a.size());
   return n;
}

int randomIndex2(int size)
{
   int n = randomGenerator.nextInt(size);
   return n;
}

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.