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

  • SEARCH
  • Home
  • 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 9044281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:56:47+00:00 2026-06-16T10:56:47+00:00

Possible Duplicate: How do I remove repeated elements from ArrayList? I want to replace

  • 0

Possible Duplicate:
How do I remove repeated elements from ArrayList?

I want to replace a duplicate number (if any) that i store in an ArrayList. I obtianed the numbers for a Random variable. Here is the class.

import java.util.*;

public class RandomNumbers {
// instance variables
private ArrayList<Integer> randomNumberList = new ArrayList<Integer>(); 
private Random randomNums = new Random();

public RandomNumbers(){
    // generating and adding random numbers to the list
    for (int i=0; i<MemoryGame.totalAnswers; i++)
        randomNumberList.add(randomNums.nextInt(32));

    System.out.println("Numbers in the list: " + randomNumberList);
    System.out.println("");
}

public ArrayList<Integer> getRandomNumbers(){
    return randomNumberList;
}
}

My school book tells me how to add,remove and retrieve a number, but not how to replace one that is duplicated.

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-06-16T10:56:49+00:00Added an answer on June 16, 2026 at 10:56 am

    If you are not limited to ArrayList, use a HashSet<Integer> instead (or better yet, a LinkedHashSet). HashSet<Integer> will garantee you not to have duplicated values in the collection, LinkedHashSet<Integer> will do the same and also preserve the ordering the the items.

    If you insist on having an ArrayList, then do this :

    public RandomNumbers(){
        HashSet<Integer> set = new HashSet<Integer>();
    
        // generating and adding random numbers to the list
        //for (int i=0; i<MemoryGame.totalAnswers; i++)
        while (set.size()<MemoryGame.totalAnswers)
            set.add(randomNums.nextInt(32));
    
        randomNumberList.addAll(set);  // dump the set in your ArrayList
    
        System.out.println("Numbers in the list: " + randomNumberList);
        System.out.println("");
    }
    

    Also, if MemoryGame.totalAnswers == 32, then you can speed up this process with a random list instead :

    public RandomNumbers(){
        // generating and adding random numbers to the list
        for (int i=0; i<MemoryGame.totalAnswers; i++)
            randomNumberList.add(i);
    
        Collections.shuffle(randomNumberList);
    
        System.out.println("Numbers in the list: " + randomNumberList);
        System.out.println("");
    }
    

    ** Update **

    Since MemoryGame.totalAnswers == 8, forget the last snippet. I’m leaving it there if anyone would happend to need it. You can skip the use of a Set entirely by folllowing pst‘s suggestion :

    public RandomNumbers(){
        // generating and adding random numbers to the list
        for (int i=0; i<32; i++)
            randomNumberList.add(i);
    
        Collections.shuffle(randomNumberList);
    
        // keep only the first ones we need
        randomNumberList.removeRange(MemoryGame.totalAnswers + 1, 32);
    
        System.out.println("Numbers in the list: " + randomNumberList);
        System.out.println("");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Remove from the array elements that are repeated. in Ruby I would
Possible Duplicate: Standard way to remove multiple elements from a dataframe I know in
Possible Duplicate: PHP remove special character from string I want to find a regex
Possible Duplicate: how to remove index.php from url in codeigniter ? I want to
Possible Duplicate: How to remove elements from an array I have an List with
Possible Duplicate: Remove duplicates from array I have a list of items. I want
Possible Duplicate: Remove non breaking space (&nbsp;) from between elements using jquery How to
Possible Duplicate: Remove text with jQuery I want to remove the text from a
Possible Duplicate: Remove items from a list while iterating in Python I want to
Possible Duplicate: Remove elements from array? how would I remove all array elements but

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.