bless1204
bless1204

Reputation: 653

Items in list are Overwritten when adding new items

I was wondering if you could help me with this problem, I'm stuck for a day in trying to solve this one. So basically what I want to do is have a list that will contain an array of an array.

I have this initialization

List<double[][]> points = new ArrayList<double[][]>(); 

I add the elements this way:

points.add(existing.Foods);

My beeColony class contains the data that I want to add:

public  class beeColony{
    double Foods[][]=new double[FoodNumber][D];
    ....
}

And here's how I declare an instance of it:

public beeColony existing=new beeColony();

Here's a snippet of the code:

for(run=0;run<existing.runtime;run++)
{
    for (iter=0;iter<existing.maxCycle;iter++)
        points.add(existing.Foods);
}

What happens is that when I output all the items the list, it only contains the last added items.

for example:

Foods = {(0,0), (1,1), (2,2), (3,3)}
points.add(Foods);
Foods = {(4,4), (5,5), (6,6), (7,7)}
points.add(Foods);

The way that I understand it is that points.get(0)[0] should countain 0,0 and so on and points.get(1)[0] should contain 4,4 and so on. But what happens is points.get(0) also has the same values as points.get(1)

Upvotes: 0

Views: 1292

Answers (2)

Pankaj Sejwal
Pankaj Sejwal

Reputation: 1595

You could use combination of array and iterator to get the work done,

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

public class listerr {

    static int getrandom(){
        Random r = new Random();
        int next = r.nextInt(100);
        return next;
    }

    static double[][] getarr(){
        double[][] arr =  {{getrandom(),getrandom()}, {getrandom(),getrandom()},
                {getrandom(),getrandom()}, {getrandom(),getrandom()}};
        return arr;
    }


    public static void main(String[] args) {
        List<double[][]> points = new ArrayList<double[][]>(); 


        for(int run=0;run<3;run++)
        {
            double[][] arr = getarr();
            points.add(arr);
        }

        Iterator itr = points.iterator();
        while(itr.hasNext()){
            double[][] dbl = (double[][]) itr.next();
            for (int i=0;i<4;i++)
            { for (int j=0;j<2;j++){

                System.out.println(dbl[i][j]);
            }

            }

        }
    }
}

Upvotes: 0

laune
laune

Reputation: 31290

Collections like ArrayList<X> contain references to X objects, like one end of a string the other end of which is "tied" to the object itself, i.e., where the data resides.

This is also true for arrays like double[][].

What you do is to copy and store the reference end repeatedly, but at the other end there is one and the same double[][]. You can change the contents of that array, but all stored string ends lead to the same array object.

You must create new copies of that array to hold different array values. If you create another BeeColony, it will have another foods array. Otherwise, use new double[m][n] and copy the values. This is how:

double[][] d = { {1,2}, {3,4}, {5,6} };
// create the vector of (still missing) rows:
double[][] copy = new double[d.length][];
for( int row = 0; row < d.length; ++row ){
    // create another row of appropriate length:
    copy[row] = new double[d[row].length];
    // copy the element values
    System.arraycopy( d[row], 0, copy[row], 0, d[row].length );
}

PS: You should stick to Java conventions. Classe names are written in camel case starting with an upper case letter; variables and methods should start with a lower case letter. Loops should declare the loop counter inside the for: for( int run = 0;... ). Avoid public for class fields; code getters and setters to access private class fields.

Upvotes: 1

Related Questions