Ket Ket - 6 months ago 12
Java Question

Update/Change value in a ArrayList

I am still quite new to java programming and I am looking for some help with my java exercise, currently I am having trouble to figure out how to update a value in a ArrayList.

I have used JAXBinding to generate some source files that I can use it to create XML file.

public static void main (String[] args) {
String delete;
AvailableFlights todayFlight = new AvailableFlights();
List<Flight> flights_today = todayFlight.getFlightDetail();

Flight journey1;
journey1 = new Flight();
journey1.setJourney("J1");
journey1.setAirline("KLM");
journey1.setConnections(1);
journey1.setDestination("Amsterdam");
journey1.setOrigin("London");
journey1.setSeats(10);
journey1.setCost(200);
journey1.setCurrency((float) 0.16);
flights_today.add(journey1);


The problem I am finding that is if I want to update the
Seats
value, I would do something like this to take away a seat:

if(delete.equals("J1")) {
Flight journey1;
flights_today.add(journey1.setSeats(-1));
flights_today.add(journey1);
}


But it just overwrites it with -1 instead of taking 1 seat off from 10.

Problem Solved with :
journey1.setSeats(journey1.getSeats()-1);

Answer

Instead of using:

journey1.setSeats(-1)

You should use:

journey1.setSeats(journey1.getSeats()-1)

setSeats(-1) will set the number of seats to -1, you should specify the actual value instead.

Comments