黑马程序员技术交流社区
标题:
Java (数组的遍历,for循环的使用)
[打印本页]
作者:
cyl571589562
时间:
2017-12-27 18:31
标题:
Java (数组的遍历,for循环的使用)
[backcolor=rgba(128, 128, 128, 0.0470588)]
/*多行注释的快捷键:Ctrl+shift+/快速格式化代码快捷键:Ctrl+shift+f自动导入一个包:Ctrl+shift+o*/
[backcolor=rgba(128, 128, 128, 0.0470588)]
package test_1;
[backcolor=rgba(128, 128, 128, 0.0470588)]
public
[backcolor=rgba(128, 128, 128, 0.0470588)]
[backcolor=rgba(128, 128, 128, 0.0470588)]
class
[backcolor=rgba(128, 128, 128, 0.0470588)]
Day_2 {
[backcolor=rgba(128, 128, 128, 0.0470588)]
public
[backcolor=rgba(128, 128, 128, 0.0470588)]
[backcolor=rgba(128, 128, 128, 0.0470588)]
static
[backcolor=rgba(128, 128, 128, 0.0470588)]
[backcolor=rgba(128, 128, 128, 0.0470588)]
void
[backcolor=rgba(128, 128, 128, 0.0470588)]
[backcolor=rgba(128, 128, 128, 0.0470588)]
main
[backcolor=rgba(128, 128, 128, 0.0470588)]
(String args[]) {
[backcolor=rgba(128, 128, 128, 0.0470588)]
//一个九九乘法表的实现
[backcolor=rgba(128, 128, 128, 0.0470588)]
[backcolor=rgba(128, 128, 128, 0.0470588)]
int
[backcolor=rgba(128, 128, 128, 0.0470588)]
c =
[backcolor=rgba(128, 128, 128, 0.0470588)]
0
[backcolor=rgba(128, 128, 128, 0.0470588)]
;
[backcolor=rgba(128, 128, 128, 0.0470588)]
for
[backcolor=rgba(128, 128, 128, 0.0470588)]
(
[backcolor=rgba(128, 128, 128, 0.0470588)]
int
[backcolor=rgba(128, 128, 128, 0.0470588)]
a =
[backcolor=rgba(128, 128, 128, 0.0470588)]
1
[backcolor=rgba(128, 128, 128, 0.0470588)]
; a <=
[backcolor=rgba(128, 128, 128, 0.0470588)]
9
[backcolor=rgba(128, 128, 128, 0.0470588)]
; a++) {
[backcolor=rgba(128, 128, 128, 0.0470588)]
for
[backcolor=rgba(128, 128, 128, 0.0470588)]
(
[backcolor=rgba(128, 128, 128, 0.0470588)]
int
[backcolor=rgba(128, 128, 128, 0.0470588)]
b =
[backcolor=rgba(128, 128, 128, 0.0470588)]
1
[backcolor=rgba(128, 128, 128, 0.0470588)]
; b <= a; b++) { c = a * b; System.
[backcolor=rgba(128, 128, 128, 0.0470588)]
out
[backcolor=rgba(128, 128, 128, 0.0470588)]
.printf(
[backcolor=rgba(128, 128, 128, 0.0470588)]
"%d*%d=%d "
[backcolor=rgba(128, 128, 128, 0.0470588)]
,b,a,c); } System.
[backcolor=rgba(128, 128, 128, 0.0470588)]
out
[backcolor=rgba(128, 128, 128, 0.0470588)]
.println(); } System.
[backcolor=rgba(128, 128, 128, 0.0470588)]
out
[backcolor=rgba(128, 128, 128, 0.0470588)]
.println(
[backcolor=rgba(128, 128, 128, 0.0470588)]
"-------------------我是华丽的分割线--------------------------"
[backcolor=rgba(128, 128, 128, 0.0470588)]
);
[backcolor=rgba(128, 128, 128, 0.0470588)]
// 遍历数组的方法
[backcolor=rgba(128, 128, 128, 0.0470588)]
[backcolor=rgba(128, 128, 128, 0.0470588)]
// 方法1:
[backcolor=rgba(128, 128, 128, 0.0470588)]
[backcolor=rgba(128, 128, 128, 0.0470588)]
int
[backcolor=rgba(128, 128, 128, 0.0470588)]
arr[] =
[backcolor=rgba(128, 128, 128, 0.0470588)]
new
[backcolor=rgba(128, 128, 128, 0.0470588)]
[backcolor=rgba(128, 128, 128, 0.0470588)]
int
[backcolor=rgba(128, 128, 128, 0.0470588)]
[
[backcolor=rgba(128, 128, 128, 0.0470588)]
3
[backcolor=rgba(128, 128, 128, 0.0470588)]
];
[backcolor=rgba(128, 128, 128, 0.0470588)]
for
[backcolor=rgba(128, 128, 128, 0.0470588)]
(
[backcolor=rgba(128, 128, 128, 0.0470588)]
int
[backcolor=rgba(128, 128, 128, 0.0470588)]
a =
[backcolor=rgba(128, 128, 128, 0.0470588)]
0
[backcolor=rgba(128, 128, 128, 0.0470588)]
; a < arr.length; a++) { System.
[backcolor=rgba(128, 128, 128, 0.0470588)]
out
[backcolor=rgba(128, 128, 128, 0.0470588)]
.println(arr[a]); } System.
[backcolor=rgba(128, 128, 128, 0.0470588)]
out
[backcolor=rgba(128, 128, 128, 0.0470588)]
.println(
[backcolor=rgba(128, 128, 128, 0.0470588)]
"-------------------我是华丽的分割线--------------------------"
[backcolor=rgba(128, 128, 128, 0.0470588)]
);
[backcolor=rgba(128, 128, 128, 0.0470588)]
//方法2:
[backcolor=rgba(128, 128, 128, 0.0470588)]
[backcolor=rgba(128, 128, 128, 0.0470588)]
for
[backcolor=rgba(128, 128, 128, 0.0470588)]
(
[backcolor=rgba(128, 128, 128, 0.0470588)]
int
[backcolor=rgba(128, 128, 128, 0.0470588)]
a : arr) { System.
[backcolor=rgba(128, 128, 128, 0.0470588)]
out
[backcolor=rgba(128, 128, 128, 0.0470588)]
.println(a); } System.
[backcolor=rgba(128, 128, 128, 0.0470588)]
out
[backcolor=rgba(128, 128, 128, 0.0470588)]
.println(
[backcolor=rgba(128, 128, 128, 0.0470588)]
"-------------------我是华丽的分割线--------------------------"
[backcolor=rgba(128, 128, 128, 0.0470588)]
);
[backcolor=rgba(128, 128, 128, 0.0470588)]
//二维数组的遍历
[backcolor=rgba(128, 128, 128, 0.0470588)]
[backcolor=rgba(128, 128, 128, 0.0470588)]
int
[backcolor=rgba(128, 128, 128, 0.0470588)]
arr2[][]=
[backcolor=rgba(128, 128, 128, 0.0470588)]
new
[backcolor=rgba(128, 128, 128, 0.0470588)]
[backcolor=rgba(128, 128, 128, 0.0470588)]
int
[backcolor=rgba(128, 128, 128, 0.0470588)]
[][]{ {
[backcolor=rgba(128, 128, 128, 0.0470588)]
1
[backcolor=rgba(128, 128, 128, 0.0470588)]
,
[backcolor=rgba(128, 128, 128, 0.0470588)]
2
[backcolor=rgba(128, 128, 128, 0.0470588)]
,
[backcolor=rgba(128, 128, 128, 0.0470588)]
3
[backcolor=rgba(128, 128, 128, 0.0470588)]
}, {
[backcolor=rgba(128, 128, 128, 0.0470588)]
4
[backcolor=rgba(128, 128, 128, 0.0470588)]
,
[backcolor=rgba(128, 128, 128, 0.0470588)]
5
[backcolor=rgba(128, 128, 128, 0.0470588)]
,
[backcolor=rgba(128, 128, 128, 0.0470588)]
6
[backcolor=rgba(128, 128, 128, 0.0470588)]
}, {
[backcolor=rgba(128, 128, 128, 0.0470588)]
7
[backcolor=rgba(128, 128, 128, 0.0470588)]
,
[backcolor=rgba(128, 128, 128, 0.0470588)]
8
[backcolor=rgba(128, 128, 128, 0.0470588)]
,
[backcolor=rgba(128, 128, 128, 0.0470588)]
9
[backcolor=rgba(128, 128, 128, 0.0470588)]
} };
[backcolor=rgba(128, 128, 128, 0.0470588)]
for
[backcolor=rgba(128, 128, 128, 0.0470588)]
(
[backcolor=rgba(128, 128, 128, 0.0470588)]
int
[backcolor=rgba(128, 128, 128, 0.0470588)]
i=
[backcolor=rgba(128, 128, 128, 0.0470588)]
0
[backcolor=rgba(128, 128, 128, 0.0470588)]
;i<arr2.length;i++){
[backcolor=rgba(128, 128, 128, 0.0470588)]
for
[backcolor=rgba(128, 128, 128, 0.0470588)]
(
[backcolor=rgba(128, 128, 128, 0.0470588)]
int
[backcolor=rgba(128, 128, 128, 0.0470588)]
j=
[backcolor=rgba(128, 128, 128, 0.0470588)]
0
[backcolor=rgba(128, 128, 128, 0.0470588)]
;j<arr2[
[backcolor=rgba(128, 128, 128, 0.0470588)]
0
[backcolor=rgba(128, 128, 128, 0.0470588)]
].length;j++){ System.
[backcolor=rgba(128, 128, 128, 0.0470588)]
out
[backcolor=rgba(128, 128, 128, 0.0470588)]
.print(arr2
[j]+
[backcolor=rgba(128, 128, 128, 0.0470588)]
" "
[backcolor=rgba(128, 128, 128, 0.0470588)]
); } System.
[backcolor=rgba(128, 128, 128, 0.0470588)]
out
[backcolor=rgba(128, 128, 128, 0.0470588)]
.println(); } }}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2