List常见用法
来源:互联网 发布:python 模拟浏览器 编辑:程序博客网 时间:2024/05/22 17:15
类型
- ArrayList(长于随机访问元素,但是在中间插入和删除元素时较慢)
- LinkList(优化了顺序访问,在随机访问方面相对较慢,特性集较ArrayList更大)
例子
public static void print(String s){ System.out.println(s); }// Random rand = new Random(47);// List<Pet> pets = Pets.arrayList(7); public static void main(String args[]){ Random rand = new Random(47); List<String> pets = new ArrayList(); pets.add("Rat"); pets.add("Manx"); pets.add("Cymric"); pets.add("Mutt"); pets.add("Pug"); pets.add("Cymric"); pets.add("Pug"); print("1: "+pets); pets.add("Hamster"); print("2: "+pets); print("3: "+pets.contains("Hamster")); pets.remove("Hamster"); String p = pets.get(2); print("4: "+p+" "+pets.indexOf(p) ); String cymric = "Cymric"; print("5: "+pets.indexOf(cymric)); print("6: "+pets.remove(cymric)); print("7: "+pets.remove(p)); print("8: "+pets); pets.add(3,"Mouse"); print("9: "+pets); List<String> sub = pets.subList(1, 4); print("subList: "+sub); print("10: "+pets.containsAll(sub)); Collections.sort(sub); print("sorted subList: "+sub); print("11: "+pets.containsAll(sub)); Collections.shuffle(sub,rand); print("shuffle subList: "+sub); print("12: "+pets.containsAll(sub)); List<String> copy = new ArrayList<String>(pets); sub = Arrays.asList(pets.get(1),pets.get(4)); print("sub: "+sub); copy.containsAll(sub); print("13: "+copy); copy = new ArrayList<String>(pets); copy.remove(2); print("14: "+copy); copy.removeAll(sub); print("15: "+copy); copy.set(1, "Mouse"); print("16: "+copy); copy.addAll(2,sub); print("17: "+copy); print("18: "+pets.isEmpty()); pets.clear(); print("19: "+pets); print("20: "+pets.isEmpty()); }//output/* 5: 21: [Rat, Manx, Cymric, Mutt, Pug, Cymric, Pug]2: [Rat, Manx, Cymric, Mutt, Pug, Cymric, Pug, Hamster]3: true4: Cymric 25: 26: true7: true8: [Rat, Manx, Mutt, Pug, Pug]9: [Rat, Manx, Mutt, Mouse, Pug, Pug]subList: [Manx, Mutt, Mouse]10: truesorted subList: [Manx, Mouse, Mutt]11: trueshuffle subList: [Mouse, Manx, Mutt]12: truesub: [Mouse, Pug]13: [Rat, Mouse, Manx, Mutt, Pug, Pug]14: [Rat, Mouse, Mutt, Pug, Pug]15: [Rat, Mutt]16: [Rat, Mouse]17: [Rat, Mouse, Mouse, Pug]18: false19: []20: true */
1.contains()方法来确定某个对象是否在列表内
2.remove()移除某个元素(若该元素有多个,移除所有该元素)
3.indexOf()获取对象在List中所处位置(获取第一个出现的位置)
4.subList(a,b)从列表中获得一个片段,左闭右开
5.containsAll()包含该集合的所有元素(Collecions.sort()和Collections.shuffle()(随机排序)不会影响)
6.retainAll()保留了同时在copy和sub中的元素(依赖于equals)
7.removeAll()移除所有元素
8.set(),replace(),替换
9.addAll()从制定位置插入一个列表
10.toArray()将Collection转换成一个数组
阅读全文
0 0
- List常见用法
- list用法及其常见错误
- 【Python】list用法及其常见错误
- java中List的常见用法
- List<T>集合的常见用法(控制台)
- Python——list常见用法
- List和ListIterator的常见用法及特性
- STL-list支持的集中常见操作的用法
- list的一些常见用法总结(实用干货收藏)
- List用法
- List用法
- list 用法
- list用法
- list用法
- list用法
- List<>用法
- list 用法
- List 用法
- 广州实体店为什么要开发微信小程序?
- vue如何实现数据的双向绑定
- el表达式与标签库
- 项目:人员管理系统
- 二叉树中路径和为某整数的所有路径 lint code by python
- List常见用法
- 转载:objc与鸭子对象
- Java作业
- HDU 4009 Transfer water【最小树形图】
- process_flag(trap_exit, true)
- Javaweb 绝对路径和相对路径-巧理解
- 框架储备知识1
- npm更新模块同步到package.js中
- jboss数据源密码解密