您的位置:首页 > 职场人生

黑马程序员——Java基础--集合框架(1)

2015-08-16 22:14 471 查看
-------
android培训、java培训、期待与您交流! ----------

一  集合类

面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,Java就提供了集合类。

特点:集合只用于存储对象,集合长度是可变的,集合可以存储不同类型的对象。

二 Collection接口

Collection 层次结构中的根接口。Collection表示一组对象,这些对象也称为 collection 的元素。

List:有序(元素存入集合的顺序和取出的顺序一致),元素都有索引。元素可以重复。

Set:无序(存入和取出顺序有可能不一致),不可以存储重复元素。必须保证元素唯一性。

其中常用的方法:

添加: 

add(object):添加一个元素 

addAll(Collection) :添加一个集合中的所有元素。 

删除: 

clear():将集合中的元素全删除,清空集合。 

remove(obj) :删除集合中指定的对象。注意:删除成功,集合的长度会改变。 removeAll(collection) :删除部分元素。部分元素和传入Collection一致。

 判断: 

boolean contains(obj) :集合中是否包含指定元素 。 

boolean containsAll(Collection) :集合中是否包含指定的多个元素。 boolean isEmpty():集合中是否有元素。  

获取: 

int size():集合中有几个元素。 

交集: 

boolean  retainAll(Collection) :对当前集合中保留和指定集合中的相同的元素。如果两个集合元素相同,返回flase;如果retainAll修改了当前集合,返回true。 

获取集合中所有元素: Iterator  iterator():迭代器 

将集合变成数组: toArray(); 

三  Iterator接口

集合方法iterator返回迭代器接口实现类对象。

迭代器方法hasNext判断有没有下一个元素。

next方法取出集合元素。

注意事项:

不要多次 next,只能一次。

如果已经没有元素了,还要去再次获取,出现没有元素被取出异常。

迭代中不能使用集合方法改变集合长度,否则出现。

四List接口

List本身是Collection接口的子接口,具备了Collection的所有方法。List的特有方法都有索引,这是该集合最大的特点。 List:有序(元素存入集合的顺序和取出的顺序一致),元素都有索引。元素可以重复。 

List接口的特有方法: 

添加: 

add(index,element) :在指定的索引位插入元素。 

addAll(index,collection) :在指定的索引位插入一堆元素。 

删除: 

remove(index) :删除指定索引位的元素。 返回被删的元素。 

获取: 

Object get(index) :通过索引获取指定元素。 

int indexOf(obj) :获取指定元素第一次出现的索引位,如果该元素不存在返回-1;   所以,通过-1,可以判断一个元素是否存在。 

int lastIndexOf(Object o) :反向索引指定元素的位置。 List subList(start,end) :获取子列表。

修改: 

Object set(index,element) :对指定索引位进行元素的修改。 

获取所有元素:

ListIterator listIterator():list集合特有的迭代器。

ArrayList:底层的数据结构是数组,线程不同步,ArrayList替代了Vector,查询元素的速度非常快。 

public class ArrayListDemo {
public static void main(String[] args) {
method();
method_1();
}
//ArrayList存储自定义对象,迭代器
public static void method_1(){
ArrayList array = new ArrayList();
array.add(new Student("a",101));
array.add(new Student("b",102));
array.add(new Student("c",103));
array.add(new Student("d",104));
Iterator it = array.iterator();
while(it.hasNext()){
Student s = (Student)it.next();
System.out.println(s.getName()+".."+s.getAge());
}
}
//ArrayList集合存储字符串,迭代器
public static void method(){
ArrayList array = new ArrayList();
array.add("qwe");
array.add("asd");
array.add("zxc");
Iterator it = array.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
LinkedList:底层的数据结构是链表,线程不同步,增删元素的速度非常快。

import java.util.*;
import cn.itcast.beans.*;
public class LinkedListDemo1 {
public static void main(String[] args) {
method_1();
}
// LinkedLis存储学生对象并迭代
public static void method_1(){
List list = new LinkedList();
list.add(new Student("a1",101));
list.add(new Student("a2",102));
list.add(new Student("a3",103));
list.add(new Student("a4",104));
Iterator it = list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
// LinkedList存储字符串并迭代
public static void method(){
List list = new LinkedList();
list.add("abc1");
list.add("abc2");
list.add("abc3");
list.add("abc4");
Iterator it = list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
Vector:底层的数据结构就是数组,线程同步的,Vector无论查询和增删都巨慢。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: