0

I really don't know what am I doing wrong here. I am gathering some data into an array of arrays of strings. In the end I get a duplicate answers instead of what I aspect.

final ArrayList<List<String>> FoundRides = new ArrayList<>();
                        FoundRides.clear();
                        for ( int i=0; i< array.length(); i++){FoundRides.add(Arrays.asList("Ankit","Bohra","Xyz")); };
final ArrayList<String> FLines = new ArrayList<String>();

                        for ( int i=0; i< array.length(); i++){
                                JSONObject line = array.getJSONObject(i);

                                FLines.clear();
                                FLines.add(line.getString("destinatie"));
                                FLines.add(line.getString("kmride"));
                                FLines.add(line.getString("created_at"));

                    //  FoundRides.set(i,FLines);                                      
                    FoundRides.add(0,FLines);


                            }

From Logs:

With FoundRides.add(0,FLines);

Initialize AllRides = [[Ankit, Bohra, Xyz], [Ankit, Bohra, Xyz], [Ankit, Bohra, Xyz]]  
INSIDE FOR FLines = [rds, 232, 2018-05-11 00:52:38]  
INSIDE FOR Counter = 0  
INSIDE FOR AllRides FoundRides = [[rds, 232, 2018-05-11 00:52:38], [Ankit, Bohra, Xyz], [Ankit, Bohra, Xyz], [Ankit, Bohra, Xyz]]  
INSIDE FOR FLines = [rds, 333, 2017-11-08 21:04:24]  
INSIDE FOR Counter = 1  
INSIDE FOR AllRides FoundRides = [[rds, 333, 2017-11-08 21:04:24], [rds, 333, 2017-11-08 21:04:24], [Ankit, Bohra, Xyz], [Ankit, Bohra, Xyz], [Ankit, Bohra, Xyz]]  
INSIDE FOR FLines = [rds, 111, 2017-11-02 23:46:08]  
INSIDE FOR Counter = 2  
INSIDE FOR AllRides FoundRides = [[rds, 111, 2017-11-02 23:46:08], [rds, 111, 2017-11-02 23:46:08], [rds, 111, 2017-11-02 23:46:08], [Ankit, Bohra, Xyz], [Ankit, Bohra, Xyz], [Ankit, Bohra, Xyz]]  
OUTSIDE FOR AllRides = [[rds, 111, 2017-11-02 23:46:08], [rds, 111, 2017-11-02 23:46:08], [rds, 111, 2017-11-02 23:46:08], [Ankit, Bohra, Xyz], [Ankit, Bohra, Xyz], [Ankit, Bohra, Xyz]]  

With FoundRides.set(i,FLines);

  Initialize AllRides = [[Ankit, Bohra, Xyz], [Ankit, Bohra, Xyz], [Ankit, Bohra, Xyz]]  
  INSIDE FOR FLines = [rds, 232, 2018-05-11 00:52:38]  
  INSIDE FOR Counter = 0  
  INSIDE FOR AllRides FoundRides = [[rds, 232, 2018-05-11 00:52:38], [Ankit, Bohra, Xyz], [Ankit, Bohra, Xyz]]  
  INSIDE FOR FLines = [rds, 333, 2017-11-08 21:04:24]  
  INSIDE FOR Counter = 1  
  INSIDE FOR AllRides FoundRides = [[rds, 333, 2017-11-08 21:04:24], [rds, 333, 2017-11-08 21:04:24], [Ankit, Bohra, Xyz]]  
  INSIDE FOR FLines = [rds, 111, 2017-11-02 23:46:08]  
  INSIDE FOR Counter = 2  
  INSIDE FOR AllRides FoundRides = [[rds, 111, 2017-11-02 23:46:08], [rds, 111, 2017-11-02 23:46:08], [rds, 111, 2017-11-02 23:46:08]]  

  OUTSIDE FOR AllRides = [[rds, 111, 2017-11-02 23:46:08], [rds, 111, 2017-11-02 23:46:08], [rds, 111, 2017-11-02 23:46:08]]  

Why am I getting in the end the last result in all three positions ? Each iteration from the for FLines is getting the result I am expecting but on the last one, all the lines in the FoundRides, the array that gathers the data, are the same.

dave
  • 575
  • 4
  • 19
P.Bogdan
  • 13
  • 5

1 Answers1

0

I am very sorry for the duplicate, for some reason I didn't find the answer in two days of search so I open a new one.

Here : Why does my ArrayList contain N copies of the last item added to the list?

This pretty much explains why I got the error.

P.Bogdan
  • 13
  • 5