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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:09:20+00:00 2026-06-13T08:09:20+00:00

I have 3 classes and a main class. The classes are City, Tour and

  • 0

I have 3 classes and a main class. The classes are City, Tour and Population. A population has an arraylist of X amount of tours and tour has an array list of X amount of cities. Where a City is just an X and Y coordinate.
When I create new tours the cities within them are randomises and then i add the tour to the population arraylist and this is working fine, the outputs are randomised. However if I then make output the arraylist again they are no longer randomised and all the tours are the same.
I think this will be easier to explain by showing some code:

Population.java

public class Population {

private ArrayList<Tour> toursList;

public Population(int size, ArrayList<City> loadedCities)
{
    toursList = new ArrayList<Tour>();
    
    // Fill the population with the number of tours
    for (int i = 0; i < size; i++) {
        Tour newTour = new Tour(loadedCities);
        newTour.setId(i);
        toursList.add(newTour);
    }
    
}

public void output() 
{
    for (Tour tour : toursList)
        System.out.println(tour.toString());
}
}

Tour.java

public class Tour {

private ArrayList<City> tour;
private int id = 0;

public Tour(ArrayList<City> cities)
{
    Collections.shuffle(cities);
    tour = cities;
}

public void setId(int i)
{
    this.id = i;
    System.out.println("Constructor: "+toString());
}

public int getId()
{
    return id;
}

public String toString()
{
    String str = "Tour: "+id+" - ";
    for (City city : tour) {
         str += city.toString()+" | ";
    }
    return str;
}
}

City.java

public class City {

private int code;
private Double y;
private Double x;

public City(int code, Double y, Double x)
{
    this.code = code;
    this.y = y;
    this.x = x;
}

public int getCode() 
{
    return code;
}

public Double getX() 
{
    return x;
}

public Double getY()
{
    return y;
}

public String toString()
{
    return "Code: "+this.code+" - X: "+this.x+" Y: "+this.y;
}
}

And then the main class just makes these calls after loading the cities arraylist:

Population population = new Population(10, cities);
population.output();

The console output for the few println’s are as follows (stripped down version):

Constructor: Tour: 0 - Code: 2 - X: 42373.8889 Y: 11108.6111
Constructor: Tour: 1 - Code: 28 - X: 43026.1111 Y: 11973.0556
Tour: 0 - Code: 8 - X: 42983.3333 Y: 11416.6667
Tour: 1 - Code: 8 - X: 42983.3333 Y: 11416.6667 

You can see the tours have now all become the same and in the same order.

  • 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-13T08:09:21+00:00Added an answer on June 13, 2026 at 8:09 am

    You use the same ArrayList<City> cities for all tours.

    Tour constructor should be like this:

    tour = new ArrayList<City>(cities);
    Collections.shuffle(tour);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two java classes. Schedule is the main class that uses an array
I have a main class and two extended classes: class Main { public $foo;
I have a java application where main class is dependent with some other classes.
I have 3 classes in my example: Class A, the main activity. Class A
I have two classes orchestrated by a main class and I would like to
I have two classes named BaseCatalog and City. BaseCatalog is the parent class of
I have three classes, one main class, one GUI class which uses awt+swing to
So I have two classes. One is a class called Main. The Main class
I currently have 3 classes, a main class containing a GUI, and the fnameTxtField
I have two classes, Main Sub In main class, i have a button and

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.