集合框架,List常用API,栈和队列初识

回顾

集合框架

两个重点------ArrayList和HashSet.

Vector/ArraysList/LinkedList区别

Vector ArraysList LinkedList
底层实现 数组 数组 链表
线程安全 安全 不安全 不安全
增删效率 较低 较低
扩容 *2 *1.5 --------

(>>)运算级最低,记得加括号。

常用Api

List*

remove()的两种删除

API 解释
1 add() 添加
2 remove(index) 索引删除
3 remove(Object) 对象删除
4 set(index,e)《Set》 修改
5 clear() 清空
6 size() 长度
7 get(index)《List》 查询
8 indexOf()《List》 找出位置
9 contains(用前要重写) 包含
10 isEmpty() 集合为空
11 itreator() 迭代器(遍历和删除)
12 toString() 转换为字符串

数组比较


Stack栈

后进先出

栈是一种特殊的数据结构,后进先出。

结构

底层还是数组。Vector派生出来的,继承Vector。

Api方法

push():入栈

peek():栈顶

取栈顶不会删除栈顶。

System.out.println("栈顶:"+stack.peek());//栈顶------最后进栈的

pop():出栈

出栈会删除栈顶元素。

System.out.println("wu:"+stack.pop());//出栈

完整代码:

复制代码
package com.ffyc.Stack;

import java.util.Stack;

public class StackDemo01 {
    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        //存------压栈
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.push(5);

        System.out.println("栈:"+stack);
        System.out.println("栈顶:"+stack.peek());//栈顶------最后进栈的
        //System.out.println("wu:"+stack.pop());//出栈

        //倒序打印
        while (stack.size()>0){
            int temp = stack.pop();//pop()后当前的栈顶元素自动删除
            System.out.println(temp);
        }

    }
}

反转"hello"

中间的回文数量的题可以用

计算器题目

准备工作

length-1 否则越界


Queue队列


offer()进

队头添加数据

poll()出

从队头取出元素/数据

peek()顶

取出最先进队的元素。第一个offer()进去的元素。

是下面代码的输出:queue.peek--->1

队列

可排序的队列

相关推荐
集成显卡9 小时前
windows 下使用 bat 批处理运行 Chrome 无头模式刷一波访问量
windows·程序员
Fanxt_Ja3 天前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
今后1233 天前
【数据结构】二叉树的概念
数据结构·二叉树
凯子坚持 c3 天前
精通 Redis list:使用 redis-plus-plus 的现代 C++ 实践深度解析
c++·redis·list
路由侠内网穿透3 天前
本地部署 GPS 跟踪系统 Traccar 并实现外部访问
运维·服务器·网络·windows·tcp/ip
第七序章3 天前
【C++STL】list的详细用法和底层实现
c语言·c++·自然语言处理·list
研华嵌入式3 天前
如何在高通跃龙QCS6490 Arm架构上使用Windows 11 IoT企业版?
arm开发·windows·嵌入式硬件
散1123 天前
01数据结构-01背包问题
数据结构
消失的旧时光-19433 天前
Kotlinx.serialization 使用讲解
android·数据结构·android jetpack
Gu_shiwww3 天前
数据结构8——双向链表
c语言·数据结构·python·链表·小白初步