集合框架,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

队列

可排序的队列

相关推荐
Azure++8 分钟前
Windows配置jar、redis、nginx开机自启
windows·redis·jar
非凡ghost1 小时前
批量转双层PDF(可识别各种语言) 中文绿色版
前端·windows·pdf·计算机外设·软件需求
m0_748240254 小时前
Windows编程+使用C++编写EXE加壳程序
开发语言·c++·windows
ttghgfhhjxkl7 小时前
Windows 系统下 RabbitMQ 延迟插件的安装步骤与常见问题修复
windows·rabbitmq·ruby
im_AMBER9 小时前
数据结构 09 二叉树作业
数据结构·笔记·学习
非凡ghost10 小时前
Adobe Lightroom安卓版(手机调色软件)绿色版
前端·windows·adobe·智能手机·软件需求
l1t10 小时前
利用DeepSeek修改数据结构提升求解集合程序效率
数据结构·python·deepseek
准时准点睡觉11 小时前
window安装MYSQL5.5出错:a windows service with the name MYSQL alreadyexists....
数据库·windows·mysql
先做个垃圾出来………12 小时前
偏移量解释
数据结构·算法
立志成为大牛的小牛13 小时前
数据结构——三十三、Dijkstra算法(王道408)
数据结构·笔记·学习·考研·算法·图论