如何初始化自己类型的Java数组

Tom*_*Tom 2 java

我有这个

Integer[] picIDs = {             
  R.drawable.whatever
  ,
  R.drawable.example
};
String[] picDescs = {
  "test1"
  ,
  "test2"      
};    
Run Code Online (Sandbox Code Playgroud)

最初我想创建一个包含字段"id"和"desc"的数据类型的数组,但似乎不可能在声明中初始化这样的数组?(我无法以谷歌的方式去做.)这是真的吗?

Per*_*ror 7

这个怎么样?

YourType[] arr = {new YourType(12,"abc"), new YourType(13, "xyz")};
Run Code Online (Sandbox Code Playgroud)

但请确保您有一个Yourtype构造函数,它接受int(id)和String作为参数.

public class YourType{
String s;
int id;
    public YourType(int id, String s){
    this.id = id; 
    this.s = s;
    }

}
Run Code Online (Sandbox Code Playgroud)