黑马程序员--集合类总结1
2014-01-02 22:15
246 查看
---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Android+IOS开发</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------
1.集合概述:
Java的集合类主要由两个接口派生而出:Collection和Map,Collection和Map是Java集合框架的根接口,这两个接口又包含了一些接口或实现类。Set和List接口是Collection接口派生的两个子接口,Queue是Java提供的队列实现,类似于List。Map实现类用于保存具有映射关系的数据(key-value)。
Collection
|--List
|---ArrayList
|--LinkedList
|--Vector
|--Set
|--HashSet
|--TreeSet
|--Map
|--HaspMap
|--TreeMap
Collection常用方法:
注意:
集合必须只有对象, 集合中的元素不能是基本数据类型。
添加删除:
boolean add(Object element)
boolean remove(Object element)
查询操作:
int size()
boolean isEmpty()
boolean contains(Object element)
Iterator iterator()
组操作:
boolean containsAll(Collection collection)
boolean addAll(Collection collection)
void clear()
void removeAll(Collection collection)
void retainAll(Collection collection)
转换操作:
Object[] toArray()
Object[] toArray(Object[] a)
List:
(1)List的特点:
Collection
|--List
元素有序(存储顺序和取出顺序一致),元素可以重复
|--Set
元素无序,唯一
(2)List的特有功能:
A:添加
add(int index,Object obj)
B:删除
remove(int index)
C:获取
Object get(int index)
D:列表迭代器
ListIterator listIterator()
E:修改
set(int index,Object obj)
(3)List的案例:
List存储字符串并遍历:
List list = new ArrayList();
list.add("hello");
list.add("world");
list.add("java");
//迭代器
Iterator it = list.iterator();
while(it.hasNext())
{
String s = (String) it.next();
System.out.println(s);
}
//普通for循环
for(int x=0; x<list.size(); x++)
{
String s = (String)list.get(x);
System.out.println(s);
}
List存储自定义对象并遍历:
(4)ListIterator的特殊用法:
A:可以逆向遍历,但是需要先正向遍历,一般不用。
B:并发修改异常
原因:在迭代器遍历的时候,不要使用集合修改集合本身。
解决方案:
A:使用迭代器遍历的时候,通过迭代器修改集合。
B:使用集合遍历,通过集合修改集合。
List集合的子类
(1)List的子类特点:
List
|--ArrayList
底层数据结构是数组,查询快,增删慢。
线程不安全,效率高。
|--Vector
底层数据结构是数组,查询快,增删慢。
线程安全,效率低。
|--LinkedList
底层数据结构是链表,查询慢,增删快。
线程不安全,效率高。
(2)到底使用谁?
需要安全吗:
要:Vector
不要:ArrayList,LinkedList
查询多:ArrayList
增删多:LinkedList
(3)ArrayList
1:存储字符串并遍历:
ArrayList<String> array = new ArrayList<String>();
array.add("hello");
array.add("world");
array.add("java);
//方式1
Iterator<String> it = array.iterator();
while(it.hasNext())
{
Strin
4000
g s = it.next();
System.out.println(s);
}
//方式2
for(int x=0; x<array.size(); x++)
{
String s = array.get(x);
System.out.println(s);
}
//方式3
for(String s : array)
{
System.out.println(s);
}
2:存储自定义对象并遍历:(自己补齐)
import java.util.ArrayList;
import java.util.Iterator;
public class LinkedList {
public static void main(String[] args) {
ArrayList<Student> array=new ArrayList<Student>();
Student s1=new Student("王献之",23,"男");
Student s2=new Student("王之",23,"女");
Student s3=new Student("献之",23,"女");
Student s4=new Student("王献是",23,"男");
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);
Iterator<Student> it=array.iterator();
while(it.hasNext())
{
Student s=it.next();
System.out.println(s.getName()+".."+s.getAge()+"..."+s.getSex());
}
}
}
4)LinkedList
存储字符串并遍历:
import java.util.LinkedList;
import java.util.Iterator;
public class ArrayList {
public static void main(String[] args) {
LinkedList<String> list=new LinkedList<String>();
list.add("你");
list.add("好");
list.add("啊");
Iterator<String> it=list.iterator();
while(it.hasNext())
{
String s =it.next();
System.out.println(s);
}
}
}
存储自定义对象并遍历:
import java.util.LinkedList;
import java.util.Iterator;
public class LinkedList {
public static void main(String[] args) {
LinkedList<Student> list=new LinkedList<Student>();
Student s1=new Student("张之",23,"男");
Student s2=new Student("王之",23,"女");
Student s3=new Student("黎之",23,"女");
Student s4=new Student("王献",23,"男");
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
Iterator<Student> it=list.iterator();
while(it.hasNext())
{
Student s=it.next();
System.out.println(s.getName()+".."+s.getAge()+"..."+s.getSex());
}
}
}
---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Android+IOS开发</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------
详细请查看:<a href="http://edu.csdn.net" target="blank">http://edu.csdn.net</a>
1.集合概述:
Java的集合类主要由两个接口派生而出:Collection和Map,Collection和Map是Java集合框架的根接口,这两个接口又包含了一些接口或实现类。Set和List接口是Collection接口派生的两个子接口,Queue是Java提供的队列实现,类似于List。Map实现类用于保存具有映射关系的数据(key-value)。
Collection
|--List
|---ArrayList
|--LinkedList
|--Vector
|--Set
|--HashSet
|--TreeSet
|--Map
|--HaspMap
|--TreeMap
Collection常用方法:
注意:
集合必须只有对象, 集合中的元素不能是基本数据类型。
添加删除:
boolean add(Object element)
boolean remove(Object element)
查询操作:
int size()
boolean isEmpty()
boolean contains(Object element)
Iterator iterator()
组操作:
boolean containsAll(Collection collection)
boolean addAll(Collection collection)
void clear()
void removeAll(Collection collection)
void retainAll(Collection collection)
转换操作:
Object[] toArray()
Object[] toArray(Object[] a)
List:
(1)List的特点:
Collection
|--List
元素有序(存储顺序和取出顺序一致),元素可以重复
|--Set
元素无序,唯一
(2)List的特有功能:
A:添加
add(int index,Object obj)
B:删除
remove(int index)
C:获取
Object get(int index)
D:列表迭代器
ListIterator listIterator()
E:修改
set(int index,Object obj)
(3)List的案例:
List存储字符串并遍历:
List list = new ArrayList();
list.add("hello");
list.add("world");
list.add("java");
//迭代器
Iterator it = list.iterator();
while(it.hasNext())
{
String s = (String) it.next();
System.out.println(s);
}
//普通for循环
for(int x=0; x<list.size(); x++)
{
String s = (String)list.get(x);
System.out.println(s);
}
List存储自定义对象并遍历:
(4)ListIterator的特殊用法:
A:可以逆向遍历,但是需要先正向遍历,一般不用。
B:并发修改异常
原因:在迭代器遍历的时候,不要使用集合修改集合本身。
解决方案:
A:使用迭代器遍历的时候,通过迭代器修改集合。
B:使用集合遍历,通过集合修改集合。
List集合的子类
(1)List的子类特点:
List
|--ArrayList
底层数据结构是数组,查询快,增删慢。
线程不安全,效率高。
|--Vector
底层数据结构是数组,查询快,增删慢。
线程安全,效率低。
|--LinkedList
底层数据结构是链表,查询慢,增删快。
线程不安全,效率高。
(2)到底使用谁?
需要安全吗:
要:Vector
不要:ArrayList,LinkedList
查询多:ArrayList
增删多:LinkedList
(3)ArrayList
1:存储字符串并遍历:
ArrayList<String> array = new ArrayList<String>();
array.add("hello");
array.add("world");
array.add("java);
//方式1
Iterator<String> it = array.iterator();
while(it.hasNext())
{
Strin
4000
g s = it.next();
System.out.println(s);
}
//方式2
for(int x=0; x<array.size(); x++)
{
String s = array.get(x);
System.out.println(s);
}
//方式3
for(String s : array)
{
System.out.println(s);
}
2:存储自定义对象并遍历:(自己补齐)
import java.util.ArrayList;
import java.util.Iterator;
public class LinkedList {
public static void main(String[] args) {
ArrayList<Student> array=new ArrayList<Student>();
Student s1=new Student("王献之",23,"男");
Student s2=new Student("王之",23,"女");
Student s3=new Student("献之",23,"女");
Student s4=new Student("王献是",23,"男");
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);
Iterator<Student> it=array.iterator();
while(it.hasNext())
{
Student s=it.next();
System.out.println(s.getName()+".."+s.getAge()+"..."+s.getSex());
}
}
}
4)LinkedList
存储字符串并遍历:
import java.util.LinkedList;
import java.util.Iterator;
public class ArrayList {
public static void main(String[] args) {
LinkedList<String> list=new LinkedList<String>();
list.add("你");
list.add("好");
list.add("啊");
Iterator<String> it=list.iterator();
while(it.hasNext())
{
String s =it.next();
System.out.println(s);
}
}
}
存储自定义对象并遍历:
import java.util.LinkedList;
import java.util.Iterator;
public class LinkedList {
public static void main(String[] args) {
LinkedList<Student> list=new LinkedList<Student>();
Student s1=new Student("张之",23,"男");
Student s2=new Student("王之",23,"女");
Student s3=new Student("黎之",23,"女");
Student s4=new Student("王献",23,"男");
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
Iterator<Student> it=list.iterator();
while(it.hasNext())
{
Student s=it.next();
System.out.println(s.getName()+".."+s.getAge()+"..."+s.getSex());
}
}
}
---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Android+IOS开发</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------
详细请查看:<a href="http://edu.csdn.net" target="blank">http://edu.csdn.net</a>
相关文章推荐
- 黑马程序员 Collection 集合的个人总结
- 黑马程序员 Map集合的个人总结
- 黑马程序员-集合(补充总结)
- 黑马程序员---一DAY17总结--java中你必须掌握的集合框架技术
- 黑马程序员 Java学习总结之集合框架(List篇)
- 黑马程序员——集合框架总结
- 黑马程序员 Java基础知识总结-j集合框架
- 集合总结-01-----黑马程序员
- 黑马程序员--集合知识点总结(二)
- 13--黑马程序员--技术总结之集合
- 黑马程序员——Java总结(集合框架)
- 黑马程序员----------各集合系列的特点(个人学习总结)
- 黑马程序员-集合总结
- 黑马程序员=======总结集合笔记
- 黑马程序员_JavaAPI——集合框架笔记总结
- 黑马程序员——java基础----集合框架知识点总结(一)
- 黑马程序员-集合总结
- 黑马程序员—基础回顾集合体系总结(短篇)
- 黑马程序员:Java基础总结----集合与IO的结合[Properties类]
- 黑马程序员之java--集合框架总结