I just looked at this SO Post:
我刚刚看了这篇SO帖子:
However, the Columbia professor's notes does it the way below. See page 9.
但是,哥伦比亚大学教授的笔记就是这样做的。见第9页。
Foo foos = new Foo[12] ;
Which way is correct? They seem to say different things.
哪种方式正确?他们似乎说不同的事情。
Particularly, in the notes version there isn't []
.
特别是,在笔记版本中没有[]。
5 个解决方案
#1
7
This simply won't compile in Java (because you're assigning a value of an array type to a variable of a the non-array type Foo
):
这根本不会用Java编译(因为你要将数组类型的值赋给非数组类型Foo的变量):
Foo foos = new Foo[12];
it's rejected by javac
with the following error (See also: http://ideone.com/0jh9YE):
它被javac拒绝并出现以下错误(另请参阅:http://ideone.com/0jh9YE):
test.java:5: error: incompatible types
Foo foos = new Foo[12];
To have it compile, declare foo
to be of type Foo[]
and then just loop over it:
要让它编译,将foo声明为Foo []类型,然后循环它:
Foo[] foo = new Foo[12]; # <<<<<<<<<
for (int i = 0; i < 12; i += 1) {
foos[i] = new Foo();
}
#2
1
Foo[] foos = new Foo[12] ; //declaring array
for(int i=0;i<12;i++){
foos[i] = new Foo(); //initializing the array with foo object
}
#3
1
You can't do this
你不能这样做
Foo foos = new Foo[12] ;
change to
Foo[] foos = new Foo[12];
there was a typo in the document on page 9. Also there's a typo on page 10
第9页的文档中有一个拼写错误。第10页也有拼写错误
int[] grades = new int[3]
I would not read the whole document if the typos are on each page.
如果每页都有拼写错误,我就不会阅读整篇文档。
#4
0
Declare by this way.
通过这种方式宣布。
Foo[] foos = new Foo[12];
#5
0
//declaring array of 12 Foo elements in Java8 style
Foo[] foos = Stream.generate(Foo::new).limit(12).toArray(Foo[]::new);
// instead of
Foo[] foos = new Foo[12];
for(int i=0;i<12;i++){
foos[i] = new Foo();
}
#1
7
This simply won't compile in Java (because you're assigning a value of an array type to a variable of a the non-array type Foo
):
这根本不会用Java编译(因为你要将数组类型的值赋给非数组类型Foo的变量):
Foo foos = new Foo[12];
it's rejected by javac
with the following error (See also: http://ideone.com/0jh9YE):
它被javac拒绝并出现以下错误(另请参阅:http://ideone.com/0jh9YE):
test.java:5: error: incompatible types
Foo foos = new Foo[12];
To have it compile, declare foo
to be of type Foo[]
and then just loop over it:
要让它编译,将foo声明为Foo []类型,然后循环它:
Foo[] foo = new Foo[12]; # <<<<<<<<<
for (int i = 0; i < 12; i += 1) {
foos[i] = new Foo();
}
#2
1
Foo[] foos = new Foo[12] ; //declaring array
for(int i=0;i<12;i++){
foos[i] = new Foo(); //initializing the array with foo object
}
#3
1
You can't do this
你不能这样做
Foo foos = new Foo[12] ;
change to
Foo[] foos = new Foo[12];
there was a typo in the document on page 9. Also there's a typo on page 10
第9页的文档中有一个拼写错误。第10页也有拼写错误
int[] grades = new int[3]
I would not read the whole document if the typos are on each page.
如果每页都有拼写错误,我就不会阅读整篇文档。
#4
0
Declare by this way.
通过这种方式宣布。
Foo[] foos = new Foo[12];
#5
0
//declaring array of 12 Foo elements in Java8 style
Foo[] foos = Stream.generate(Foo::new).limit(12).toArray(Foo[]::new);
// instead of
Foo[] foos = new Foo[12];
for(int i=0;i<12;i++){
foos[i] = new Foo();
}