A:当不明确数组中的元素时,动态初始化 举例 :
1
2
|
int
[] arr =
new
int
[
3
];
arr[
0
] =
3
;
|
1
2
|
int
[] arr =
new
int
[]{
1
,
2
,
3
,
4
,
5
};
int
[] arr = {
1
,
2
,
3
,
4
,
5
};
|
1
2
3
4
|
int
-->
0
double
-->
0.0
boolean
-->
false
char
-->
'\u0000'
空字符,unicode编码。
|
举例
1
2
|
int
[] arr =
new
int
[
3
];
System.out.println(arr[
3
]);
|
举例
2
3
|
int
[] arr =
new
int
[
3
];
arr =
null
;
System.out.println(arr[
1
]);
|
1
2
3
4
5
6
7
8
9
|
int
[][] arr2 =
new
int
[
2
][
3
];
public
static
void
printArray2(
int
[][] arr2){
for
(
int
i=
0
; i
for
(
int
j=
0
; j
System.out.print(arr2[i][j]+
" "
);
}
System.out.println();
}
}
|
求一年的销售总额。
1
2
3
4
5
6
7
8
9
|
public
static
void
printArray2(
int
[][] arr2){
int
sum =
0
;
for
(
int
i=
0
; i
for
(
int
j=
0
; j
sum = sum + arr[i][j];
}
System.out.println(
"sum ="
+ sum);
}
}
|