0

I am creating random number generator in Java as part of program I am writing to learn the language better (come from more C#/C++ background).

    ArrayList<Integer> al = new ArrayList<Integer>();
    Random ran = new Random();
    for(int i = 1; i <= 11; i++)
        al.add(i);


    for(int i = 0; i < 2; i++)
    {
        ArrayList<Integer> temp = new ArrayList<Integer>();
        int num = al.remove(ran.nextInt(al.size()));

        temp.add(num);
        Arrays.sort(temp);

        text("\Random Number " + i + " is: " + temp[i]);
    }

On arrays.sort(temp) I get a no suitable method error and in my text output function I get array required but java.util.ArrayList found. Can anyone suggest a better way to sort this random number array into ascending order or see something I am doing wrong currently that could easily be corrected? Thanks.

4 Answers 4

4

Use Collections.sort(temp).

In Java, arrays and lists are two different beasts. Arrays.sort() only works for arrays; the equivalent function for lists is Collections.sort().

I can't say I fully understand the logic behind your code, but you might also want to take a look at Collections.shuffle().

edit Upon closer inspection, there are other problems with the code:

  1. You are re-creating temp from scratch, so on each loop iteration it will contain exactly one element.
  2. temp[i] is not valid syntactically; the correct syntax is temp.get(i). Even with the correct syntax, it'll give you an "out of bounds" exception, since temp only contains one element.
Sign up to request clarification or add additional context in comments.

2 Comments

Many Thanks - thats what you get when you search array sort in Java on google first. :) I will also take a look at collections.shuffle. With regards the output line after changing to Collections.Sort(temp); can i still print out the line with the temp[i] index?
@KOL: The correct syntax is temp.get(i). See the updated answer.
3

When sorting a ArrayList you should use:

Collections.sort();

In your case, I suggest you to use a TreeSet, it provides you a sorted collection (it sort itself after every add or remove. Also, it prevents you from adding duplicate elements.

TreeSet<Integer> randomSet = new TreeSet<Integer>();
Random ran = new Random();
while(randomSet.size() < 3) {   
    randomSet.add(Math.abs(ran.nextInt()) % 11 + 1); //+1 to adjust your range [1..11]
}

for (int i : randomSet)
    System.out.println(i);

Math.abs() guarantee that you will only have positive numbers and you can use % operator to set a maximum value.

More info on add took from oracle docs:

If this set already contains the element, the call leaves the set unchanged and returns false.

3 Comments

Thanks for this extra logic. I want 2 random numbers generated in the range 1-11. So from your comment the % should have 11 after it and the for loop <=3 so generate 3 numbers? The only other contraint I have is that I dont want duplictae random numbers (i.e - if 3 comes out the next time round it should not be selected.
That was very useful - thanks - can you explain a bit more on how the % 11 + 1 is working - i.e why the need to add +1 and if I was my range was 20 I would not need the + 1 right? Is it the same as the modulo operator in C++? thanks
Yes, that's the same in C++. Without the +1, the range goes from 0 to 10. The +1 is just a kludge to adjust your range to 1 to 11
1

You can use Collection class for sort the ArrayList:

ArrayList<Integer> al =new ArrayList<Integer>();

Collections.sort(al , new Comparator<Integer>()
{
  public int compare(Integer a, Integer b) {
    return a.compareTo(b) ;
  }
});

So now the al has a sorted list.

Comments

0

It will do:

Collections.sort();

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.