当前位置:网站首页>List interface three sub implementation classes

List interface three sub implementation classes

2022-06-23 08:18:00 Liang FuFu

1. List Three sub implementation classes

1.1 List Set three sub implementation class characteristics

1.ArrayList: It is our common , In a single threaded program , The highest efficiency of execution .(List Set default use )
  			 The underlying structure is an array structure : Quick query , Add or delete slowly .
  			 Is a thread unsafe class , Out of sync .
2.Vector: The underlying data structure is also an array , Quick query , Add or delete slowly 
		  Is a thread safe class , Sync , Inefficient execution 
3.LinkedList: The underlying data structure is a linked list : Slow query , Additions and deletions quickly 
			  Thread unsafe class , Efficient 

1.2 Vector aggregate

1. Unique features :
		 public void addElement(E obj): stay Vector Add a new element to the end of the collection 
         public E elementAt(int index): Get the element at the specified location 
         public Enumeration<E> elements(): It's like an iterator , Unique method 
         		Enumeration Interface 
         			boolean  hasMoreElements(): Whether there are more components ( Elements ) Traversable 
         			E nextElement(): Get next element 
2.Vector There are two more traversals 
		 public E elementAt(int index) and size() Methods combined with common for
              example :for(int x=0;x<Vector.size();x++){
    
                 String s = Vector.elementAt(x);
                 sout(s);
             }
		 public Enumeration<E> elements():Vector The unique iterator operation of 
		 		boolean  hasMoreElements(): Whether there are more components ( Elements ) Traversable 
		 		E nextElement(): Get next element 
              example :Enumeration<String> enumeration = Vector.elements();
				while(enumeration.hasMoreElements()){
    
                    String s = enumeration.nextElement();
            		System.out.println(s);
                }

		

1.3 LinkedList aggregate

1. Unique features :public void addFirst(Object e): Add any type of element to the beginning of the linked list every time 
		  public void addLast(Objet e): Every time an element of any type is conditioned to the end of the linked list 
    	  public Object removeFirst() Remove and return the first element from this list .
    	  public Object removeLast() Delete from this list and return the last element 
    	  public Object getFirst(): Get the first element of the linked list 
    	  public Object getLast(): Get the last element of the linked list 
2. utilize LinkedList Realize stack function 
    public class MyStack{
    
        private LinkedList list;
        public MyStack(){
    
            list = new LinkedList<>;
        }
        public void add(Object obj){
    
            list.addFirst(obj);
        }
        public Object get(){
    
            return list.removeFirst();
        }
        public boolean isEmpty(){
    
            return link.isEmpty();
        }
    }
	public class Test{
    
        main();
        MyStack mystack = new MyStack();
        mystack.add("hello");
        mystack.add("world");
        mystack.add("javaee");
        
        while(!MyStack.isEmpty()){
    
            Object o = mystack.get();
            sout(o);
        }
    }
原网站

版权声明
本文为[Liang FuFu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230745478689.html