私はちょっと混乱しています。たとえば、データベースを作成したり、クラス内で変数を作り、クラス内のいくつかのメソッドは、いくつかの機能を作ります。メソッドでOOPクラスのメンバを変更できません
このクラスにはDataRegisterという名前が付けられています。公開メンバーは1人で、ArrayList
です。「追加」、「削除」、「ClearData」、「GetData」の4つのメソッドもあります。
public ArrayList<Data> data;
public DataRegister(){
if(data == null){
data = new ArrayList<Data>();
}
}
public void addData(Data c){
data.add(c);
System.out.println("Added "+c.getName());
System.out.println(data.size());
}
public void clearRegister(){
data.clear();
System.out.println("Cleared Data Register");
}
public void removeData(Data c){
data.remove(c);
}
public ArrayList<Data> getData(){
System.out.println(data.size());
return data;
}
それは完全にそれが事、 だんが、私はそれに新しいデータオブジェクトを追加するとき、それは名前(データの方法でアクセスされるデータのクラスのメンバ)の言うデータレジスタiの型の新しいオブジェクトを作成しますArrayListに1つのオブジェクトがあるので、getData()
を実行したときにからsysout
へと変更されたので、これはArrayList
が空であることを意味しますが、データオブジェクトを追加したばかりですもう1つは、ArrayList
の長さが1であると言って、前の番号を忘れてしまいました。
私は追加し、次の例から取得しています:
//This class implements Runnable and is used as thread.
//making it so every function in the class can read and edit this var.
private DataRegister dr;
//this runs when the user decides to make a new Database
//the user can max. have 1 DB
//only fires if dr == null
public void makeDB(){
dr = new DataRegister();
dr.clearRegister();
}
//this runs whenever the user decides to remove the database.
//the user is unable to user this function atm.
//only fires if dr != null
public void removeDB(){
dr = null;
}
//fires whenever the user wants to create a new object of type Data
public void createData(){
...code code code and more code
//user creates the object by entering info in a GUI and when the user
//presses "confirm" the code will read the info and make a new Object of
//type Data in a var _tempData.
dr.addData(_tempData);
}
public void drawContent(){
... does a lot of things with the GUI
ArrayList<Data> _data = dr.getData();
System.out.println(_data.size());
//this outputs 0 2 times because the getData() also prints it in console
}
これは、実際のクラスではなく、あくまでも一例を与えます。
どのようにDataRegister' 'のオブジェクトを作成し、どのようにされています'getData'メソッドを使って値を追加して取得していますか? – SMA
[問題を再現する完全で実証可能な最小限の例](http://stackoverflow.com/help/mcve)を追加してください。あなたが持っているのは、クラス全体またはそれを呼び出すコード(問題の原因となる可能性が高い)を含んでいない不完全なスニペットです。 –
Plsは失敗したコードの例を追加します。あなたはクラスコードのみを記述しましたが、このクラスを使用して記述しているコードは表示されません。 – SimpleGuy