- class demo {
- public static void main(String[] args) {
- int []arr={6,13,8,18,35,3,22};
- int [][]tdArr={{22,66,44},{77,33,88},{25,45,65},{11,66,99}};
- printArr(arr);
- System.out.println("max:"+getMax(arr));
- System.out.println("min:"+getMin(arr));
- reverseArr(arr);
- printArr(arr);
- System.out.println("3的index:"+findE(arr,3));
- printTwoDimensionArray(tdArr);
- System.out.println("二维数组的和是:"+sumArr(tdArr));
- }
- //数组的遍历
- public static void printArr(int []arr){
- for(int i=0;i<arr.length;i++){
- System.out.println("arr["+i+"]"+":"+arr[i]);
- }
- }
- //获取数组最大值
- public static int getMax(int []arr){
- int max=arr[0];
- for (int i=1;i<arr.length ;i++ ) {
- if(max<arr[i]){
- max=arr[i];
- }
- }
- return max;
- }
- //获取数组中最小值
- public static int getMin(int []arr){
- int min=arr[0];
- for (int i=1;i<arr.length ;i++ ) {
- if(min>arr[i]){
- min=arr[i];
- }
- }
- return min;
- }
- //反转数组
- public static int[] reverseArr(int[]arr){
- for (int i=0;i<=arr.length/2 ;i++ ) {
- int temp=0;
- temp=arr[i];
- arr[i]=arr[arr.length-1-i];
- arr[arr.length-1-i]=temp;
- }
- return arr;
- }
- //查找指定元素第一次在数组中出现的索引
- public static int findE(int[]arr,int e){
- for (int i=0;i<arr.length ;i++ ) {
- if(e==arr[i]){
- return i;
- }
- }
- return -1;
- }
- //二维数组遍历
- public static void printTwoDimensionArray(int tdArr[][]){
- for (int i=0; i<tdArr.length;i++ ) {
- for(int j=0;j<tdArr[i].length;j++){
- System.out.print(tdArr[i][j]+" ");
- }
- System.out.println();
- }
- }
- //二维数组求和
-
- public static int sumArr(int[][] tdArr){
- int sum=0;
- for(int i=0;i<tdArr.length;i++){
- for (int j=0;j<tdArr[i].length ;j++ ) {
- sum+=tdArr[i][j];
- }
- }
- return sum;
- }
- }
复制代码
|
|