0
public double[] randomSolutions(int[] path) {
    double[] doubleSol = new double[vertices];
    Random r = new Random();
    for (int i = 0; i < doubleSol.length; i++) {
        doubleSol[i] = r.nextDouble();
    }
    return doubleSol;
}

public ArrayList randomDecSol(int[] path) {
    ArrayList<double[]> list = new ArrayList<>();
    int count = 0;
    for (int i = 0; i < 10000; i++) {
        list.add(randomSolutions(path));
    }
    return list;
}

public ArrayList<int[]> doubleToIntArrayList(ArrayList<double[]> list) {
    ArrayList<int[]> RandVerArray = new ArrayList<>();
    int[] randV = new int[vertices];
    for (int i = 0; i < list.size(); i++) {
        for (int j = 0; j < list.get(i).length; j++) {
            double vertex = ((list.get(i)[j] * 100));
            System.out.print(vertex + " ");
            randV[j] = (int) Math.round(vertex);
            System.out.print(randV[j] + " ");
        }
        RandVerArray.add(randV);
        System.out.print(" \n");
    }
    return RandVerArray;
}

public void print(ArrayList<int[]> RandVerArray){
    for(int[] sol : RandVerArray){
        System.out.print(Arrays.toString(sol)+"\n");
    }        
}

I have these 4 functions, the first one returns an array filled with random doubles. I then create an ArrayList and run the function 10000 times and add the arrays to the ArrayList. then I loop through the ArrayList and multiply the double in each index of the arrays by 100, round the numbers and convert them to an integer and duplicate the process of having an array list of arrays but with integers instead. I then try and print all of the numbers and it only prints the same numbers over and over again which just so happens to be the last array in the double ArrayList. What's going on here? I can't figure it out.

1 Answer 1

1

You are actually replacing the value of the array randV over and over. Change the code as below

public ArrayList<int[]> doubleToIntArrayList(ArrayList<double[]> list) {
ArrayList<int[]> RandVerArray = new ArrayList<>();
int[] randV = null;
for (int i = 0; i < list.size(); i++) {
    randV = new Integer[vertices];
    for (int j = 0; j < list.get(i).length; j++) {
        double vertex = ((list.get(i)[j] * 100));
        System.out.print(vertex + " ");
        randV[j] = (int) Math.round(vertex);
        System.out.print(randV[j] + " ");
    }
    RandVerArray.add(randV);
    System.out.print(" \n");
}
return RandVerArray;}
Sign up to request clarification or add additional context in comments.

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.