这怎么非法 [英] How is this illegal

查看:57
本文介绍了这怎么非法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

a) int i[] = new int[2]{1,2};
b) int[] i = new int[]{1,2,3}{4,5,6}; 

我知道我们不能在声明时给出数组的大小.但是在语句(a)中,我们在初始化中给出了size.那么在Java中语句(a)是非法的而语句(b)是合法的

I know we cannot give size of an array at declaration . But in statement(a) we are giving size in initialization . Then how is statement (a) illegal and statement (b) is legal in java

推荐答案

int i1 [] = new int [2] {1, 2}; // Invalid
int i2 [] = new int [] {1, 2}; // Valid
int [] i3 = new int [][] {1, 2, 3} {4, 5, 6}; // Invalid
int [][] i4 = new int [][] {new int [] {1, 2, 3}, new int [] {4, 5, 6}}; // Valid

这篇关于这怎么非法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