Learning of ArrayList collection, Vector collection and LinkedList collection

ArrayList is a sub-implementation class commonly used in the List interface

The bottom layer is a data structure array structure,

Its features are:

            Fast query, slow addition and deletion

    From a memory perspective: thread-unsafe, unsynchronized, and efficient to execute. Multithreading: synchronized: Synchronization means to solve thread safety problem sychronized (lock object) { synchronized code  shares data; } To solve thread safety problem, it can be solved through synchronization, but the efficiency is low...

    
                    




 Vector collection

The bottom layer is an array of growable objects,

Its features are:

                Fast query, slow addition and deletion;

   From the perspective of memory: thread safety, synchronization, and high execution efficiency.
 
  Unique function:
  public void addElement(Object obj)------->add(Object obj)
public Enumeration elements(): Returns the enumeration of this vector ---> Equivalent to: public Iterator iterator()
   boolean hasMoreElements( ) --->boolean hasNext() ;

Object nextElement()   --->Object next() ;

Features of the LinkedList collection:

  The bottom layer is a linked list implementation,

Its features are:

                Inquiries are slow, additions and deletions are fast;

  From the perspective of memory: thread unsafe, asynchronous, and efficient execution.
 
Unique function:
  Add function
  addFirst(Object e): Insert the specified element to the beginning of the list
addLast(object e): Add the specified element to the end of the list
  Get function:
  getFirst(): Get the first element of the list
  getLast() : Get the second element of the list. The
 
remove function
  public Object removeFirst() removes and returns the first element of this list. public Object removeLast();
 

 Each collection has its own unique methods, let's look at several methods of ArrayList first

(1) Traversal function:
1) One is the iterator() of Collection

  2) size() and get(int index) ordinary for loop

We can understand it through the following code.

public static void main(String[] args) {
		
		//Create a collection object, remember to import the package Ctrl+Shift+O
		ArrayList list = new ArrayList() ;
		
		//add element
		list.add("you") ;
		list.add("are") ;
		list.add("beautiful") ;
		
		
		// Traverse, there are two ways
		//Get iterator, import package
		Iterator it = list.iterator() ;
		while(it.hasNext()) {
			String s = (String)it.next() ;
			System.out.println(s);
		}
		
		System.out.println("*******************");
		
		for(int x = 0 ; x < list.size() ; x ++) {
			String s1 = (String) list.get(x) ;
			System.out.println(s1);
		}
		
	}//you  are  beautiful

After learning the above method, let's try to achieve the following function.

Requirement: ArrayList collection stores custom objects and traverses

Analysis: (1) Create a student class;

         (2) Implemented in the ArrayListDemo class;

public class Student {
	
	private String name ;
	private int age ;
	// no-argument constructor
	public Student() {
		super();
	}
        // constructor with parameters
	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
        //setxxgetxx method
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	
public class ArrayListDemo2 {
	
	public static void main(String[] args) {
		
		//Create ArrayList collection object
		ArrayList  array = new ArrayList();
		
		//create student object
		Student s1 = new Student("Little Swallow", 18) ;
		Student s2 = new Student("尔康", 20) ;
		Student s3 = new Student("紫薇", 17) ;
		Student s4 = new Student("永琪", 21) ;
		
                //Add to
		array.add(s1) ;
		array.add(s2) ;
		array.add(s3) ;
		array.add(s4) ;
		
		//Iterator method (we traversed in two ways, don't forget)
		Iterator it = array.iterator() ;
		while(it.hasNext()) {
			Student s = (Student)it.next() ;
			System.out.println(s.getName()+"---"+s.getAge());
		}
		
		System.out.println("********************");
		
		for(int x = 0 ; x <array.size() ; x ++) {
			Student s1 = (Student)array.get(x) ;
			System.out.println(s1.getName()+"     "+s1.getAge());
		}
	}

Next, we learn how to add and traverse elements of the Vector collection through a very simple piece of code

public static void main(String[] args) {
		
		//Create a Vector collection object
		Vector v = new Vector() ;
		
		//add element
		v.addElement("you");
		v.addElement("are");
		v.addElement("beautiful");
		
		//traverse
		//Get Enumeration: enumeration of vectors
		Enumeration en = v.elements() ;
		while(en.hasMoreElements()) {
			String s = (String)en.nextElement() ;
			System.out.println(s);
		}
	}//you  are  beautiful

Let's learn a few important methods of the LinkedList collection. I have already mentioned that it has add functions, get functions, and delete functions.

public static void main(String[] args) {
		
		//Create LinkedList collection object
		LinkedList link = new LinkedList() ;
		
		//add element
		link.add("hello");
		link.add("world");
		link.add("java");
		System.out.println("link:"+link);
		
		//addFirst(Object e): Insert the specified element at the beginning of the list
		link.addFirst("android");
		System.out.println("link:"+link);
		link.addLast("mysql");
		System.out.println(link);
		
		//getFirst(): Get the first element of the list
		System.out.println(link.getFirst());
		System.out.println(link.getLast());
		
		//public Object removeFirst() removes and returns the first element of this list.
		System.out.println("removeFirst():"+link.removeFirst());
		System.out.println(link);
		//public Object removeLast()
		System.out.println(link.removeLast());
		System.out.println(link);
	}//link:[hello, world, java]
         //link:[android, hello, world, java]
         //android, hello, world, java, mysql]
         //android
         //mysql
         // removeFirst():android
         // [hello, world, java, mysql]
         // mysql
         //[hello, world, java]

The output is as noted above .

Having learned so much, let's do a little exercise.

Requirements:
  Simulate the characteristics of the stack structure, first in, last out
  

