多维数组列表奇怪的行为

时间:2018-05-15 14:04:41

标签: arrays string list set add

我真的不知道我在这里做错了什么。我正在将一些数据收集到一个字符串数组中。最后,我得到了一个重复的答案,而不是我的方面。

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);


                            }

来自日志:

使用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]]  

使用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]]  

为什么我最终得到所有三个位置的最后结果?来自for FLines的每次迭代都得到了我期望的结果,但是在最后一次,FoundRides中的所有行,即收集数据的数组都是相同的。

1 个答案:

答案 0 :(得分:0)

我很抱歉这些副本,由于某种原因,我在搜索的两天内找不到答案,所以我开了一个新的。

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

这很好地解释了我收到错误的原因。