This is my code:
这是我的代码:
public class One
{
//Loots is a class
public static Loots[] lootsList = new Loots[] { };
}
public class Two
{
Loots MyLoots = new Loots();
// How to add in THIS class, MyLoots to lootsList?
}
Please help me.
I don't want to using List!
请帮助我。我不想使用列表!
2 个解决方案
#1
0
Accessing Static variable and method of another class
访问静态变量和其他类的方法。
ClassName.StaticVariable;
OR
或
ClassName.StaticMethod;
NOTE:- Static variable and Method are active in whole Application.
注:-静态变量和方法在整个应用中都很活跃。
use this
使用这个
public class One
{
public static Loots[] lootsList = new Loots[1];
}
public class Two
{
Loots MyLoots = new Loots();
// How to add in THIS class, MyLoots to lootsList?
One.Loots[0]=MyLoots; //use this to access array of objects
}
enjoy coding .........
享受编码.........
#2
0
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);
}
}
This is a better way to do it because the ArrayList can hold as many objects as you need without it being defined by the initialization of the list.
这是一种更好的方法,因为ArrayList可以容纳任意多的对象,而不需要通过列表的初始化来定义它。
BY defining it like you did, there has to be a specific amount of Loots max in the array.
通过像你一样定义它,在数组中必须有一个特定数量的Loots max。
Alternative:
选择:
public static Loots[] lootsList = new Loots[20];
Example:
例子:
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);
}
}
#1
0
Accessing Static variable and method of another class
访问静态变量和其他类的方法。
ClassName.StaticVariable;
OR
或
ClassName.StaticMethod;
NOTE:- Static variable and Method are active in whole Application.
注:-静态变量和方法在整个应用中都很活跃。
use this
使用这个
public class One
{
public static Loots[] lootsList = new Loots[1];
}
public class Two
{
Loots MyLoots = new Loots();
// How to add in THIS class, MyLoots to lootsList?
One.Loots[0]=MyLoots; //use this to access array of objects
}
enjoy coding .........
享受编码.........
#2
0
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);
}
}
This is a better way to do it because the ArrayList can hold as many objects as you need without it being defined by the initialization of the list.
这是一种更好的方法,因为ArrayList可以容纳任意多的对象,而不需要通过列表的初始化来定义它。
BY defining it like you did, there has to be a specific amount of Loots max in the array.
通过像你一样定义它,在数组中必须有一个特定数量的Loots max。
Alternative:
选择:
public static Loots[] lootsList = new Loots[20];
Example:
例子:
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);
}
}