day05-数组的遍历
This commit is contained in:
25
day05/src/com/inmind/Demo03_array_foreach.java
Normal file
25
day05/src/com/inmind/Demo03_array_foreach.java
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package com.inmind;
|
||||||
|
/*
|
||||||
|
11.数组的操作_数组的遍历
|
||||||
|
遍历:将数组中每个数据,都一一展示出来
|
||||||
|
*/
|
||||||
|
public class Demo03_array_foreach {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//定义一个具体值为1,2,3的整数数组
|
||||||
|
int[] arr = {1,2,3,5,6,7,78,8,9};//50个值
|
||||||
|
//简单操作:直接根据索引获取值即可
|
||||||
|
/*System.out.println(arr[0]);
|
||||||
|
System.out.println(arr[1]);
|
||||||
|
System.out.println(arr[2]);*/
|
||||||
|
//实际操作:使用for循环实现遍历(0~arr.length-1)
|
||||||
|
//正向遍历
|
||||||
|
for (int i = 0; i < arr.length; i++) {
|
||||||
|
System.out.println(arr[i]);
|
||||||
|
}
|
||||||
|
System.out.println("-----------------------------------");
|
||||||
|
//反向遍历 最大索引~0
|
||||||
|
for (int i = arr.length-1; i >=0; i--) {
|
||||||
|
System.out.println(arr[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user