'function loop for more than one times java

so basically i tried to call method that return array from other class java, it works perfectly except it double the size or length of the array 2 times from original. here is my code to return the array and the length.

public static double [] get_days(){
        //extracted days from table into array
        readFile();
        double[] data = new double[list.size()];
        System.out.println(list.size());
        Integer[] daysArray = list.stream().map(Product::getDay)
                .toArray(Integer[]::new);
        for(int i = 0; i < daysArray.length; i++){
            data[i] = Double.valueOf(daysArray[i]) ;
        }
        System.out.println("Array Size (Supposed to have 230 Data only) "+ data.length);
        return data;
    }

here is how I call the method on the other class

public class order_Picking extends AbstractProblem{
    get_Product test = new get_Product();

    public order_Picking(){

        super(161,1,1);

    }


    public double [] var = new double[numberOfVariables];
    public double [] Days = test.get_days();

    @Override
    public void evaluate (Solution solution){
        System.out.println(Days.length);
        //Jumlah produk pada batch ke-i pada picking list ke-i pada lokasi yang ke-i
        for(int i=0; i< var.length;i++){
            var[i]= EncodingUtils.getInt(solution.getVariable(i));
        }



        //jumlah ketersedian produk
        int k1 = 100;
        int k2 = 250;
        int k3 = 150;


        //Picking list-1
        double [] pl1 = new double[3] ;
        int p1 =100;
        pl1[0]= p1;
        int p2 = 20;
        pl1[1]= p2;
        int p3 = 40;
        pl1[2]= p3;
        int totalpl1 = p1+p2+p3;

        //picking list-2
        double [] pl2 = new double[3] ;
        int p4 = 10;
        pl2[0]= p4;
        int p5 = 20;
        pl2[1]= p5;
        int p6 = 15;
        pl2[2]= p6;
        int totalpl2 = p4+p5+p6;


        // Fungsi Tujuan untuk minimasi jarak
        double f1 = distance(var)  ;


        double c1 = 0;
        double c2 = 0;
        for (int i = 0 ; i < var.length;i++){
            c1 = (var[i]+var[i]*var[i])-totalpl1 ;
        }
        for (int i = 0 ; i < var.length;i++){
            c2 = (var[i]+var[i]*var[i])-totalpl2 ;
        }

        //constraint picking list-1


        //constraint picking list-2




        solution.setObjective(0, f1);
        solution.setConstraint(0,c1 == 0.0 ? 0.0 : c1);
        solution.setConstraint(0,c2 == 0.0 ? 0.0 : c1);
    }

    @Override
    public Solution newSolution() {

        Solution solution = new Solution(161, 1, 1);

        for (int i = 0 ; i<var.length;i++){
            solution.setVariable(i,EncodingUtils.newBinaryInt(0,1));
        }



        return solution;
    }

    public static void main(String[] args) {

        order_Picking value = new order_Picking();

        NondominatedPopulation result = new Executor()
                .withAlgorithm("GA")
                .withProblemClass(order_Picking.class)
                .withProperty("Populationsize",100)
                .withProperty("sbx.rate",0.2)
                .withProperty("pm",0.5)
                .withMaxEvaluations(10000)
                .run();
        for (Solution solution : result) {
            if (solution.violatesConstraints()) {
                System.out.println("Solution for index 0 : " + +solution.getObjective(0));
                  for (int i = 0; i < value.var.length; i++){
                      System.out.println("Solusi variabel ke-"+i+" adalah "+ solution.getVariable(i));
                  }
            }
        }
    }


    public double distance (double [] x){
        double a = 0;
        for (int i = 0; i < x.length ; i++){
            a += x[i];
        }
        return a;
    }

}

There is nothing wrong with the method but when i called it on other classs outside the public static void main it seems to run twice as it return the size of the array more than 230, i dont understand why it become 460 while it supposed to 230 here is the result on console



Solution 1:[1]

I genuinely do not see your list variable nor do I see your readFile() method (apologies if it's super obvious and I'm pulling a facepalm). My current hypothesis is so: when you read the file perhaps you do not empty the destination and it just loads up extra data leading to doubling.

Maybe it is a class-level StringBuilder? Please include this part in your question :)

private StringBuilder list = null;

Then inside the file reading method:

public static void readFile()
{
    // can empty with a new instance
    list = new StringBuilder();
    // or reset the length
    list.setLength(0);
    ...
    // perform reading, now with an empty StringBuilder
}

Without ^ this ^ you could be doing something similar to the following example:

list = new StringBuilder("1").append(", ").append("2");
readFile(); // in method -> list.append("1").append(", ").append("2");

Which would give the StringBuilder the output:

["1", ", ", "2", "1", ", ", "2"]

With the length of 6, instead of the desired:

["1", ", ", "2"]

With the length of 3. I could see this being responsible for an exact double count.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1