public class One{
public List<Loots> lootsList = new ArrayList<Loots>();
public void someVoid(){
new Two(this);
}
}
public class Two
{
One one;
public Two(One one){
this.one = one:
}
Loots MyLoots = new Loots();
// How to add in THIS class, MyLoots to lootsList?
public void someVoid(){
one.lootsList.add(MyLoots);
}
}
Dies ist eine bessere Art und Weise, es zu tun, weil die Arraylist so viele Objekte wie Sie halten können brauchen, ohne dass es durch die Initialisierung der Liste definiert wird.
Wenn Sie es so definieren, wie Sie es getan haben, muss eine bestimmte Anzahl von Loots max im Array vorhanden sein.
Alternative:
public static Loots[] lootsList = new Loots[20];
Beispiel:
public class One{
public static Loots[] lootsList = new Loots[20];
public void someVoid(){
new Two(this);
}
}
public class Two
{
One one;
public Two(One one){
this.one = one:
}
Loots MyLoots = new Loots();
// How to add in THIS class, MyLoots to lootsList?
public void someVoid(){
one.lootsList[0].add(MyLoots);
}
}
Verwenden 'list' oder' ArrayList' statt. Viel einfacher. – SripadRaj