关于集合的练习P235-1,2,3
第一题:
import java.util.*; public class ListTest { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); for(int i=0;i<101;i++){ list.add(i);
}
System.out.println("原先的数据:\n"+list); list.remove(10); System.out.println("移除之后的数据:\n"+list);
}
}
运行的结果:
原先的数据:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
移除之后的数据:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
第二题:
import java.util.*; public class SetList { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>();
HashSet<String> set = new HashSet<String>();
TreeSet<String> set1 = new TreeSet<String>(); Collections.addAll(list, "A","a","c","C","a");
Collections.addAll(set, "A","a","c","C","a");
Collections.addAll(set1, "A","a","c","C","a"); System.out.println("list的数据:\t"+list);
System.out.println("set的数据:\t"+set);
System.out.println("set的数据:\t"+set1); }
}
运行的结果:
list的数据: [A, a, c, C, a]
set的数据: [c, A, C, a]
set的数据: [A, C, a, c]
第三题:
import java.util.*; public class TestMap { public static void main(String[] args) { Emp e1 = new Emp("001","小赵");
Emp e2 = new Emp("002","小钱");
Emp e3 = new Emp("003","小孙");
Emp e4 = new Emp("004","小李");
Emp e5 = new Emp("005","小周");
Emp e6 = new Emp("006","小吴"); HashMap<String,String> m = new HashMap<String,String>(); m.put(e1.getId(), e1.getName());
m.put(e2.getId(), e2.getName());
m.put(e3.getId(), e3.getName());
m.put(e4.getId(), e4.getName());
m.put(e5.getId(), e5.getName());
m.put(e6.getId(), e6.getName()); System.out.println("原始的数据:\n"+m); m.remove("005"); System.out.println("移除之后的数据:\n"+m); }
}
class Emp{ private String id;
private String name;
public Emp(String id, String name) {
super();
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }
运行的结果:
原始的数据:
{004=小李, 005=小周, 006=小吴, 001=小赵, 002=小钱, 003=小孙}
移除之后的数据:
{004=小李, 006=小吴, 001=小赵, 002=小钱, 003=小孙}
最新文章
- Spring的IOC原理(转载)
- [Linux] Netstat 执行过慢,占CPU100%,原因查找
- Php 使用 fsockopen发送http请求
- gulp折腾日记——gulp-livereload
- Gym 100952F&;&;2015 HIAST Collegiate Programming Contest F. Contestants Ranking【BFS+STL乱搞(map+vector)+优先队列】
- Java之Collections.emptyList()、emptySet()、emptyMap()的作用和好处以及要注意的地方
- tomcat如何访问非webapp下的资源文件
- WPS 表格筛选两列相同数据-完美-2017年11月1日更新
- Python Selenium 文件下载
- JAVA语言 第二周
- iOS 沙盒目录结构及正确使用
- python集合set{ }、集合函数及集合的交、差、并
- Java_反射机制详解
- PHP中文件操作(1)--打开/读取文件
- windows内存debug技巧
- 7、Node.js EventEmitter
- 【Linux】- vi/vim
- css position: relative | absolute | static | fixed详解
- JDK1.8源码阅读系列之二:LinkedList
- Eureka搭建