0

Okay so I have this two Lists called idadults and adults, I also have two String[] arrays called infoAdults (of size 255) and adultsID (of size 1). The code I have makes infoAdults read the information from a CSV file and adds that information to the adults list. It also checks if any value/box is missing, and changes its value to "9999". On column #15 in the CSV file, there's the id of each Adult, but some of them are missing. adultsID grabs each value of the column and then adds it to the idadults list. The code also first checks if that value exists, and if not it changes it to "9999" again. My problem is that when I print the adults list it prints everything correctly like it should be, but when I try to print the idadults list it only prints the last value in the column over and over again n times, n being the size of the column. I already tried removing the "static" part when I define both of my Lists, but it didn't work. I also already tried to print adultsID[0] individually, and they all print correctly. What do?

public String[] adultsID = new String[1];
public static List<String[]> idadults = new ArrayList<String[]>();
public static String csvFile = userDir + "/Adults.csv";
public String[] infoAdults = new String[255];
public static List<String[]> adults = new ArrayList<String[]>();

try {
        br = new BufferedReader(new FileReader(csvFile));

        while ((line = br.readLine()) != null) {
            infoAdults = line.split(csvSplitBy);

            for(int i=0;i<infoAdults.length;i++){
                if(infoAdults[i].isEmpty()){
                    infoAdults[i]="9999";
                }
                if(i>16){
                    adultsID[0]=infoAdults[16];
                }
                else{
                    adultsID[0]="9999";
                }
            }
            idadults.add(adultsID);
            adults.add(infoAdults);
        }
        for (String[] strings : idadults) {
            System.out.println(Arrays.toString(strings));
        }
    }

1 Answers1

0

When you add adultsID to adults, you are adding the reference of adultsID.

Inside the while loop you are modifying only the data referenced by adultsID:

             if(i>16){
                    adultsID[0]=infoAdults[16];
                }
                else{
                    adultsID[0]="9999";
                }

And then you are adding the same reference to adults, on every iteration

idadults.add(adultsID);

So all the references point to same data which is modified every time the loop runs.

That is why you see only the last value reflected in all the list elements.

But for infoAdults , you are reassigning a new array to infoAdults everytime in the loop:

    infoAdults = line.split(csvSplitBy);

So infoAdults refers to new data on every iteration.

Thus you add a new instance of infoAdults everytime in the list and all of them refer to different data

To get the expected output you can assign a new array to adultsID everytime in loop:

 while ((line = br.readLine()) != null) {
        infoAdults = line.split(csvSplitBy);
        adultsID = new String[500];
        for(int i=0;i<infoAdults.length;i++){
            if(infoAdults[i].isEmpty()){
                infoAdults[i]="9999";
            }
            if(i>16){
                adultsID[0]=infoAdults[16];
            }
            else{
                adultsID[0]="9999";
            }
        }
Mithilesh Gupta
  • 2,800
  • 1
  • 17
  • 17