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 8613023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:46:14+00:00 2026-06-12T04:46:14+00:00

I have a piece of code, and the idea is that it takes in

  • 0

I have a piece of code, and the idea is that it takes in an array list with n amount of numbers and shuffles it 50 times and adds each time adds the new shuffle to another array list.

However what it seems to do is shuffle it once, add it to the array list (Like is should), but for the next 49 times, it doesnt shuffle it. It only adds the same one.
You may understand more from my code below:

int chromeSize;
ArrayList<GeoPoint> geoPoints = new ArrayList<GeoPoint>();      
ArrayList<Integer> addToFirstChrome = new ArrayList<Integer>();
ArrayList<ArrayList<Integer>> populationShuffle = new ArrayList<ArrayList<Integer>>();

for (int i=0; i<geoPoints.size(); i++) {
  addToFirstChrome.add(i);
}
System.out.println("add To First Chrome " + addToFirstChrome);

for (int j =0; j<50; j++) {
  Collections.shuffle(addToFirstChrome);
  populationShuffle.add(addToFirstChrome);
}  

for (int p=0;p<populationShuffle.size();p++) {
  System.out.println("Pop " + p +"=" + populationShuffle.get(p));
}

And here is a sample of the output:

10-02 10:10:26.785: I/System.out(19648): add To First Chrome [0, 1, 2, 3, 4]
10-02 10:10:26.790: I/System.out(19648): Pop 0=[2, 1, 3, 4, 0]
10-02 10:10:26.790: I/System.out(19648): Pop 1=[2, 1, 3, 4, 0]
10-02 10:10:26.790: I/System.out(19648): Pop 2=[2, 1, 3, 4, 0]
10-02 10:10:26.790: I/System.out(19648): Pop 3=[2, 1, 3, 4, 0]
10-02 10:10:26.790: I/System.out(19648): Pop 4=[2, 1, 3, 4, 0]

So as you see, it shuffles the first one, but not anymore.
Am i missing something here?

  • 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-12T04:46:15+00:00Added an answer on June 12, 2026 at 4:46 am

    Am i missing something here?

    Yes. You’re missing the fact that you’re adding the same reference on each iteration:

    for(int j =0; j<50; j++) {
        Collections.shuffle(addToFirstChrome);
        populationShuffle.add(addToFirstChrome);
    }
    

    That’s effectively the same as:

    for (int j =0; j < 50; j++) {
        Collections.shuffle(addToFirstChrome);
    }
    for (int j = 0; j < 50; j++) {
        populationShuffle.add(addToFirstChrome);
    }
    

    The value of addToFirstChrome is just a reference.

    It sounds like you want 50 separate collections, in which case you need to create a new collection on each iteration:

    for (int j = 0; j < 50; j++) {
        List<Integer> copy = new ArrayList<Integer>(addToFirstChrome);
        Collections.shuffle(copy);
        populationShuffle.add(copy);
    }
    

    (Note that this requires you to change the type of populationShuffle to List<List<Integer>> or ArrayList<List<Integer>> – prefer programming to interfaces where possible.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this piece of code, that takes a single function with no parameter,
I have a piece of code that was written by someone else before the
I have a piece of code (below) that can get the text of an
We have the following piece of code (idea for this code was found on
I have a piece of code that aims to launch the android camera, let
I have a piece of code that needs to be able to modify a
I have a piece of code that divides a image matrix img into small
I have a piece of code that looks something like this (ClearImportTable and InsertPage
I currently have a piece of code that I am working on using function
I have a piece of code that needs to do many computations based on

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.