[bxd学习java基本点]8.linkedList的一个封装,可为堆栈及队列。++++一句话来看continue与break++++ArrayList除重复的标准写法+++++++++

day14-09
linkedList
堆栈 先进先出,如同一个杯子;
队列 先进先出 fisrt in first out 如同水管。

removeLast;removeFirst

 

package test.bixiangdong;



import java.util.LinkedList;



public class Test {

	private LinkedList link;

	Test(){

		link=new LinkedList();

	}

	//add an element

	public void add(Object obj){

		link.add(obj);

	}

	//first in first out to get an element

	public Object get(){

		return link.removeLast();

		//removeLast;first out first in;removefirst first in first out

	}

	//whether the link is empty

	public boolean isNull(){

		return link.isEmpty();

	}

	/**

	 * @param args

	 */

	public static void main(String[] args) {

		// TODO Auto-generated method stub

		

		Test t=new Test();

		t.add("java1");

		t.add("java2");

		t.add("java3");

		t.add("java4");

		while(!t.isNull()){

			System.out.println(t.get());

		}

	}



}


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

contiue与break一看便知

 

	public static void continueTest(){

		for(int i=0;i<5;i++){

			if(i==2){

				continue;

			}

			System.out.println(i);

			//the result:

			//0

			//1

			//2

			//3

			//4

		}

	}

	public static void breakTest(){

		for(int i=0;i<5;i++){

			if(i==2){

				break;

			}

			System.out.println(i);

			//the result:

			//0

			//1

		}

	}


 

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

ArrayList除重复元素的标准写,注意next不要发生最后一个元素为空的异常

 

package test.bixiangdong;



import java.util.ArrayList;

import java.util.Iterator;

import java.util.LinkedList;



public class Test {

	

	public void singleElement(ArrayList al){

		

		ArrayList alNew=new ArrayList();

		Iterator it=al.iterator();

		while(it.hasNext()){

			

			Object obj=it.next();

			if(!alNew.contains(obj)){

				//alNew.add(it.next());//NoSuchElementException  ——————————next()一定要注意,因为你取了二次,最后一次必为空

				alNew.add(obj);

			}

		}

		sop(alNew);

	}

	public static void sop(Object obj){

		System.out.println(obj);

	}

	public static void main(String[] args) {

		// TODO Auto-generated method stub

		Test t=new Test();

		ArrayList al=new ArrayList();

		al.add("java01");

		al.add("java01");

		al.add("java02");

		al.add("java03");

		al.add("java04");

		t.singleElement(al);

	}



}


 


 

你可能感兴趣的:(LinkedList)