public class Demo03 {
public static void main(String[] args) {
int [] arrays = {1,2,3,4,5,99};
//打印全部数组元素
for (int i = 0; i < arrays.length; i++) {
System.out.println(arrays[i]);
}
//求和
int sum = 0;
for (int i = 0; i < arrays.length; i++) {
sum = sum + arrays[i];
}
System.out.println(sum);
//查找最大元素
int max = arrays[0];
for (int i = 0; i < arrays.length; i++) {
if (arrays[i]>max) {
max=arrays[i];
}
}
System.out.println(max);
}
}
数组的增强for循环
public class Demo04 {
public static void main(String[] args) {
int[] array = {10,20,30,40,50,60};
for (int i : array) {
System.out.println(i);
}
}
}
public class Demo04 {
public static void main(String[] args) {
int[] arrays = {10,20,30,40,50,60};
printArrays(arrays);//调用打印数组方法
}
//建一个方法-打印数组元素
public static void printArrays (int[] arrays){
for (int i = 0; i < arrays.length; i++) {
System.out.print(arrays[i]+" ");
}
}
public class Demo05 {
public static void main(String[] args) {
int [] a = {1,2,3,4,5,6,7};
int [] reverse = reverse(a); //调用反转
printArrays(reverse); //调用打印
}
//建一个-反转数组方法
public static int[] reverse(int[] arrays){
int[] result = new int[arrays.length];
for (int i = 0, k = result.length-1; i < arrays.length; i++,k--) {
result[k] = arrays[i];
}
return result;
}
//打印数组元素建方法
public static void printArrays(int[] arrays) {
for (int i = 0; i < arrays.length; i++) {
System.out.print(arrays[i] + " ");
}
}
}
多维数组
public class Demo06 {
public static void main(String[] args) {
int[][] arrays = {{1,2},{10,20,30},{100,200},{1000,2000,20},{10000,20000},};
for (int i = 0; i < arrays.length; i++) {
for (int k = 0; k < arrays[i].length; k++) {
System.out.print(arrays[i][k]+"\t");
}
System.out.println();
}
}
}
标签:23,arrays,System,int,length,static,数组,使用,public
From: https://www.cnblogs.com/PedroPascal/p/17096844.html