0

I am trying to build an array of custom object references. Here is some simplified code.

import java.util.*;

public class Map {

    private int[] dimension;

    private City[] cities;
    private int nCities;

    public static void main( String[] args ) {
        Map map = new Map( 5, 20, 500, 500 );
    }

    public Map( int nCities, int diameter, int w, int h ) {
        this.dimension = new int[2];

        this.dimension[0] = w;
        this.dimension[1] = h;

        this.create_cities( nCities, diameter );
    }

    private void create_cities( int nCities, int diameter ) {
        // data
        this.cities = new City[nCities];
        this.nCities = nCities;

        // locate cities
        long seed = System.currentTimeMillis();
        Random random = new Random( seed );

        int[] location = new int[2];

        for( int n = 0; n < nCities; n++ ) {
            location[0] = random.nextInt( this.dimension[0] );
            location[1] = random.nextInt( this.dimension[1] );

            this.cities[n] = new City( diameter, location );

            System.out.println( this.cities[n].toString() );
        }
        System.out.println( "\n" + Arrays.toString( this.cities ) );
    }
}

Result:

diameter: 20, location: [311, 324]
diameter: 20, location: [85, 294]
diameter: 20, location: [364, 182]
diameter: 20, location: [269, 412]
diameter: 20, location: [123, 200]

[diameter: 20, location: [123, 200], diameter: 20, location: [123, 200], diameter: 20, location: [123, 200], diameter: 20, location: [123, 200], diameter: 20, location: [123, 200]]

Can anyone unbang my head from the wall? I am storing the same reference 5 times over, correct? How do I not do that? I would like to store a reference to each unique City object.

0

3 Answers 3

2

You're storing the same location array in each city. Make a new array for each one and you should be good to go.

    for( int n = 0; n < nCities; n++ ) {
        int[] location = new int[2];
        location[0] = random.nextInt( this.dimension[0] );
Sign up to request clarification or add additional context in comments.

4 Comments

Good psychic debugging. You have guessed that the location is stored in the city as an array and not as integers that are copied, right?
@JKE Actually, it is safer to copy the values of the array passed in location inside the city class. It works both ways, but if you reuse the city class and you don't know how it is implemented, this is a likely error to happen
I guessed that the location reference is stored as-is in the City objects, which would produce the output shown. If the array was copied in the constructor, the points stored in another structure or as plain ints, no sharing would have happened and JKE wouldn't have had a problem.
@Luis Your suggestion to copy location in City's constructor also fixes it as Mat confirmed. I agree that this way seems like a cleaner design. Thanks all for the help!
0
        this.cities[n] = new City( diameter, location );

This is assigning the Reference to same 'location' object to each city, Thus in the end when you are printing all the cities, it shows the locations as latest object refernce..

Move the

       int[] location = new int[2];

inside the loop

     for( int n = 0; n < nCities; n++ ) {

or simply

        this.cities[n] = new City( diameter, new int[](random.nextInt( this.dimension[0] ),random.nextInt( this.dimension[1] ) );

Comments

-1

You are passing same diameter and location to the City constructor while creating City object, you need to find a way to pass different diameter and location each time you create a City object.

store the different diameter and location in an array and pass proper values while creating City Object.

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.