Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7571567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:42:19+00:00 2026-05-30T15:42:19+00:00

I am creating random number generator in Java as part of program I am

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:42:21+00:00Added an answer on May 30, 2026 at 3:42 pm

    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would I go about creating a random number generator which has a bias
I require help with a random number generator I am creating. My code is
using c# (asp.net) i'm programmatically creating several drop down lists (random number). the user
I am creating a random ID using the below code: from random import *
I'm creating a multi-threaded random.org number getter to implement into my c# IRC bot.
I'm creating a script which creates a random security number for a user, and
I have the following code: Class B { void generator() { // creating random
I am creating a number guessing game to help myself learn how to use
Creating hashes of hashes in Ruby allows for convenient two (or more) dimensional lookups.
I'm creating an arbitrary number of instances (using for loops and ranges). At some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.