如何更新数组列表数据值?

时间:2017-03-07 20:58:24

标签: java arraylist

我有以下课程,其中保留了我的arraylist:

import java.util.ArrayList;
import java.util.Random;

public class Theatre {

private ArrayList<Room> theatres;

public Theatre(){
    theatres = new ArrayList<Room>();
    theatres.add(new Room("Theatre 1", 120));
    theatres.add(new Room("Theatre 2", 180));
    theatres.add(new Room("Theatre 3", 100));
    theatres.add(new Room("Theatre 4", 120));
    theatres.add(new Room("Theatre 5", 200));
    theatres.add(new Room("Theatre 6", 180));
    theatres.add(new Room("Theatre 7", 80));
    theatres.add(new Room("Theatre 8", 50));
    theatres.add(new Room("Theatre 9", 120));
    theatres.add(new Room("Theatre 10", 150));

}

public void addTheatre(String theatreName, int seatsAvailable){
    Room t = new Room(theatreName, seatsAvailable);
}

public void printTheatres(){
    for (int index=0; index<theatres.size(); index++)
    {
    System.out.println(theatres.get(index));
}
   }

public void updateSeats(){
    int numSelected = Basket.seatsBooked;
    int userFilmSelection = MainActivity.filmSelectionNumber;

    theatres.get(userFilmSelection).setSeatsAvailable(theatres.get(userFilmSelection).getSeatsAvailable() - numSelected);
}


public int getSeats(){
    int theatre = 0;
    int userFilmSelection = MainActivity.filmSelectionNumber;

    theatre = theatres.get(userFilmSelection).getSeatsAvailable();
    Basket.theatre+=theatres.get(userFilmSelection).getTheatreName();

    return theatre;
}

}

我想要一种方法来更新这个arraylist中的席位,当它们从我的GUI类添加到篮子中时。我怎样才能做到这一点。在我的GUI中,我有以下内容,但每次返回MainActivity订单屏幕时,它似乎都会失去座位数。我想这是因为我每次创建一个新的剧院对象,但不知道如何解决这个问题。

我需要以下方法在更新后返回当前可用座位数,以便输出到GUI。

public int getSeats(){
    Theatre t = new Theatre();
    t.updateSeats();
    return t.getSeats();
}

代码有效,因为我在java IDE中尝试过这个名为blue j的东西,它允许我看到对象但不在这里,很可能是因为我每次都创建新对象。

更新 这应该返回更新的座位数,但不是。

public int getSeats(){
        Theatre.updateSeats();
        return Theatre.getSeats();
    }

1 个答案:

答案 0 :(得分:0)

如果getSeats()Theatre方法,那么

public int getSeats(){
    Theatre t = new Theatre();
    t.updateSeats();
    return t.getSeats();
}

每次调用都会创建一个新实例,因此每次都使用新数据 您首先发布的方法

public int getSeats(){
    int theatre = 0;
    int userFilmSelection = MainActivity.filmSelectionNumber;

    theatre = theatres.get(userFilmSelection).getSeatsAvailable();
    Basket.theatre+=theatres.get(userFilmSelection).getTheatreName();

    return theatre;
}
如果在updateSeats()之后调用,

似乎很好 我不确定我是否完全理解你的问题。

好的,如果您需要Room始终可用,您可能需要将List声明为static

private static final List<Room> theatres;

static {
   theatres = new ArrayList<>(10);
   theatres.add(new Room("Theatre 1", 120));
   theatres.add(new Room("Theatre 2", 180));
   theatres.add(new Room("Theatre 3", 100));
   theatres.add(new Room("Theatre 4", 120));
   theatres.add(new Room("Theatre 5", 200));
   theatres.add(new Room("Theatre 6", 180));
   theatres.add(new Room("Theatre 7", 80));
   theatres.add(new Room("Theatre 8", 50));
   theatres.add(new Room("Theatre 9", 120));
   theatres.add(new Room("Theatre 10", 150));
}

public Theatre(){
   // Nothing here
}

我重写了你的所作所为,看看你是否明白。

public class Room
{
   private String _name;
   private int _seats;

   public Room(final String name, final int seats) {
      _name = name;
      _seats = seats;
   }

   public String getName() {
      return _name;
   }

   public int getAvailableSeats() {
      return _seats;
   }

   public void setName(final String name) {
      _name = name;
   }

   public void setAvailableSeats(final int seats) {
      _seats = seats;
   }
}

public class Theatre
{
   private static final List<Room> ROOMS;

   static {
      ROOMS = new ArrayList<Room>(10);
      ROOMS.add(new Room("Room 1", 120));
      ROOMS.add(new Room("Room 2", 180));
      ROOMS.add(new Room("Room 3", 100));
      ROOMS.add(new Room("Room 4", 120));
      ROOMS.add(new Room("Room 5", 200));
      ROOMS.add(new Room("Room 6", 180));
      ROOMS.add(new Room("Room 7", 80));
      ROOMS.add(new Room("Room 8", 50));
      ROOMS.add(new Room("Room 9", 120));
      ROOMS.add(new Room("Room 10", 150));
   }

   public static void addRoom(final String roomName, final int seatsAvailable) {
      ROOMS.add(new Room(roomName, seatsAvailable));
   }

   public static void printRooms() {
      for (final Room room : ROOMS) {
         System.out.println(room.getName());
      }
   }

   public static void updateSeats() {
      final Room room = ROOMS.get(MainActivity.filmSelectionNumber);
      room.setAvailableSeats(room.getAvailableSeats() - Basket.seatsBooked);
   }

   public static int getSeats() {
      final Room room = ROOMS.get(MainActivity.filmSelectionNumber);
      Basket.theatre += room.getName();

      return room.getAvailableSeats();
   }
}