Почему add добавляет элемент не только в конец списка, а перезаписывает все предыдущие?

 
 
 
Сообщения:7
Всем добрый день
В кратце по кусочку кода

ResultSet rs = stmt.executeQuery( "SELECT * FROM \""+vladelec+"\";" );
            while ( rs.next() ) {
                data.first_name = rs.getString("first_name");
                data.last_name = rs.getString("last_name");
                data.avatar = rs.getString("avatar");
                data.rfid = rs.getInt("rfid");
                data.date = rs.getDate("date");
                data.contact = rs.getString("contact");
                data.work_time = rs.getString("work_time");
                data.vehicles = rs.getString("vehicles");
                data.violations = rs.getString("violations");
                data.coordinates = rs.getString("coordinates");
                data.id = rs.getInt("id");

                jsonList.add(data);

                try {
                    jsonInString = mapper.writeValueAsString(jsonList);
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                    //logger.error("Failed building json "+e);
                }
                System.out.println(jsonInString);


Сделали запрос в БД
Далее,в цикле заносим данные из бд в объект data, и после этого этот обьект добавляем в jsonList. ( List<Data> jsonList = new ArrayList();)
Далее, из списка формируем json, и видим, что при добавлении объекта, предыдущие элементы также перезаписываются:

[{"first_name":"Ivan","last_name":"Ivanov","avatar":null,"rfid":1,"date":null,"contact":null,"work_time":null,"vehicles":null,"violations":null,"coordinates":null,"id":0}]
[{"first_name":"Ivan","last_name":"Ivanov","avatar":null,"rfid":2,"date":null,"contact":null,"work_time":null,"vehicles":null,"violations":null,"coordinates":null,"id":0},{"first_name":"Ivan","last_name":"Ivanov","avatar":null,"rfid":2,"date":null,"contact":null,"work_time":null,"vehicles":null,"violations":null,"coordinates":null,"id":0}]
[{"first_name":"Ivan","last_name":"Ivanov","avatar":null,"rfid":3,"date":null,"contact":null,"work_time":null,"vehicles":null,"violations":null,"coordinates":null,"id":0},{"first_name":"Ivan","last_name":"Ivanov","avatar":null,"rfid":3,"date":null,"contact":null,"work_time":null,"vehicles":null,"violations":null,"coordinates":null,"id":0},{"first_name":"Ivan","last_name":"Ivanov","avatar":null,"rfid":3,"date":null,"contact":null,"work_time":null,"vehicles":null,"violations":null,"coordinates":null,"id":0}]
[{"first_name":"Ivan","last_name":"Ivanov","avatar":null,"rfid":4,"date":null,"contact":null,"work_time":null,"vehicles":null,"violations":null,"coordinates":null,"id":0},{"first_name":"Ivan","last_name":"Ivanov","avatar":null,"rfid":4,"date":null,"contact":null,"work_time":null,"vehicles":null,"violations":null,"coordinates":null,"id":0},{"first_name":"Ivan","last_name":"Ivanov","avatar":null,"rfid":4,"date":null,"contact":null,"work_time":null,"vehicles":null,"violations":null,"coordinates":null,"id":0},{"first_name":"Ivan","last_name":"Ivanov","avatar":null,"rfid":4,"date":null,"contact":null,"work_time":null,"vehicles":null,"violations":null,"coordinates":null,"id":0}]
[{"first_name":"Ivan","last_name":"Ivanov","avatar":null,"rfid":5,"date":null,"contact":null,"work_time":null,"vehicles":null,"violations":null,"coordinates":null,"id":0},{"first_name":"Ivan","last_name":"Ivanov","avatar":null,"rfid":5,"date":null,"contact":null,"work_time":null,"vehicles":null,"violations":null,"coordinates":null,"id":0},{"first_name":"Ivan","last_name":"Ivanov","avatar":null,"rfid":5,"date":null,"contact":null,"work_time":null,"vehicles":null,"violations":null,"coordinates":null,"id":0},{"first_name":"Ivan","last_name":"Ivanov","avatar":null,"rfid":5,"date":null,"contact":null,"work_time":null,"vehicles":null,"violations":null,"coordinates":null,"id":0},{"first_name":"Ivan","last_name":"Ivanov","avatar":null,"rfid":5,"date":null,"contact":null,"work_time":null,"vehicles":null,"violations":null,"coordinates":null,"id":0}]

Тоесть, в конечном итоге json будет выглядеть так: [{"first_name":"Ivan","last_name":"Ivanov","avatar":null,"rfid":5,"date":null,"contact":null,"work_time":null,"vehicles":null,"violations":null,"coordinates":null,"id":0},{"first_name":"Ivan","last_name":"Ivanov","avatar":null,"rfid":5,"date":null,"contact":null,"work_time":null,"vehicles":null,"violations":null,"coordinates":null,"id":0},{"first_name":"Ivan","last_name":"Ivanov","avatar":null,"rfid":5,"date":null,"contact":null,"work_time":null,"vehicles":null,"violations":null,"coordinates":null,"id":0},{"first_name":"Ivan","last_name":"Ivanov","avatar":null,"rfid":5,"date":null,"contact":null,"work_time":null,"vehicles":null,"violations":null,"coordinates":null,"id":0},{"first_name":"Ivan","last_name":"Ivanov","avatar":null,"rfid":5,"date":null,"contact":null,"work_time":null,"vehicles":null,"violations":null,"coordinates":null,"id":0}]



Кто может подсказать, в чем ошибка?
 
 
Сообщения:1192
В Java объекты всегда передаются по ссылке, а примитивы - по значению.
В массив добавляются ссылки на один и тот же объект, нужно присваивать data новый объект в начале цикла.
 
 
Сообщения:7
almagnit:
В Java объекты всегда передаются по ссылке, а примитивы - по значению.
В массив добавляются ссылки на один и тот же объект, нужно присваивать data новый объект в начале цикла.


Благодарю за ответ
 
Модераторы:Нет
Сейчас эту тему просматривают:Нет