学习Java的记录(二)—线性表、栈、队列
1. Collection接口
Collection:存储元素的合集,合集框架的根接口
▪ Set:元素无序,不可重复
▪ List:元素有序,容许重复(存储顺序与插入顺序一样)
▪ Stack:栈(先进后出)
▪ Queue:队列(先进先出)
▪ Priority Queue:优先队列(用于存储按照优先级顺序处理的对象,优先级小先出)
collection的常用方法:
▪ **add(o:E):boolean**
给合集添加一个新元素o
▪ **size():int**
返回该合集中的元素数目
▪ **addAll/removeAll/retainAll(c: Collection<? extends E>):boolean**
addAll:把指定集合中的所有元素添加到这个合集中
removeAll:把这个集合中删除指定合集中的所有元素
retainAll:保留既出现在这个合集中也出现在制定和集中的元素
▪ **toArray():object[]**
为该集合中的元素返回一个Object数组
▪**iterator**
迭代器
需要注意!
Collection接口中有些方法是不能在具体子类中实现的 。这种情况下,这些方法会抛出异常java.lang.UnsupportedOperationException
解决方法:
public void someMethod(){
throw new UnsupportedOperationException
("method not support");
}
2. Iterator(迭代器)
是一种经典的设计模式,用于在不需要暴露数据在数据结构中保存细节的情况下,来遍历一个数据结构。换言之,iterator可以迭代collection的所有子类而无需知道其保存细节。
- iterator()
- next()
- hasNext()
例如:
import java.util.*;
public class TestIterator {
public static void main(String[] args) {
Collection<String> collection = new LinkedList<String>();
collection.add("New York");
collection.add("Atlanta");
collection.add("Dallas");
collection.add("Madison");
Iterator<String> iterator = collection.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next().toUpperCase() + " ");
}
System.out.println();
/*使用foreach循环来简化代码*/
// for(String element:collection)
// {
// System.out.print(element.toUpperCase()+" ");
// }
}
}
区别 ListIterator 和 Iterator :
3.线性表
List接口:定义一个容许重复值的顺序存储元素的合集。
- add(index,element)//在index处增加一个元素
- addAll(index,collection)//在index处增加collection里的所有元素
- remove(index)//删除index处的元素
- set(index,element)//设index处的元素为element
- indexOf(element)//返回元素为element的第一个索引值
- lastIndexOf(element)//返回元素为element的最后一个索引值
ArrayList 和 LinkedList
ArrayList:数组线性表,动态数组,通过下标访问。
LinkedList:链表,继承于AbstractSequentialList的双向链表。包含两个重要成员:header和size。LinkedList顺序访问高效,随机访问效率低。
ArrayList 和 LinkedList的对比
总结:
频繁读用ArrayList,频繁更新用LinkedList!
4.Comparator接口
Comparable接口:
public interface Comparable<T>{
public int CompareTo(T o)
}
javaAPI中的一些类,比如String,Date,Calendar等以及所有基本类型的数字包装类都实现了Comparable接口,其中定义的CompareTo方法用于比较实现了Comparable接口的同一类的的两个元素。
如果元素的类没有实现Comparable接口,那么可以定义一个比较器—Comparator来比较不同类的元素。
创建实现一个java.util.Comparator 接口的类并重写compare方法。
public int compare(T element1,T element2)
举例:
public class GeometricObjectComparator
implements Comparator<GeometricObject>, java.io.Serializable {
public int compare(GeometricObject o1, GeometricObject o2) {
double area1 = o1.getArea();
double area2 = o2.getArea();
if (area1 < area2)
return -1;
else if (area1 == area2)
return 0;
else
return 1;
}
}
5.Collections类
sort:对集合排序
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList list1=new ArrayList();
list1.add("red");
list1.add("yellow");
list1.add("green");
ArrayList list2=new ArrayList();
list2.add("red");
list2.add("yellow");
list2.add("blue");
List<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(1); // 1 is autoboxed to new Integer(1)
arrayList.add(2);
arrayList.add(3);
arrayList.add(1);
arrayList.add(4);
arrayList.add(0, 10);
arrayList.add(3, 30);
System.out.println("A list of integers in the array list:");
System.out.println(arrayList);
Collections.sort(arrayList);
System.out.println(arrayList);
}
}
binarySearch:查找指定合集中的元素,返回所查找元素的索引
shuffle:对集合进行随机排序
其他:
6.队列和优先队列
队列(queue)
先进先出
举例:
public class TestQueue {
public static void main(String[] args) {
java.util.Queue<String> queue = new java.util.LinkedList<>();
queue.offer("Oklahoma");
queue.offer("Indiana");
queue.offer("Georgia");
queue.offer("Texas");
while (queue.size() > 0)
System.out.print(queue.remove() + " ");
}
}
优先队列(priority queue)
元素具有优先级,优先级高(值小的先删除)
举例:
public class PriorityQueueDemo {
public static void main(String[] args) {
PriorityQueue<String> queue1 = new PriorityQueue<String>();
queue1.offer("Oklahoma");
queue1.offer("Indiana");
queue1.offer("Georgia");
queue1.offer("Texas");
System.out.println("Priority queue using Comparable:");
while (queue1.size() > 0) {
System.out.print(queue1.remove() + " ");
}
PriorityQueue<String> queue2 = new PriorityQueue<String>(
4, Collections.reverseOrder());
queue2.offer("Oklahoma");
queue2.offer("Indiana");
queue2.offer("Georgia");
queue2.offer("Texas");
System.out.println("\nPriority queue using Comparator:");
while (queue2.size() > 0) {
System.out.print(queue2.remove() + " ");
}
}
}