码迷,mamicode.com
首页 > 编程语言 > 详细

Java 数组练习

时间:2019-12-30 21:15:36      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:int   排列   stat   system   二分   转换   while   i++   二维数组   

//二维数组
public class Test61{
    public static void main(String[] args) {
        int[][] arr={{1,2,3,4},{1,2,3},{1,2},{1}};
        for(int i=0;i<arr.length;i++){
            for(int j=0;j<arr[i].length;j++){
                System.out.printf("%d\t",arr[i][j]);
            }
            System.out.println(" ");
        }
    }
}、
import java.util.Arrays;

//数组逆序
/*public class Test60{
    public static void main(String[] args) {
        int[] arr={1,2,3,4};
        reverse(arr);
    }
    public static void reverse(int[] arr1){
        int left=0;
        int right=arr1.length-1;
        while(left<right){
            int tmp=arr1[left];
            arr1[left]=arr1[right];
            arr1[right]=tmp;
            left++;
            right--;
        }
        System.out.println( Arrays.toString(arr1));
    }
}*/
//数组数字排列
public class Test60{
    public static void main(String[] args) {
        int[] arr={1,2,3,4,5,6};
        transform(arr);
    }
    public static void transform(int[] arr1){
        int left=0;
        int right=arr1.length-1;
        while(left<right){
            if(left<right&&arr1[left]%2==0){
                left++;
            }else if(left<right&&arr1[right]%2!=0){
                right--;
            }else{
            int tmp=arr1[left];
            arr1[left]=arr1[right];
            arr1[right]=tmp;
          }
    }
        System.out.println(Arrays.toString(arr1));
    }
}
//查找数组中的元数
/*public class Test58{
    public static void main(String[] args) {
        int[] arr={1,2,3,4,5,6,7,8,9};
        int ret=find(arr,5);
        System.out.println(ret);
    }
    public static int find(int[] arr1,int num){
        for(int i=0;i<arr1.length;i++){
            if(arr1[i]==num){
                return i;
            }
        }
        return -1;
    }
}*/
//查找数组中指定的元数(二分查找)
public class Test58{
    public static void main(String[] args) {
        int[] arr={1,2,3,4,5,6,7,8,9};
        int ret=find(arr,5);
        System.out.println(ret);

    }
    public static int find(int[] arr1,int num){
        int left=0;
        int right=arr1.length-1;
        while(left<=right){
            int mid=(left+right)/2;
            if(num>arr1[mid]){
                left=mid+1;
            }else if(num<arr1[mid]){
                right=mid-1;
            }else{
                return mid;
            }
        }
        return -1;
    }
}
//找数组中最大的元数
/*public class Test57{
    public static void main(String[] args) {
        int[] arr={1,2,3,4,56,8,45,6};
        System.out.println(MAX(arr));
    }
    public static int MAX(int[] arr1){
        int max=arr1[0];
        for(int i=1;i<arr1.length;i++){
            if(max<arr1[i]){
               max=arr1[i];
            }
        }
        return max;
    }
}*/
//求数组中原数的平均值
public class Test57{
    public static void main(String[] args) {
        int[] arr={1,2,3,4,56,6};
        double num=agv(arr);
        System.out.println(num);

    }
    public static double agv(int[] arr1){
        double num=0;
        for(int x:arr1){
            num+=x;
        }
        return num/arr1.length;

    }
}
import java.util.Arrays;

//数组转换为字符串
/*public class Test56{
    public static void main(String[] args) {
        int[] arr={1,2,3,4};
        newarr(arr);
    }
    public static void newarr(int[] arr1){
        System.out.println(Arrays.toString(arr1));
    }
}*/
//数组的拷贝
import java.util.Arrays;
public class Test56{
    public static void main(String[] args) {
        int[] arr={1,3,4,5,6};
        int[] newarr=Arrays.copyOf(arr,arr.length);
        System.out.println(Arrays.toString(newarr));
    }
}
import java.util.Arrays;

//数组逆序
/*public class Test60{
    public static void main(String[] args) {
        int[] arr={1,2,3,4};
        reverse(arr);
    }
    public static void reverse(int[] arr1){
        int left=0;
        int right=arr1.length-1;
        while(left<right){
            int tmp=arr1[left];
            arr1[left]=arr1[right];
            arr1[right]=tmp;
            left++;
            right--;
        }
        System.out.println( Arrays.toString(arr1));
    }
}*/
//数组数字排列
public class Test60{
    public static void main(String[] args) {
        int[] arr={1,2,3,4,5,6};
        transform(arr);
    }
    public static void transform(int[] arr1){
        int left=0;
        int right=arr1.length-1;
        while(left<right){
            if(left<right&&arr1[left]%2==0){
                left++;
            }else if(left<right&&arr1[right]%2!=0){
                right--;
            }else{
            int tmp=arr1[left];
            arr1[left]=arr1[right];
            arr1[right]=tmp;
          }
    }
        System.out.println(Arrays.toString(arr1));
    }
}

Java 数组练习

标签:int   排列   stat   system   二分   转换   while   i++   二维数组   

原文地址:https://www.cnblogs.com/yuzhenghan/p/12121395.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!