Java数组的查找方式及二分查找
  1. 数组查找

    public class ArraySearchTest {
        public static void main(String[] args) {
            int[] a = new int[]{1, 5, 6, 7, 10, 3, 9};
            int value = 9;
    
            int result = search(a, 9);
    
            if (result > 0) {
                System.out.println("找到了,索引为" + result);
            } else {
                System.out.println("未找到");
            }
        }
    
        public static int search(int[] array, int value) {
            int index = -1;
            for (int i = 0; i < array.length; i++) {
                if (array[i] == value) {
                    index = i;
                    break;
                }
            }
            return index;
        }
    }
    
  2. 二分查找(Binary Search):待查找的数组必须有序

    1,2,3,4,5,6,7,8,9

    10

    public static int binarySearch(int[] array, int value) {
            int left = 0;
            int right = array.length - 1;
            int middle;
    
            while (left <= right) {
                middle = (left + right) / 2;
    
                for (int k = 0; k < array.length; k++) {
                    System.out.print(array[k]);
                    if (k == middle) {
                        System.out.print("#");
                    }
                    System.out.print(" ");
                }
    
                System.out.println();
    
                if (array[middle] == value) {
                    return middle;
                }
    
                if (value > array[middle]) {
                    left = middle + 1;
                }
    
                if (value < array[middle]) {
                    right = middle - 1;
                }
            }
    
            return -1;
        }
    

    效果

    1 2 3 4 5
    1 2 3 4 5 6 7
    1 2 3 4 5 6 7 8
    1 2 3 4 5 6 7 8 9# 
    找到了,索引为8
    
  3. 随机生成50个数字(整数),每个数字的范围是[10,50],统计每个数字出现的次数以及出现次数最多的数字与它的个数 ,最后将每个数字及其出现次数打印出来,如果某个数字出现次数为 0,则不要打印它。打印时按照数字的升序排列。

    import java.util.Random;
    
    /**
     * @name: WorkTest
     * @author: terwer
     * @date: 2022-10-19 00:10
     **/
    public class WorkTest {
        public static void main(String[] args) {
            int[] nums = new int[50];
    
            Random random = new Random();
            for (int i = 0; i < nums.length; i++) {
                nums[i] = 10 + random.nextInt(50 - 10 + 1);
            }
    
    
            for (int j = 10; j <= 50; j++) {
                int count = 0;
                for (int m = 0; m < nums.length; m++) {
                    if (nums[m] == j) {
                        count++;
                    }
                }
    
                if (count > 0) {
                    System.out.println(j + "出现的次数:" + count);
                }
            }
    
    
    //        for (int k = 0; k < nums.length; k++) {
    //            System.out.println(nums[k]);
    //        }
        }
    }
    

    更好的实现:

    import java.util.Random;
    
    /**
     * @name: WorkTest
     * @author: terwer
     * @date: 2022-10-19 00:10
     **/
    public class WorkTest2 {
        public static void main(String[] args) {
            int[] count = new int[41];
    
            Random random = new Random();
            for (int i = 0; i < 50; i++) {
                int number = 10 + random.nextInt(50 - 10 + 1);
                System.out.println(number);
                count[number - 10]++;
            }
    
    
            for (int j = 0; j < count.length; j++) {
                if (count[j] == 0) {
                    continue;
                }
    
                System.out.println((10 + j) + "出现的次数:" + count[j]);
            }
    
            int max = count[0];
            int maxNum = 10;
            for (int k = 0; k < count.length; k++) {
                if (max < count[k]) {
                    max = count[k];
                }
    
                if (max == count[k]) {
                    maxNum = k + 10;
                    System.out.println(maxNum);
                }
            }
            System.out.println("最大的数字出现的次数:" + max);
        }
    }
    

Terwer...大约 2 分钟后端开发JavaSE查找数组二分必须有序二分查找searchbinary-search
包装类与数组
  1. 包装类(Wrapper Class)。

    针对原生数据类型的包装。 包装类(8 个)都位于 java.lang​ 包下。

    java 中的 8 个包装类分别是:Byte,Short,Integer,Long,Float,Double,Character,Boolean。

    他们的使用方式都是一样的,可以实现原生数据类型和包装类额双向转换。

  2. 数组(Array):相同类型 数据的集合叫做数组。

  3. 如何定义数组:

    type[] 变量名 = new type[数组中元素的个数 ]; 
    

    按照下列方式定义长度为 10 的数组:

    int[] a = new int[10]; // 或者
    int a[] = new int[10];
    
  4. 数组中的元素索引是从 0 开始的。对于数组来说,最大的索引 == 数组的长度 - 1 。

  5. 定义数组的第三种方式:

    type[] 变量名 = new type[]{逗号分隔的初始化值列表}
    
  6. Java 中的每个数组都有一个 length 属性,他表示数组的长度。

    length 属性是 public,final,int 的。数组长度一旦确定,就不能改变大小。


Terwer...大约 3 分钟后端开发JavaSE数组包装二维使用定义classarray