私は私の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;
}
}
を彼らは私のGUIクラスからバスケットに追加されたとき、私は、このArrayListの中に席を更新する方法をしたいと思います。これどうやってするの。私のGUIでは次のようなことがありますが、MainActivityの注文画面に戻るたびに座席数が減るようです。これは、毎回新しいTheaterオブジェクトを作成するためだが、これを回避する方法はわからないからだと思う。
私はGUIに出力できるように、更新された後に利用可能な現在の座席数を返すために以下のメソッドが必要です。
public int getSeats(){
Theatre t = new Theatre();
t.updateSeats();
return t.getSeats();
}
私はイムは、新しいオブジェクトを毎回作成するので、私はここで、オブジェクトが、doesntのを見ることができますし、最も可能性の高い青色のJと呼ばれるJavaのIDEでそれを試してみましたので、コードは動作します。
更新 これは更新されたシート数を返すはずですが、そうではありません。 getSeats()
もし
public int getSeats(){
Theatre.updateSeats();
return Theatre.getSeats();
}
は '「私はイムは、新しい劇場のオブジェクトごとに作成しているため、これがあると思いますが、これを回避する方法を知ってはいけない。」' - おそらく非静的プライベートシアターフィールドを作成し、それだけを扱います。 –