这听起来很容易,但我一直试图这样做, I want to initialize my custom class object array using curly braces
这是失败的例子:
类:
class Tranforminfo{
int left;
int top;
int right;
int bottom;
float rorate;
public Tranforminfo(int left, int top, int right, int bottom, float rorate) {
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
this.rorate = rorate;
}
}
Run Code Online (Sandbox Code Playgroud)
用法:(不正确)
// attempt 1
Tranforminfo somedamn = new Tranforminfo[]{(1,2,3,4,5),(6,4,3,5,6)};
// attempt 2
Tranforminfo somedamn = new Tranforminfo[]{{1,2,3,4,5},{6,4,3,5,6}};
// attempt 3
Tranforminfo somedamn = new Tranforminfo[]((1,2,3,4,5),(6,4,3,5,6));
Run Code Online (Sandbox Code Playgroud)
到目前为止没有运气帮助赞赏,我在android(JAVA)编码
有一些方法可以做到:
Transforminfo[] somedamn =
new Transforminfo[] { new Transforminfo(1,2,3,4,5),
new Transforminfo(6,4,3,5,6) };
Transforminfo[] somedamn =
{ new Transforminfo(1,2,3,4,5), new Transforminfo(6,4,3,5,6) };
Run Code Online (Sandbox Code Playgroud)
首先创建Transforminfo数组链接,然后添加新Transforminfo元素.
就像Integer []array = {1, 2, 3},但你必须使用构造函数来创建Transforminfo元素.
另一个了解对象创建数组的示例.一切都是平等的.
String array[] = { new String("str1"), new String("str2") };
String[] array = { new String("str1"), new String("str2") };
String array[] = new String[] { new String("str1"), new String("str2") };
String[] array = new String[] { new String("str1"), new String("str2") };
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14258 次 |
| 最近记录: |