您的位置:首页 > 编程语言 > Java开发

RE:JAVA学习-集合操作

2017-08-23 21:05 267 查看
1.Iterator:迭代器

1.1 作用:对collection集合进行迭代(遍历集合)

Collection c = new HashSet();

Iterator it= c.iterator();

public class Collection_iterator {

public static void main(String[] args) {
Collection c=new HashSet();
c.add("one");
c.add("#");
c.add("two");
c.add("#");
c.add("three");
c.add("#");
c.add("four");
c.add("#");
System.out.println(c);//[two, #, one, three, four]
Iterator it=c.iterator();
while(it.hasNext()){
String str=(String)it.next();
System.out.println(str);
if("#".equals(str)){
/*
* 使用迭代器遍历集合的过程中
* 不能通过集合的方法增删元素
* 否则迭代器在下次遍历元素时
* 会抛出异常.可以通过迭代器
* 提供的remove方法删除通过next
* 获取的元素
*/
//              c.remove(str);
it.remove();
}
}
System.out.println(c);//[two, one, three, four]
}
}


1.2 hasNext方法: boolean hasNext()判断集合是否还有元素可以遍历

public class NewForDemo2 {

public static void main(String[] args) {
Coll
4000
ection c=new ArrayList();
c.add("one");
c.add("#");
c.add("two");
c.add("#");
c.add("three");
c.add("#");
c.add("four");
c.add("#");
System.out.println(c);
/*
* 新循环并非新的语法,这是编译器认可而非虚拟机
* 认可的
* 编译器在编译源代码时发现使用新循环遍历集合时
* 会将代码改变为使用迭代器遍历
* 所以要注意,使用新循环遍历集合时不要使用集合
* 的方法修改元素
*/
for(Object o:c){
String str=(String)o;
System.out.println(str);
if(str.equals("#")){
//              c.remove(str);
}
}
}
}


1.3 E next() 返回迭代的下一个元素

遍历集合:

while(it.hasNext()){

String str=it.next();

System.out.println(str);

}

在进行迭代时不能对集合c进行操作

1.4 void remove() 在原集合中删除元素

注意:在调用remove方法前必须通过迭代器的next()方法迭代过元素,那么删除的就是这个元素。(并且不能再次调用remove方法,除非再调用next()后才可再次调用)

删除:

while (it.hasNext()) {

String str = it.next();

if (str.indexOf(‘c’) != -1) {

it.remove();

}

}

2.增强for循环(jdk1.5特性 本质上是迭代器)

2.1 语法:for(元素类型 e: 集合或数组){

循环体

}

for (String str : c) {

System.out.print(str.toUpperCase() + ” “);

}

在进行循环的时候不能对集合c进行操作

3.泛型(jdk1.5特性 本质:参数化类型)

3.1 定义:在类、接口和方法的定义过程中,所操作的数据类型被传入的参数指定

3.2 应用:在创建对象时可以将该类型作为参数传递(如:ArrayList E为泛型参数)

所有的集合类型都带有泛型参数,这样在创建集合时可以指定放入集合中的对象类型

4.List(可重复集) 是Collection的字接口,用于定义线性表数据结构

4.1 ArrayList和LinkedList(List的两个实现类)(方法逻辑上基本一样)

ArrayList更适合于随机访问而LinkedList更适合于插入和删除

4.2 E get(int index) 获取集合中指定下标对应的元素(从0开始)

E set(int index,E element) 将给定的元素存入给定位置,并将原位置的元素返回

4.3 void add(int index,E element) 将给定的元素插入到指定位置(原位置及后续元素都顺序向后移动)–注意区分 void add(E element) 添加元素方法

E remove(int index) 删除给定位置的元素,并将被删除的元素返回

4.4 List subList(int fromIndex,int toIndex)

截取下标从fromIndex到toIndex(含头不含尾)

注意:subList获取的List与原List占有相同的存储空间,对子List的操作会影响原List

public void testSubList() {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
list.add(i);
}
System.out.println(list); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
List<Integer> subList = list.subList(3, 8);
System.out.println(subList); // [3, 4, 5, 6, 7]
// subList获得的List和源List占有相同的数据空间
for (int i = 0; i < subList.size(); i++) {
subList.set(i, subList.get(i) * 10);
}
System.out.println(subList); // [30, 40, 50, 60, 70]
System.out.println(list); // [0, 1, 2, 30, 40, 50, 60, 70, 8, 9]
}


