Java陣列的練習
基於Java語言的案例,實現陣列的反轉、擷取、拼接,求陣列的最大、最小值,求陣列元素之和。
public class ArrayUtils{
// //1.計算陣列中最大值
public static int arrayMaxElement(int[] data){
int MaxElement = data[0];
for(int i=0; i<data.length; i++){
if(data[i] > MaxElement){
MaxElement = data[i];
}
}
System.out.println("最大值為:"+MaxElement);
return MaxElement;
}
// //2.計算陣列中最小值
public static int arrayMinElement(int[] data){
int MinElement = data[0];
for(int i=0; i<data.length; i++){
if(data[i] < MinElement){
MinElement = data[i];
}
}
System.out.println("最小值為:"+MinElement);
return MinElement;
}
// //3.計算陣列值之和
public static int arrayElementSum(int[] data){
int sum = 0;
for(int i=0; i<data.length; i++){
sum = sum + data[i];
}
System.out.println("陣列值之和為:"+sum);
return sum;
}
// //4.陣列拼接
public static int[] arrayJoin(int[] a, int[] b){
int[] c = new int[a.length + b.length];
for(int i=0; i<a.length; i++){
c[i] = a[i];
}
for(int j=a.length; j<c.length; j++){
c[j] = b[j-a.length];
}
System.out.println("a:");
printArray(a);
System.out.println();
System.out.println("b:");
printArray(b);
System.out.println();
System.out.println("c:");
printArray(c);
return c;
}
// //5.陣列擷取
//[start, end)
public static int[] arraySub(int[] data, int start , int end){
int[] sub = new int[end - start];
System.arraycopy(data, start, sub, 0, sub.length); //類方法、靜態方法
printArray(sub);
return sub;
}
// //6.陣列列印
public static void printArray(int[] data){
for(int i=0; i<data.length; i++){
System.out.print(data[i]+" ");
}
}
//7.陣列反轉
// 比如:[1,2,3,4] => [4,3,2,1]
public static void printReversal(int[] data){
System.out.println("原陣列:");
printArray(data);
System.out.println();
int count = data.length;
for(int i=0; i< count/2; i++){
int temp = data[i];
data[i] = data[count - i - 1];
data[count - i - 1] = temp;
}
//System.out.println("陣列元素個數為"+count);
System.out.println("反轉陣列:");
printArray(data);
}
public static void main(String[] args){
int[] data = {1, 3, 4, 6, 9, 8, 7};
System.out.println("data陣列內容為:");
printArray(data);
System.out.println();
arrayMaxElement(data);
System.out.println();
arrayMinElement(data);
System.out.println();
arrayElementSum(data);
System.out.println();
System.out.println("a,b陣列的拼接:");
int[] a = {1, 2, 3};
int[] b = {4, 5, 6};
arrayJoin(a, b);
System.out.println();
System.out.println("擷取陣列為:");
arraySub(data, 1, 4);
System.out.println();
printReversal(data);
System.out.println();
}
}
執行結果如下: