标签:
package javafirst; import java.util.Arrays;
class Show{ public void showArray(int[] arr){ for(int i : arr){ System.out.print(i + " "); } System.out.println(); } }
public class ArrayTest { public static void main(String[] args){ Show sh = new Show(); //习题一 arr1将0~3的元素复制给arr2(四个数) int[] arr1 = {1,2,3,4,5,6}; int arr2[]; arr2 = Arrays.copyOfRange(arr1, 0,4); sh.showArray(arr2); System.out.println("\n习题二"); //习题二 输出数组最小的数,用Arrays类的方法简单一些 Arrays.sort(arr1); System.out.println("最小的数字为 "+arr1[0] +"\n 习题三"); //习题三 替换位置2元素 String arrStr[] = new String[]{"abc","adc","sdc","bcd"}; for (String str : arrStr){ System.out.print(str + " "); } System.out.println("\n位置2替换后:"); Arrays.fill(arrStr, 2,3,"bb"); for (String str : arrStr){ System.out.print(str + " "); } System.out.println("\n习题四"); //习题四 二维数组的行列互调 int[][] arr3 = {{1,2,3},{4,5,6},{7,8,9}}; System.out.println("对调前"); for(int i = 0; i < arr3.length; i++){ for(int k = 0; k < arr3[i].length; k++){ System.out.print(arr3[i][k] + " "); } System.out.println(); } System.out.println("对调后"); for(int i = 0; i < arr3.length; i++){ for(int k = 0; k < arr3[i].length; k++){ System.out.print(arr3[k][i] + " "); } System.out.println(); } } }
输出结果
1 2 3 4 习题二 最小的数字为 1 习题三 abc adc sdc bcd 位置2替换后: abc adc bb bcd 习题四 对调前 1 2 3 4 5 6 7 8 9 对调后 1 4 7 2 5 8 3 6 9
标签:
原文地址:http://www.cnblogs.com/whytohow/p/4865420.html