Java 数组练习

Posted 哈哈,呵呵,嘿嘿

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 数组练习相关的知识,希望对你有一定的参考价值。

//二维数组
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 ",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 数组练习的主要内容,如果未能解决你的问题,请参考以下文章

图文+视频新手也友好Java一维数组详细讲解(内含练习题答案+详解彩蛋喔~)

java习题练习

Java程序设计基础 数组 练习

Java 数组练习

text 练习数组Java

Java-数组练习1