  Analysis: We need to customize a class and use the unique functions of the LinkedList collection to operate in this class

import java.util.LinkedList;

/**
 * This is a custom stack collection class
 * Use the unique function of LinkedList in this MyStack
 *
 */
public class MyStack {
	//Member variables
	private LinkedList link ;
		
	/**
	 * Executing the parameterless construction of MyStack actually constructs a LinkedList object
	 */
	public MyStack () {
		link = new LinkedList() ;
	}
	
	//add element
	public void add(Object obj) {
		link.addFirst(obj);
	}
	
	//get element
	public Object get() {
		return link.removeFirst() ;
	}
	
	
	//judgment function
	public boolean isEmpty() {
		return link.isEmpty();
	}
	
	
}

public class MyStackDemo {
	
	public static void main(String[] args) {
		
		//Create a custom MyStack stack collection object
		MyStack my = new MyStack() ;	//LinkedList list = new LinkedList();
		
		//add element
		my.add("hello");		//LinkedList ["java","world","hello"]
		my.add("world");
		my.add("java");
		
		while(!my.isEmpty()) {
			System.out.println(my.get());
		}
	}
}//java
 //world
 //hello

After learning so much, let's look at a small exercise about the ArrayList collection

需求:给集合中添加重复的元素(字符串类型),将重复的元素从集合去除掉。

思路:
1)创建一个A集合,添加重复元素,
2)创建一个B集合,
3)遍历A集合中的元素获取到每一个元素,
A集合中判断B集合中是否包含A集合中的元素,
包含,不搭理
不包含,添加到B集合中

                4)遍历B集合。

public static void main(String[] args) {
		
		//创建一个集合
		ArrayList list = new ArrayList() ;
		
		//添加元素
		list.add("you") ;
		list.add("are") ;
		list.add("beautiful") ;
		list.add("you") ;
		list.add("are") ;
		list.add("so") ;
		list.add("beautiful") ;
		list.add("you") ;
		list.add("are") ;
		list.add("very") ;
		list.add("nice") ;
		list.add("great") ;
		
		//创建一个新的集合
		ArrayList newList = new ArrayList() ;
		
		//遍历旧集合,使用第一种遍历方式,如果现在忘记第二种遍历方式了,现在点上去复习一下哟~
		Iterator it = list.iterator() ;
		while(it.hasNext()) {
			String s = (String) it.next() ;
			
			/ / Determine whether the new set contains the elements in the old set
			if(!newList.contains(s)) {
				newList.add(s) ;
			}
		}
		
		// loop through the new collection
		Iterator it2 = newList.iterator() ;
		while(it2.hasNext()) {
			String s = (String) it2.next() ;
			System.out.println(s);
		}
	}   //you
            //are
            //beaytiful
            //so
            //beautiful
            //very
            //nice
            //great

This is one of the methods, and now there is such a requirement: it is not allowed to create a new collection to complete, what should I do?

 
 
public static void main(String[] args) {
	ArrayList list= new ArrayList();
	list.add("hello");
	list.add("world");
	list.add("hello");
	list.add("java");
	list.add("hello");
	list.add("pythe");
	list.add("you");
	list.add("great");
	/**
		* Introduce selection sort,
 		* Compare the elements corresponding to the 0 index with the elements corresponding to the following indexes in turn
 		* 	If the previous element and the latter element are duplicated, kill the latter element
 		* 1 index in turn....
 		*/
	for(int x = 0;x<list.size()-1;x++) {
		for(int y=x+1;y<list.size();y++) {
			if(list.get(x).equals(list.get(y))) {
				list.remove(y);
				and--;
				}
			}
		
		}
			Iterator it=list.iterator();
				while(it.hasNext()) {
						String s = (String)it.next();
							System.out.println(s);
						}

	}//hello
        //world
        //java
        //pythe
        //you
        //great


Okay, in this section, I'll stop here first.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325895978&siteId=291194637