常用集合接口表
各集合底层实现原理
http://m.blog.csdn.net/qq_25868207/article/details/55259978
疑问测试:
1.添加完集合元素后,必须获取其中的元素再取相应的属性值。直接返回取出的元素只是一个地址。
Course cr1=new Course(1,"数据结构");
courseToSelect.add(cr1);
System.out.println((Course)courseToSelect.get(0));
Course temp=(Course)courseToSelect.get(0);
System.out.println(temp.getId()+" "+temp.getCourseName());
Course cr2=new Course(2,"离散数学");
courseToSelect.add(cr2);
Course temp1=(Course)courseToSelect.get(1);
System.out.println(temp1.getId()+" "+temp1.getCourseName());
/*
collection.list.student_course.Course@30f39991
1 数据结构
2 离散数学
*/
2.两种add方法中验证:集合有序,确实制定位置会把之前添加的顺序挤到后面;
Course cr1=new Course(1,"数据结构");
courseToSelect.add(cr1);
Course cr2=new Course(2,"离散数学");
courseToSelect.add(0,cr2);
System.out.println((Course)courseToSelect.get(0));
Course temp=(Course)courseToSelect.get(0);
System.out.println(temp.getId()+" "+temp.getCourseName());
Course temp1=(Course)courseToSelect.get(1);
System.out.println(temp1.getId()+" "+temp1.getCourseName());
/*集合有序,确实制定位置会把之前添加的顺序挤到后面;
2 离散数学
1 数据结构
*/
集合概要:
基础接口collection的增删改查
java.lang.collection
boolean |
|
boolean |
addAll(Collection<? extends E> c) |
void |
clear() |
boolean |
|
boolean |
containsAll(Collection<?> c) |
boolean |
|
int |
hashCode() |
boolean |
isEmpty() |
iterator() |
|
boolean |
|
boolean |
removeAll(Collection<?> c) |
boolean |
retainAll(Collection<?> c) |
Object[] |
toArray() |
默认的集合操作中,put元素后,会按照object处理,获取元素的时候也是默认为object,所以如果没有范型留意类型强转。
初始化数组的巧妙写法
Course[] courses={new Course(5,"Java"),new Course(6,"c"),new Course(7,"C++")};
for (Course co:courses
) {
System.out.println("id "+co.getId()+" name "+co.getCourseName());
}
/**初始化数组的巧妙写法
* id 5 name Java
* id 6 name c
* id 7 name C++
*/
细节详解:
ArrayList:
允许包括 null 在内的所有元素。
为提高这类迭代器的正确性而编写一个依赖于此异常的程序是错误的做法:迭代器的快速失败行为应该仅用于检测 bug。
构造方法
构造方法摘要 |
|
|
|
|
|
|
方法摘要:
方法摘要 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
迭代器:
java.util.iteraor
iterator:只是接口,依赖于集合存在,旨在遍历的时候作用,没有存储功能。
|
|
|
|
|
|
范型:
不指定范型,集合中的元素原理上,可以是任意类型的对象或者对象的引用。,而范型免去了类型转换的麻烦:
范型针对指定类和制定类的字类型都可以,范型中基本类型要用其封装类。
如果是父类中没有没有隐式构造器,子类中必须手动添加上。
Set:
循环遍历只能用forEach或者itertor,因为set无序,所以不能像List一样用Get()方法。
Map:
操作的是映射关系。