Java集合框架-List(四)

Vector现在基本已经被ArrayList取代。但是vector中有一个elements()方法,返回的是枚举类型 Enumeration<E>。

package com.test.list;
import java.util.Enumeration;
import java.util.Vector;
public class VectorDemo
{
	public static void main(String[] args)
	{
		Vector<String> list = new Vector<String>();
		list.add("Kevin");
		list.add("John");
		list.add("Clare");
		list.add("Gary");
		
		//使用elements()方法,返回的是枚举类型 Enumeration<E>
		//枚举是Vector特有的取出方式
		for(Enumeration<String> en = list.elements(); en.hasMoreElements();)
		{
			System.out.println(en.nextElement());
		}
	}
}

LinkedList底层是双向循环链表实现的。所以插入和删除效率较高,但查询较慢。

简单看看链表的形式。

双向链表

//模拟单向链表
package com.test.list;
public class NoteTest
{
	public static void main(String[] args)
	{
		Note note1 = new Note("note1");
		Note note2 = new Note("note2");
		Note note3 = new Note("note3");		
		note1.next = note2;
		note2.next = note3;	
		//显示note3的数据
		System.out.println(note1.next.next.data);
		// 新生成一个note4,将其插入到note1和note2的中间
		Note note4 = new Note("note4");
		note1.next = note4;
		note4.next = note2;
		//显示note3的数据
		System.out.println(note1.next.next.next.data);
		//将note4删除
		note1.next = note2;
		note4.next = null;
		//显示note3的数据
		System.out.println(note1.next.next.data);
	}
}
class Note
{
	String data; //存放节点数据本身
	Note next;   //存放指向下一个节点的引用
	public Note(String data)
	{
		this.data = data;
	}
}
//模拟双向循环链表
package com.test.list;
public class NoteTest2
{
	public static void main(String[] args)
	{
		DoubleNote note1= new DoubleNote("note1");
		DoubleNote note2= new DoubleNote("note2");
		DoubleNote note3= new DoubleNote("note3");
		note1.next = note2;
		note2.previous = note1;
		note2.next = note3;
		note3.previous = note2;
		note3.next = note1;
		note1.previous = note3;
		//添加note4,并将其插在note1和note2之间
		DoubleNote note4 = new DoubleNote("note4");
		note1.next = note4;
		note4.previous = note1;
		note4.next = note2;
		note2.previous = note4;
		//将note4删除
		note1.next = note2;
		note2.previous = note1;
		note4.next = null;
		note4.previous = null;
		//显示note1的数据
		System.out.println(note1.next.next.next.data);
	}
}
class DoubleNote
{
	DoubleNote previous;//前趋节点
	String data; //数据本省
	DoubleNote next; //后继节点
	public DoubleNote(String data)
	{
		this.data = data;
	}
}

LikedList的一些特有方法,例如addFirst(), removeFirst()等。



 

//使用LinkedList的方法模拟队列。
package com.test.list;
import java.util.LinkedList;

public class LinkedListDemo1
{
	public static void main(String[] args)
	{
		MyQueue queue = new MyQueue();
		queue.put("First");
		queue.put("Second");
		queue.put("Third");
		//返回First
		System.out.println(queue.get()); 
		//返回Second
		System.out.println(queue.get()); 
		//返回false
		System.out.println(queue.isEmpty());
	}	
}
class MyQueue
{
	private LinkedList list = new LinkedList();
	public void put(Object obj)
	{
		list.addLast(obj);
	}
	public Object get()
	{
		return list.removeFirst();
	}
	public boolean isEmpty()
	{
		return list.isEmpty();
	}	
}


//使用LinkedList的方法模拟栈
package com.test.list;
import java.util.LinkedList;
import java.util.List;

public class LinkedListDemo2
{
	public static void main(String[] args)
	{
		MyStack stack = new MyStack();
		stack.push("One");
		stack.push("Two");
		stack.push("Three");
		stack.push("four");
		
		for(int i = 0; i < 4 ;i++)
		{
			System.out.println(stack.size());
			System.out.println(stack.pop());
		}		
	}
}
class MyStack
{
	private LinkedList list = new LinkedList();
	public void push(Object obj)
	{
		//入栈
		list.addFirst(obj);
	}
	public Object pop()
	{
		// 出栈,指从栈顶删除该元素并返回
		return list.removeFirst();
	}
	public Object peek()
	{
		// 查看栈顶元素
		return list.getFirst();
	}
	public int size()
	{
		return list.size();
	}
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值