可以用截取子集合并使用clear方法来清楚List中的一段元素

// 可以用于删除连续元素

list.subList(3, 8).clear();

System.out.println(list);

4.5 转换为数组

两种方法:

Object[] toArray()

T[] toArray(T[]a)

List<String> list=new ArrayList<String>();
list.add("aa");
...
String[] str=list.toArray(new String[] {});
4.6 数组转换为List   (Arrays类中提供的一个静态方法asList)
static <T>List<T> asList<T...a>
返回的List的集合元素类型由传入的数组的元素类型决定
注意:返回的集合不能对其增删元素,否则会抛出异常。并且对集合的元素进行修改会影响数组对应的元素。
List<String> list = Arrays.asList(strArr);


5.Collections 集合工具类(是一个辅助类,为集合提供排序 线程安全)

5.1 void sort(List list) 对给定的集合元素进行自然排序

Collections.sort(list)

public class SortListDemo1 {

public static void main(String[] args) {
List<Integer> list=new ArrayList<Integer>();
Random random=new Random();
for(int i=0;i<10;i++){
list.add(random.nextInt(100));
}
System.out.println(list);

Collections.sort(list);
System.out.println();
}

}


5.2 Comparable 接口

作用:在使用Collections的sort排序的集合元素都必须是Comparable接口的实现类,该接口表示其子类是可比较的。

实现接口时要重写一个抽象方法:

int compareTo(T t);

返回值表示大小 >0 则当前对象大于给定对象 其他同理

public class Point implements Comparable<Point>{
private int x;
private int y;
/**
* @param x
* @param y
*/
public Point(int x, int y) {
super();
this.x = x;
this.y = y;
}
public Point(){

}

public String toString(){
return "("+x+","+y+")";
}

public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
/*
* 实现Comparable接口后必须重写方法compareTo
* 该方法的作用是定义当前对象this与参数对象o之间
* 比较大小的规则
* 返回值不关注具体取值,只关注取值范围
* 当返回值>0 当前对象this大于参数对象o(this>o)
* 当返回值<0 this<o
* 当返回值=0 this=o
*
*/
public int compareTo(Point o) {
int len=this.x*this.x+this.y*this.y;
int olen=o.x*o.x+o.y*o.y;
return len-olen;
}

}


//测试
public class SortListDemo2 {

public static void main(String[] args) {
List<Point> list=new ArrayList<Point>();
list.add(new Point(3,4));
list.add(new Point(1,4));
list.add(new Point(4,4));
list.add(new Point(5,2));
list.add(new Point(3,1));
list.add(new Point(6,3));
System.out.println(list);
/*
* sort方法要求集合元素必须实现Comparable接口
* 否则无法排序
*/
Collections.sort(list);
System.out.println(list);
}
}


5.3 Comparator

临时指定比较规则

实现

int compare(T o1,T o2)

返回值表示大小 >0 则o1>o2 其他同理

public class SortListDemo3 {
public static void main(String[] args) {
List<String> list=new ArrayList<String>();
list.add("祈求者");
list.add("主宰");
list.add("暗影恶魔");
System.out.println(list);
/*
* Collections提供了一个重载的sort方法
* static void sort(List,Comparator)
* 该方法要求额外传入一个比较器,然后使用这个
* 比较器定义的规则比较集合元素并进行自然排序
* 使用该方法时,sort不要求集合元素必须实现
* Comparable接口了(因为不用元素自身的比较规则)
*/
Collections.sort(list,new Comparator<String>(){
public int compare(String o1,String o2){
return o1.length()-o2.length();
}
});
System.out.println(list);
}
}


6.队列和栈(主要写的是队列)

队列 先进先出(FIFO first in first out)

栈 先进后出(FILO first in last out)

Queue队列:

6.1创建:Queue queue = new LinkedList();

6.2主要方法:

boolean offer 将一个对象添加至队尾(添加成功返回true)

E poll() 从队首删除并返回一个元素

E peek() 返回队首元素(但不删除)

Deque双端队列(从队列的两端分别可以入队和出队操作)double ended queue

限制Deque只能从一端出入,则可实现 栈(Stack)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: