JAVA编程思想学习笔记(十一)容器类List

容器类

在《java编程思想》一书中,容器类本是在持有对象那一章节里面的,这里我特意给提出来了,因为内容代码比较多,与其放一起显得太臃肿,倒不如这样来的清爽些。

List

List承诺可以将元素维护在特定的序列中,List接口在Collection的基础上添加了大量的方法,使得可以在List中插入和移除元素。
有两种类型的List:

  • 基本的ArrayList,它擅长于随机访问元素,但是在中间插入和移除元素时比较慢。
  • LinkedList,它通过代价比较低的在List中间进行插入和删除操作,提供了优化的顺序访问。但是它在随机访问方面比较差。
    下面先介绍
    ArrayList
    对于ArrayList的方法可以先看下代码:
class Word{
	String w;
	Word(String w){
		this.w=w;
	}
	public String toString(){
		return w;
	}
}
class WordD extends Word{ 
	WordD(String w) {
		super(w);
		// TODO Auto-generated constructor stub
	}
	
	public String toString(){
		return w+"D";
		
	}
	
}
public class ListTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List<Word> ws = new ArrayList<Word>();
		Word a = new Word("A");
		Word b = new Word("B");
		Word c = new Word("C");
		Word d = new WordD("D");
		Word e = new Word("E");
		//增
		ws.add(a);
		ws.add(b);
		ws.add(c);
		ws.add(d);
		System.out.println("1:"+ws);
		//删
		System.out.println("2:"+"e:"+ws.remove(e)+"  b:"+ws.remove(b));
		System.out.println("3:"+ws);
		
		//插
		ws.add(2, e);
		System.out.println("4:"+ws);
		//查		
		System.out.println("5:"+ws.get(0)+":"+ws.indexOf(ws.get(0)));
		//截取
		List<Word> sub = ws.subList(1, 3);
		System.out.println("6:"+sub);
		System.out.println("7:"+ws.containsAll(sub));
		//清空
		ws.clear();
		System.out.println("8:"+ws);
	}

}

运行结果:

1:[A, B, C, DD]
2:e:false  b:true
3:[A, C, DD]
4:[A, C, E, DD]
5:A:0
6:[C, E]
7:true
8:[]

看了代码应该对ArrayList有一个大致的了解,下面仔细说下:

  • add方法:在list中添加对象,可以是子类的对象。可以在参数中设置添加的位置,作为插入来使用。
  • remove方法:将要移除的对象的引用传递给remove方法,就可以在List中移除该对象。删除成功返回true,要是想删除的对象不在List中,则返回false。
  • get方法:根据索引获得对应的在List中的对象。
  • indexOf方法:获得对象在List中的索引位置。
  • sublist方法:在List中截取一段,有两个参数,一个起始位置的索引,另一个是结束位置的索引,需要注意的是,截取出来的内容包括起始位置,但是不包括结束位置。
  • clear方法,清空List。

常用的方法基本就这些,还有一些其他的方法,在了解基本的这些后,也很容易看出来怎么用,比如addAll方法,显然就是add方法的变形,参数为一组对象就可以了。
接下来介绍
LinkedList
LinkedList也像ArrayList一样实现了基本的List接口,但是它执行某些操作时比ArrayList更高效,但是在随机访问方面更逊一筹。
Linked还添加了可以使其用作栈、队列或双端队列的方法。
这些方法中有些彼此之间只是名称的差异,或者存在些许差异。例如,getFirst和element完全一样,都是返回表头,而不移除它,如果为空,则跑出异常,而peek只是稍有差异,为空时返回null。
下面展示各方法的差异性:

class Word{
	String w;
	Word(String w){
		this.w=w;
	}
	public String toString(){
		return w;
	}
}
class WordD extends Word{ 
	WordD(String w) {
		super(w);
		// TODO Auto-generated constructor stub
	}
	
	public String toString(){
		return w+"D";
		
	}
	
}
public class ListTest {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		LinkedList<Word> ws = new LinkedList<Word>();
		Word a = new Word("A");
		Word b = new Word("B");
		Word c = new Word("C");
		Word d = new WordD("D");
		Word e = new Word("E");
		ws.add(a);
		ws.add(b);
		ws.add(c);
		ws.add(d);
		System.out.println("getFirst:"+ws.getFirst());
		System.out.println("element:"+ws.element());
		System.out.println("peek:"+ws.peek());
		
		System.out.println("remove:"+ws.remove());
		System.out.println("removeFirst:"+ws.removeFirst());
		System.out.println("poll:"+ws.poll());
		System.out.println("ws:"+ws);
		
		ws.addFirst(e);
		System.out.println("addFirst:"+ws);
		ws.offer(a);
		System.out.println("offer:"+ws);
		
		ws.add(c);
		System.out.println("add:"+ws);
		
	}

}

输出结果:

getFirst:A
element:A
peek:A
remove:A
removeFirst:B
poll:C
ws:[DD]
addFirst:[E, DD]
offer:[E, DD, A]
add:[E, DD, A, C]

猜你喜欢

转载自blog.csdn.net/qq4131533523/article/details/86350612