奇怪的数组返回类型
- 论坛
- 奇怪的数组返回类型
8 浏览
奇怪的数组返回类型
有人见过像这样在方法签名后面放置的数组[]
吗?
public static String mySplit(String s)[] { return s.split(","); } public static void main(String... args) { String[] words = mySplit("a,b,c,d,e"); System.out.println(Arrays.toString(words)); }
打印结果为
[a, b, c, d, e]
以前,奇怪的符号可能是为了与"C"兼容,但我也不会想象有人会在C中写这样的代码。
有人知道为什么这样做是被允许的吗?
我使用的是Java 7更新10版本,如果有关系的话。
在Java 6中也会产生相同的结果。 http://ideone.com/91rZV1
顺便说一下,这个代码不能编译,我也不指望它会编译。
public staticList mySplit(String s) { return Collections.emptyList(); }