从0.5到1学数据结构(java版)

1.数组

1.1数组的创建

        //1.知道有什么元素
        int[] a = {1,2,3};
        System.out.println(Arrays.toString(a));
        //2.
        int[] b = new int[]{1,2,3};
        System.out.println(Arrays.toString(b));
        //3.知道有几个元素
        int[] c = new int[3];
        for (int i = 0; i < c.length; i++) {
            c[i] = i +1 ;
        }
        System.out.println(Arrays.toString(c));
        //4.不需要指定长度多少 数组列表 致命数据类型 对象的类型Integer
        ArrayList<Integer> d = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            d.add(i+1);
        }
        System.out.println(d.toString());

1.2数组元素的添加

        //1.直接把要添加的元素放到末尾
        d.add(5);
        System.out.println(d.toString());
        //2.放到哪个索引上
        d.add(3,4);
        System.out.println(d.toString());

1.3访问元素

        //1.普通数组,直接索引访问
        int c1 = c[0];
        System.out.println(c1);
        //2.数组列表,需要调用get方法
        int d1 = d.get(1);
        System.out.println(d1);

1.4更新元素

        c[1] = 11;
        System.out.println(c[1]);
        d.set(1,11);
        System.out.println(d.get(1));

1.5删除元素

        d.remove(3);
        System.out.println(d.toString());

1.6数组长度

        int cs = c.length;
        System.out.println(cs);
        //ArrayList需要用size方法
        int ds = d.size();
        System.out.println(ds);

1.7遍历数组

        for (int i = 0; i < a.length; i++) {
            int aa = a[i];
            System.out.println(aa);
        }
        //ArrayList遍历数组
        for (int i = 0; i < d.size(); i++) {
            int dd = d.get(i);
            System.out.println(dd);
        }

1.8查找元素

        for (int i = 0; i < a.length; i++) {
            if (a[i]==1){
                System.out.println("有1");
            }
        }
        //ArrayList可以用contains方法
        boolean contains = d.contains(11);
        System.out.println(contains);

1.9数组排序

        //普通数组用arrays.sort
        Arrays.sort(a);
        System.out.println(Arrays.toString(a));
        //ArrayList
        Collections.sort(d);
        System.out.println(d.toString());
        //ArrayList倒序
        Collections.sort(d,Collections.reverseOrder());
        System.out.println(d.toString());

2.链表

2.1创建链表

LinkedList<Integer> ll = new LinkedList<>();

2.2添加元素

        ll.add(1);
        ll.add(2);
        ll.add(3);
        System.out.println(ll.toString());
        //根据索引添加元素
        ll.add(2,22);
        System.out.println(ll.toString());

 2.3访问元素

        int i = ll.get(0);
        int o = ll.get(3);
        System.out.println(i);
        System.out.println(o);

2.4搜索索引

        int i1 = ll.indexOf(22);
        System.out.println(i1);

2.5更新元素

        ll.set(0, 0);
        System.out.println(ll.toString());

2.6删除元素

        ll.remove(2);
        System.out.println(ll.toString());

2.7长度

        int size = ll.size();
        System.out.println(size);

3.队列

3.1创建队列

Queue<Integer> queue =new  LinkedList<>();

3.2添加元素

        queue.add(1);
        queue.add(2);
        queue.add(3);
        System.out.println(queue.toString());

3.3获取即将出列的元素

        int peeka = queue.peek();
        System.out.println(peeka);

3.4删除即将出列的元素

        int polla = queue.poll();
        System.out.println(polla);

3.5判空

System.out.println(queue.isEmpty());

3.6遍历队列

        while (!queue.isEmpty()){
            int pp = queue.poll();
            System.out.println(pp);
        }

4.栈

4.1创建栈

Stack<Integer> ss = new Stack<>();

4.2添加元素

        ss.push(1);
        ss.push(2);
        ss.push(3);
        System.out.println(ss.toString());

4.3获取栈顶元素

        Object peek = ss.peek();
        System.out.println(peek);

4.4删除栈顶元素

        Object pop = ss.pop();
        System.out.println(pop);
        System.out.println(ss.toString());

4.5栈的大小

        int size = ss.size();
        System.out.println(size);

4.6栈是否为空

        boolean empty = ss.isEmpty();
        System.out.println(empty);

4.7栈的遍历

        while (!ss.isEmpty()){
            Object pop1 = ss.pop();
            System.out.println(pop1);
        }

5.哈希表

5.1创建哈希表

        //1.通过数组创建哈希表,索引是key
        String[] hashtable = new String[4];
        //2.通过hashmap创建哈希表
        HashMap<Integer, String> map = new HashMap<>();

5.2添加元素

        //1.数组添加元素
        hashtable[1]="diyi";
        hashtable[2]="dier";
        hashtable[3]="disan";
        //2.hashmap添加元素
        map.put(1,"diyi");
        map.put(2,"dier");
        map.put(3,"disan");
        System.out.println(map.toString());

5.3更新元素

        //1.数组更新元素
        hashtable[1]="diyia";
        //2.hashmap更新元素
        map.put(1,"diyia");
        System.out.println(map.toString());

5.4删除元素

        hashtable[1]="";
        map.remove(1);

5.5获取元素

        String s = hashtable[2];
        System.out.println(s);
        String s1 = map.get(2);
        System.out.println(s1);

5.6判断是否含有此元素

        boolean b = map.containsKey(3);
        System.out.println(b);

5.7长度

        int size = map.size();
        System.out.println(size);

5.8判断是否为空

        map.isEmpty();

6.哈希集合

6.1创建集合

        HashSet<Integer> set = new HashSet<>();

6.2添加元素

        set.add(10);
        set.add(3);
        set.add(5);
        set.add(2);
        set.add(2);
        System.out.println(set.toString());

6.3搜索元素

        boolean contains = set.contains(3);
        System.out.println(contains);

6.4删除元素

        set.remove(10);

6.5长度

        set.size();

7.堆

7.1堆的创建

        //默认最小堆
        PriorityQueue<Integer> minheap = new PriorityQueue<>();
        //最大堆
        PriorityQueue<Integer> maxheap = new PriorityQueue<>(Collections.reverseOrder());

7.2添加元素

        minheap.add(10);
        minheap.add(8);
        minheap.add(9);
        minheap.add(11);
        minheap.add(2);
        System.out.println(minheap.toString());
        maxheap.add(10);
        maxheap.add(8);
        maxheap.add(9);
        maxheap.add(11);
        maxheap.add(2);
        System.out.println(maxheap.toString());

7.3获取堆顶元素

        minheap.peek();
        maxheap.peek();

7.4删除堆顶元素

        minheap.poll();
        maxheap.poll();

7.5长度

        minheap.size();

7.6遍历堆元素

        while (!minheap.isEmpty()){
            System.out.println(minheap.poll());
        }

8.树和图

跟算法匹配比较多

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值