0

Java Array List is overriding the data added and put the last items added only.

The output expected is 1.0, 1.0, -1.0,-1.0, -1.0, 1.0,-1.0, -1.0, -1.0,-1.0, -1.0, -1.0

But, the output I get is -1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0 .

The code override the list by last array elements which is a=[-1,-1,-1,-1]. That is why all 12 elements of the list value is overridden to -1.

import java.util.ArrayList;
import java.util.List;

    public class Test {
public static void main(String[] args){

    double sum=0;  int n=0,y=0,count=0,x=1;
    double []a=new double []{1,0,1};
    double []b=new double [a.length];
    List<double[] >mylist= new ArrayList<>();      
    double [][]c=new double[][]{{0,0.66,0.66},{0.66,0,0.66},{0,0.66,0}};
      mylist.add(0,a);         

while(n>=0 && n<4){
      for (int i = 0 ; i < 3 ; i++)
        for (int j = 0 ; j < a.length; j++)
        {
            sum=sum+(c[i][j]*a[j]); 
            if(j==a.length-1)
                {
                    if(sum>0)
                        sum=1;
                    else
                        sum=-1;
                    b[y]=sum;
                    y++;
                    count++;
                    sum=0; 
                }


            if(count==a.length){
                           mylist.add(x,b);
                x++;
                y=0;
               count=0; 
                    for(int k=0;k<a.length;k++){
                        a[k]=b[k];
                    }
               } 
  }n++;
}

    for (int i = 0 ; i < mylist.size(); i++)
    for (int j = 0 ; j < a.length ; j++)
        {
            System.out.print(mylist.get(i)[j]+",");
        }
}                   
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Miller
  • 3
  • 5
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer May 19 '20 at 10:27

1 Answers1

0

Arrays in Java are passed by reference to methods, you can check this link for detailed explanation "pass by referece and pass by value" for more about Java parameter passing.

In you code, you just have multiple references of array a and array b. So that is why when their values change all initial values also change. you can use a debugger and you will see it starts changing from the second insertion of b to the ArrayList.

To fix this create a copy of array a at the beginning (whose content will not change). for each new insertion, create a temporary array to hold the values and insert them to your ArrayList.

Below is a fix

import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) {

    double sum = 0;
    int n = 0, y = 0, count = 0, x = 1;
    double[] a = { 1, 0, 1 };
    // Create a copy of a to insert at the beginning 
    double[] acpy = new double[] { 1, 0, 1 };
    double[] b = new double[a.length];
    List<double[]> mylist = new ArrayList<>();
    double[][] c = new double[][] { { 0, 0.66, 0.66 }, { 0.66, 0, 0.66 }, { 0, 0.66, 0 } };
    mylist.add(0, acpy);
    double[] temp ;
    while (n >= 0 && n < 4) {
        // this will create a new reference whenever you are about to insert a new element
        temp = new double[a.length];
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < a.length; j++) {

                sum = sum + (c[i][j] * a[j]);
                if (j == a.length - 1) {
                    if (sum > 0)
                        sum = 1;
                    else
                        sum = -1;
                    b[y] = sum;
                    // copy values of b for insertion to ArrayList
                    temp[y] = sum;
                    y++;
                    count++;
                    sum = 0;
                }

                if (count == a.length) {
                    mylist.add(x, temp);
                    x++;
                    y = 0;
                    count = 0;
                    for (int k = 0; k < a.length; k++) {
                        a[k] = b[k];
                    }
                }
            }
        n++;
    }

    for (int i = 0; i < mylist.size(); i++)
        for (int j = 0; j < a.length; j++) {
            System.out.print(mylist.get(i)[j] + ",");
        }
    }
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
  • Hello, Zenacha It is working perfectly. But when using A array (i.e 1,0,1) values taken as calculated value from computation (NOT GIVEN), at that time when I add array A to the list, can't get array A values from list, but I can access the rest. – Miller May 19 '20 at 01:53
  • I did not understand you well. So you cannot get the array you inserted at index 0? The whole mistake you are making is adding array objects to your ArrayList and later on manipulating this referenced objects. so if you wanna add an element from computation, create a reference which you won't modify like what is done inside the loop for temp array. Please clarify me more if this is not the answer to the probem – Zencha Gabriel May 19 '20 at 13:42