JAVA中STL使用
2024-08-25 14:25:04
Vector:和c++的vector使用方法类似。
Vector<Integer> vec=new Vector<> ();
ArrayList:Java.util.ArrayList类是一个动态数组类型,也就是说,ArrayList对象既有数组的特征,也有链表的特征。
import java.util.*; public class Main {
public static void main(String[] args) {
ArrayList<Integer> arr=new ArrayList<>();
arr.add(1);//添加
arr.add(2,6);
arr.size();//获取长度
arr.set(1, 4);//修改
int st=arr.remove(0);//删除
//arr.clear();//清空
//arr.get(1);//获取
int v=arr.indexOf(3);//获取索引
System.out.println(v);
} }
LinkedList:LinkedList 实现 Deque 接口,即能将LinkedList当作双端队列使用。
import java.util.*; public class Main {
public static void main(String[] args) {
LinkedList<Integer> lin=new LinkedList<>();
lin.addFirst(st);//在首部加
lin.addLast(st);//在尾
lin.removeFirst();//移除首元素
lin.removeLast();//尾
lin.push(st);//压入栈
lin.pop();//弹出栈
} }
HashSet:无重复元素。
HashSet<Integer> hashset=new HashSet<>();
HashMap:
HashMap<Integer, Integer> map=new HashMap<>();
STL声明总结:
import java.util.*; public class Main { public static void main(String[] args) { List<String> mylist1 = new ArrayList<>();
List<String> mylist2 = new LinkedList<>();
List<String> mylist3 = new Vector<>(); Vector<String> vec = new Vector<>(); Queue<String> que = new LinkedList<>(); Stack<String> sta = new Stack<>(); Set<String> myset = new HashSet<>();
Set<String> myset2 = new TreeSet<>(); // Good Map<String, Integer> mymap = new HashMap<>(); // Good
Map<String, Integer> mymap2 = new TreeMap<>();
}
}
最新文章
- iftop与dstat-一次网站故障分析经历
- Leetcode Copy List with Random Pointer
- ACM 杭电HDU 2084 数塔 [解题报告]
- MVC5 + EF6 + Bootstrap3 (15) 应用ModelState和Data Annotation做服务器端数据验证
- 前端科普文—为什么<;!DOCTYPE>; 不可或缺
- 用eclipse开发和调试postgresql-8.4.1
- Undefined index:
- Linux 下WordPress FTP帐号解决办法
- C++学习50 对字符串流的读写
- POJ 3468 A Simple Problem with Integers(线段树)
- websphere中由于实际应用没有卸载干净,导致安装不了。以下是完全卸载应用程序的方法
- (转)linux性能优化总结
- ubuntu 安装qq
- VS2010在WIN7 64位系统下架设网站及路由器配置
- Oracle查询指定某一天数据,日期匹配
- centos6.5图形界面NetworkManager 配置ip文件位置
- 【百度地图API】自定义可编辑的交通路线
- flask_session_源码剖析
- OC可点击的两种轮播图效果
- vue项目向小程序迁移调研