编程语言
首页 > 编程语言> > Java10-5_数组的使用

Java10-5_数组的使用

作者:互联网

数组的使用

Practice 1

For循环打印全部数组元素

 1 package com.Xujie.Day10;
 2 
 3 public class Java10_03 {
 4     public static void main(String[] args) {
 5         int[] arrays={1,2,3,4,5};
 6         for (int i = 0; i < arrays.length; i++) {
 7             System.out.println(arrays[i]);
 8         }
 9     }
10 }

 

 

Practice 2

For循环计算所有数组元素之和

 1 package com.Xujie.Day10;
 2 
 3 public class Java10_04 {
 4     public static void main(String[] args) {
 5         int[] array={1,2,3,4,5};
 6         int sum=0;
 7         for (int i = 0; i < array.length; i++) {
 8             sum+=array[i];
 9         }
10         System.out.println(sum);
11     }
12 }

 

 

Practice 3

For循环查找数组中最大元素

 1 package com.Xujie.Day10;
 2 
 3 public class Java10_05 {
 4     public static void main(String[] args) {
 5         int[] arrays={1,2,3,4,5};
 6         int max=arrays[0];
 7         for (int i = 1; i < arrays.length ; i++) {
 8             if (max< arrays[i]){
 9                 max=arrays[i];
10             }
11         }
12         System.out.println(max);
13     }
14 }

 

 

Practice 4

使用forEach循环打印数组元素

 1 package com.Xujie.Day10;
 2 
 3 public class Java10_06 {
 4     //JDK1.5 引入forEach  没有下标
 5     public static void main(String[] args) {
 6         int[] arrays={1,2,3,4,5};
 7         printArray(arrays);
 8 
 9     }
10     public static void printArray(int[] arrays){
11         for (int i = 0; i < arrays.length; i++) {
12             System.out.print(arrays[i]+"\t");
13         }
14     }
15 }

 

 

Practice 5

翻转数组(难点,重点)

 1 package com.Xujie.Day10;
 2 
 3 import static com.Xujie.Day10.Java10_06.printArray;
 4 
 5 public class Java10_07 {
 6     public static void main(String[] args) {
 7         int[]arrays={1,2,3,4,5};
 8         int[] reverse= reverse(arrays);
 9         printArray(reverse);
10     }
11     public static int[] reverse(int arrays[]){
12         int[] result=new int[arrays.length];
13         for (int i=0,j= result.length-1;i< result.length;i++,j--){
14             result[j]=arrays[i];
15         }
16         return result;
17     }
18 }

 

标签:Java10,int,arrays,length,static,数组,使用,public
来源: https://www.cnblogs.com/xujie-0528/p/13565459.html