Java数据结构---数组

来源:互联网 发布:钥匙包 淘宝 编辑:程序博客网 时间:2024/05/16 15:21

    • 数组
      • 数组的声明
      • 数组的查找和删除

数组

数组的声明

int[] ints=new int[10];int intss[]=new int[10];int intss[] =new int[]{1,4,6,7};int intsss[]={2,4,6,8};

数组的查找和删除

  • 数组的缺点是长度固定,不能随着数据的大小变化而变化
  • 数组的查找 和删除

    • 假如有数组的长度为N,那么我们的查到到其中某个元素的平均查找次数是N/2
      查找时间是T=K*N/2
    • 如果我们要删除某个元素。第一用平均N/2的查找次数查找到该数据,同时该数据删除后,后面的数据需要需要移动平均N/2次 耗时 T=K*N
  • 二分法查找 //前提 数组是有序的

public class ArrayDemo {    public static void main(String args[]) {        System.out.println("我是一个java main函数");        System.out.println("找到了--》"+ find(356));    }    //二分法  --->前提  : 数组是有序的    public static int find(int value) {        int[] ints = new int[]{1, 2, 4, 5, 7, 8, 9, 34, 66, 123, 156, 356, 456, 563, 578, 588, 656, 2442};        int start = 0;        int end = ints.length - 1;        while (end >= start) {            int index = (start + end) / 2;            if (ints[index] == value) {                return index;            } else if (value > ints[index]) {                start = index + 1;            } else {                end = index - 1;            }        }        return 0;    }}
原创粉丝点击