Я действительно не знаю, что я здесь делаю неправильно. Я собираю некоторые данные в массив массивов строк. В конце я получаю дубликаты ответов вместо того, что мне нравится.
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, массиве, который собирает данные, одинаковы.