Collection
来源:互联网 发布:jquery高级编程源代码 编辑:程序博客网 时间:2024/06/04 00:33
学习心得
一、专业课
1、Collection
2Map
2.1
2.2
2.3
TreeMap<String,Object> treeMap = new TreeMap<>();
// treeMap.put(null,"ad"); //treeMap的key不能为null
treeMap.put("a",null);
System.out.println(treeMap.size());
Hashtable<String,String>tHashtable = new Hashtable<>();
// tHashtable.put(null,"1"); //Hashtable的key与value
// tHashtable.put("a",null); //不能为null
HashSet<String>set = new HashSet<>();set.add(null);
HashSet<A>set2 = new HashSet<>();
set2.add(newA());//A需要重写equals与hashCode方法
set2.add(newA()); //才能保证唯一
TreeSet<B>set3 = new TreeSet<>();
set3.add(newB());//B需要重写Comparable接口>才能储存
set3.add(newB());//或者使用Comparator工具保证对象唯一
System.out.println(set3.size());
System.out.println(set.size());
System.out.println(set2.size());
HashMap<String,String> hashMap = new HashMap<>();
hashMap.put(null,null);
hashMap.put(null,"w");
System.out.println(hashMap.size());
ArrayList<String>list = new ArrayList<>();
list.add(null);
System.out.println(list.size());
LinkedList<String>list2 = new LinkedList<>();
list2.add(null);
System.out.println(list2.size());
二、小组PK
1.我方题目
1.阅读以下代码,若能运行输出运行结果,若不能运行说明原因(提示:getMessage()除数为0时输出:/by zero)
publicclass Question {
publicstatic void main(String[] args) {
Question1q1 = new Question1(0);
System.out.println(q1.count(0));
}
}
classQuestion1 {
staticint a;
{
System.out.println(a++); (7)
}
static{
System.out.println(++a); (6)
}
publicQuestion1(int a) {
this.a= a;
System.out.println(a); (5)
}
publicstatic int count(int a) {
try{
intb = 3 / a;
}catch (Exception e) {
System.out.println(e.getMessage()); (1)
returna; (2)
}finally {
a++; (3)
System.out.println("finally"); (4)
}
return2;
}
}
1//执行(6),成员变量初始值为1,输出1
1//执行(7),输出1,先输出再自增
0 //执行(5),输出的是局部变量
/by zero //打印异常信息
finally //异常执行完后,return之前执行finally语句块
0 //在finally中的a++没有返回,在catch中return之前把a压入了栈中,最后返回的是压栈时候的a,输出0
2.程序能否运行,如果能请输出结果,如果不能说明理由。
publicstatic void main(String[] args) {
//TODO Auto-generated method stub
chara = 1;
intb = 1;
intf = 2;
Longg = 3l;
Integere = 3;
Integerc = 321;
Integerd = 321;
Stringstr = "abc";
Stringstr3 = "abc";
StringBufferstr1 = new StringBuffer("abc");
StringBuilderstr2 = new StringBuilder("abc");
System.out.println(a== b); true//true
System.out.println(g== (a + f)); false//true
System.out.println(c== d); true//false
System.out.println(e== (b + f)); false //true
System.out.println(str.equals(str1));true //true
System.out.println(str1.equals(str2));true//true
System.out.println(str2.equals(str)); true//true
System.out.println(str== str3); false//true
System.out.println(str== str1); false//false
}
程序报错,str== str1不能比较
3、.填空题。
1)Hashtable的_______键值_______(键/值)不能为null.
2)TreeMap的________键______(键/值)不能为null.
3)HashMap的键和值________可以________(可以/不可以)为null.
4)ArrayList________可以________(可以/不可以)null.HashSet_______可以_________(可以/不可以)存null.
5)HashSet存储的元素的类必须实现__________hashCode()、eqquals()_____(什么方法/什么接口),否则无法实现存储的元素不重复。
6)TreeSet存储的元素需要实现__________comparable______________(什么方法/什么接口),否则无法实现存储的元素不重复。
4.程序能否运行,如果能请输出结果,如果不能说明理由。
publicclass Test04 {
staticint a=6;
publicstatic void main(String[] args) {
try{
System.out.println(fun());
}catch (Exception e) {
System.out.println(++a);
}
System.out.println(a);
}
publicstatic int fun() {
try{
inta = 2;
a=a/0;
System.out.println(a++);
}catch (ArithmeticException e) {
System.out.println(a++);
returna;
}finally{
System.out.println(a++);
returna;
}
}
}
答案:
6
7
8
8
5.publicclass J_test{
public static void main(String[]args) {
int i = 0;
try {
for (i = 0; i < 10;i++) {
try {
if(i%3== 0){
thrownew Exception();
}
System.out.println("1:"+1+",");
}catch(Exceptione) {
System.out.println("2:"+i+",");
i += 2;
if(i%3== 2){
thrownew Exception();
}
}finally{
i *= 2;
System.out.println("3:"+i+",");
}
}
}catch(Exception e){
System.out.println("4:"+i+",");
return;
}finally{
System.out.println("5:"+i+",");
i++;
}
System.out.println("End!");
}
}
2:0,
3:4,
4:4,
5:4,
2、对方题目
1、下列叙述正确的有哪些?()
A.TreeSet和TreeMap都不可以存放null做为值,并且他们都是线程安全的;
B.LinkedHashSet和LinkedHashMap能保证数据添加的顺序,并且他们都是线程安全的;
C.ArrayList和LinkedList都是线程不安全的,并且存放的元素可以为null;
D.LinkedList的底层实现是链表,所以插入和删除元素会效率比较高,而查找元素效率较低,原因是它查找元素只能从头到尾查找;
E.HashSet和HashMap在特性上有很多相似的地方,是因为HashSet的底层结构是通过HashMap来实现的.
答案:CE
解析:BLinkedHashSet和 LinkedHashMap都是线程不安全的,ATreeSet不能用null做值,而TreeMap不能用 null作为键值,值可以为null,而且两者都是线程不安全。DLinkedList是双向链表,可以从头到尾查找,也可以从尾到头查找。
2、请认真阅读下面的代码。编译若有错,请改正,然后输出结果。运行若有错,请注释掉错误行,然后输出结果。若没错,请输出所有结果。
publicstatic void main(String[] args) {
HashTable<Object,Object>ht=new Properties();
ht.put("A",9);
ht.put("B",10);
ht.put("C",11);
if(!ht.isEmpty()){
for(inti=0;i<=ht.size();i++){
System.out.println(ht.remove(0));
}
}
System.out.println(ht.size());
}
答案:
编译报错,修改地方是HashTable---->Hashtable
null
null
null
null
3
3、以下代码有没有错误,若没有,写出运行结果,若有,写出错误原因
publicclass Test5 {
publicstatic void main(String[] args) {
Bb = new B();
try{
b.fun();
}catch(Exception e) {
System.out.println("异常");
}finally{
System.out.println(b.left);
}
}
}
classA{
privatevoid fun() throws IOException{
System.out.println("-----");
thrownew IOException();
}
}
classB extends A{
intleft = 2;
intright = 2;
publicvoid fun()throws Exception{
for(int i = 1; i<4; i++) {
for(int j = 1; j<=right; j++) {
if(j == right || j == left) {
System.out.print("*");
}else{
System.out.print("_");
continue;
}
}
System.out.println();
if(i<2) {
left--;
right++;
}else {
left++;
right--;
thrownew Exception();
}
}
}
}
答案:
_*
*_*
异常
2
4、下面代码能否执行,如果能,输出可描述的结果,如果不能,找出不可描述的错误。
publicclass Test03 {
publicstatic void main(String[] args) {
Aa = null;
try{
a.getX("String");
break;
A.getNum();//////////////私有方法只能在类中调用
}catch(RuntimeException e){
e.printStackTrace();
}catch(NullPointerException e){//////////如果有多个catch异常必须从小到大排序
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
System.out.println("end!");
}
}
}
classA{
privatestatic void getNum() throws RuntimeException{
System.out.println("thisfun is A getNum");
}
staticvoid getX(String i) throws NullPointerException{
System.out.println("thisfun is A getX");
}
}
classB extends A{
publicvoid getNum() throws Exception{
System.out.println("thisfun is B getNum");
}
staticvoid getX(String i) throwsMyException{/////////该方法是重写父类的方法,而其抛出的 MyException与NullPointerException不相关,抛出的异常必需不能比父类更加宽泛,需要与父类方法抛出的异常类型一致,也就是说首先得是相同或者派生关系,其次他需要抛出比父类更小或一致的异常
System.out.println("thisfun is B getX");
}
}
classMyException extends Exception{
publicMyException(String message){
super(message);
}
}
5、java中的集合类包括ArrayList、LinkedList、HashMap等类,下列关于集合类描述正确的是()
A.ArrayList和LinkedList均实现了List接口
B.ArrayList的访问速度比LinkedList快
C.添加和删除元素时,ArrayList的表现更佳
D.HashMap实现Map接口,它允许任何类型的键和值对象,并允许将null用作键和值
答案:ABD
学习心得:
1.谈泊以明志,宁静以致远
- collection
- collection
- Collection
- collection
- collection
- collection
- Collection
- collection
- Collection
- Collection
- collection
- Collection
- Collection
- Collection
- Collection
- Collection
- collection
- Collection
- JavaScript中setInterval的用法
- 尼姆博弈
- 京峰运维学习系列--3
- System.getProperty("属性名")方法的使用
- jstl拆分字符串
- Collection
- DelegatingFilterProxy
- 受尽折磨的一周
- 设计模式之简单工厂
- bootstrap日期插件的汉化
- LeetCode第8题:String to Integer (atoi)
- 用docker搭建区块链环境(直接可用)
- 1 nginx 安装和配置
- E