数组使用:
1. For-Each循环
2. 数组作方法入参
3. 数组作返回值
4. 普通的For循环
代码:
package com.pzz.array;
public class ArrayDemo03 {
public static void main(String[] args) {
int[] arrays = {1, 2, 3, 4, 5};
//打印全部的数组元素
for (int i = 0; i < arrays.length; i++) {
System.out.println(arrays[i]);
}
System.out.println("====================");
//计算所有元素的和
int sum = 0;
for (int i = 0; i < arrays.length; i++) {
sum = sum + arrays[i];//a+=arrays[i];
}
System.out.println(sum);
System.out.println("==================");
//查找最大元素
int max = arrays[0];
for (int i = 0; i < arrays.length; i++) {
if (arrays[i]>max){
max = arrays[i];
}
}
System.out.println(max);
}
}
代码:
package com.pzz.array;
public class ArrayDemo04 {
public static void main(String[] args) {
int[] arrays = {1,2,3,4,5};
// //jdk1.5,没有下标
// for (int array : arrays) {
// System.out.println(array);
// }
//printArray(arrays);
int[] reverse = reverse(arrays);
printArray(reverse);
}
//打印数组元素
public static void printArray(int[] arrays){
for (int i = 0; i < arrays.length; i++) {
System.out.print(arrays[i]+" ");
}
}
//反转数组
public static int[] reverse(int[] arrays){
int[] result = new int[arrays.length];
//反转的操作
for (int i = 0,j= result.length-1; i < arrays.length; i++,j--) {
//result[] = arrays[i];
result[j] = arrays[i];
}
return result;
}
}
标签:Java,05,arrays,System,int,length,数组,out
From: https://www.cnblogs.com/PZZzuel/p/16608153.html