JAVA数组 *** 作

JAVA数组 *** 作,第1张

JAVA数组 *** 作
package threeday;
import java.util.Arrays;

public class array {
    public static void main(String args[]){
       // one();
       // two();
        //three();
        //four();
        //five();
       // six();
       // seven();
      //  int[] arr1= new int[]{4,8,56,3,69,54};
       // array sort = new array();
//        sort.eight(arr1);
     //   sort.nine(arr1);
     //   sort.ten(arr1);
        int arr[] = eleven(2,89);
        for(int i : arr){
            System.out.println(i);
        }
    }

    public static void one(){
        int a1[] = new int[10];
        a1[0] = 56;
        a1[1] = 72;
        for(int i = 2;i < a1.length;i ++) {
            a1[i] = (int)(Math.random() * 100); //别忘了int
        }
        for (int i = 0;i < a1.length; i++){
            System.out.println(a1[i]);
        }
        for(int i = a1.length - 1;i >= 0;i --){
            System.out.println(a1[i]);
        }
    }

    public static void  two(){
        int[][] a = new int[3][4];
        for (int i = 0 ;i < 3 ; i++){
            for (int j = 0; j < 4; j++){
                a[i][j] = (int)(Math.random()*100);
            }
        }
        for (int i = 0 ;i < 3 ; i++){
            for (int j = 0; j < 4; j++){
                System.out.print(a[i][j] + " ");//单引号不行
            }
            System.out.println();
        }
    }

    public static void three(){
        //9+99+999+9999+.......+9999999999
        long sum = 0;
        long num = 0;
        //System.out.println(arr[9]);
        for (int i = 0;i < 10;i ++){
            sum = sum + (num * 10 +9);
            num = num * 10 + 9;
        }
        System.out.println(sum);
    }

    public  static void four(){
        double sum = 0.0;
        for(int i = 1; i < 100; i ++){
            sum = 1.0/i + sum;
        }
        System.out.println(sum);
    }

    public static void five(){
        int[] arr = new int[5];
        //填充数组
        Arrays.fill(arr,9);
        for (int i = 0;i < arr.length;i ++){
            System.out.println(arr[i]);
        }
        //使用索引填充
        Arrays.fill(arr,1,3,23);
        for (int i = 0; i < arr.length; i++){
            System.out.println(arr[i]);
        }
        Arrays.sort(arr);
        for(int i = 0;i  max){
                max = a[i];
            }
            a[a.length-3] = max;
            System.out.println(a[i]);
        }
    //查询
        Arrays.sort(a);
        int index = Arrays.binarySearch(a,0,1,9);
        System.out.println(index);
    }

    public static void seven(){
        String str[] = new String[]{"vf","fe","ff"};
        Arrays.sort(str);
        int index = Arrays.binarySearch(str,0,2,"ff");
        System.out.println(index);
    }

    public static void eight(int[] arr){
        for (int i = 1; i < arr.length; i ++){
            for (int j = 0; j < arr.length - i; j ++){
                if(arr[j] < arr[j + 1]){
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j + 1] = temp;
                }
            }
        }
        showArray(arr);
    }

    public static void showArray(int[] arr){
        for(int i:arr){
            System.out.print(i + " ");
        }
        System.out.println();
    }

    public static void nine(int[] arr){
        int index;
        for (int i = 1;i < arr.length; i ++){
            index = 0;
            for(int j = 1;j <= arr.length - i; j++){
                if (arr[j] > arr[index]){
                    index = j;
                }
            }
            int temp = arr[arr.length - i];
            arr[arr.length - i] = arr[index];
            arr[index] = temp;
        }
        showArray(arr);
    }

    public static void ten(int[] arr){
        int len = arr.length;
        for(int i = 0; i < len/2;i ++){
            int temp = arr[i];
            arr[i] = arr[len - 1 - i];
            arr[len - 1 - i] = temp;
        }
        showArray(arr);
    }

    //返回的是数组就要写成数组
    public static int[] eleven(int len,int max){
        int arr[] = new int[len];
        for (int i = 0; i < len; i++){
            arr[i] = (int)(Math.random()*max);
        }
        return arr;
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5718252.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存