Collection set
The length of the array is fixed, and the length of the set is variable.
Arrays store elements of the same type, which can store basic data type values. Collections store objects. And the types of objects can be inconsistent.
Set framework
import java.util.Collection; import java.util.ArrayList; //Common methods of Collection public class CollectionDemo{ public static void main(String[] args){ //Collection is an interface that cannot create objects and can use polymorphism to use methods Collection<String> collection = new ArrayList<>(); //Add method to add elements to the collection collection.add("Li Bai"); //collection.add(String); you cannot add an unspecified type. collection.add("Su Shi"); collection.add("Du Fu"); collection.add("Wang Anshi"); collection.add("Li Qingzhao"); System.out.println(collection); //remove method, the return value type is boolean, deleting the given object //collection.remove("Lin Daiyu"); //System.out.println(aremove); if not, return false collection.remove("Li Qingzhao"); //Determine whether a given object is included in the current collection and the return value type is boolean boolean acontains = collection.contains("Li Qingzhao"); System.out.println(acontains);//false //size, returns the number of elements in the collection int asize = collection.size(); System.out.println(asize);//4 //isEmpty, to determine whether the collection is empty, the collection is empty, returns true, not empty, returns false boolean aisEmpty = collection.isEmpty(); System.out.println(aisEmpty);//false //toArray, which stores elements in a collection into an array with the return value type Object [] array Object[] atoArray = collection.toArray(); //foreach for(int i=0;i<atoArray.length;i++){ System.out.println(atoArray[i]); } //Clean the array clear collection.clear(); System.out.println(collection); } }
Iterator interface
The Iterator interface has two common methods
boolean hasNext() If there are still elements to iterate over, return true to determine if there is another element in the set.
E next() returns the next element of the iteration
Remove the next element in the collection
Iterator iterator is an interface that can not be used directly. Obtaining the Iterator interface to implement the object of the class can be achieved through the method iterator () in the Collection interface; this method returns the implementation class object of the iterator.
The steps of using the iterator are as follows:
1. Use iterator() in Collection set to get the implementation class object of iterator, and use Iterator interface to receive it.
2. Use the method hasNext in the Iterator interface to determine if there is another element
3. Use the method next in the Iterator interface to extract the next element of the collection
import java.util.*; public class IteratorDemo{ public static void main(String[] args){ //You can use polymorphism to use this method Collection<String> collection = new ArrayList<>(); collection.add("Li Shangyin"); collection.add("Li Qingzhao"); collection.add("Wang Anshi"); collection.add("Su Shi"); collection.add("Bai Juyi"); //Use iterators to extract elements from a collection //To get an implementation class of Iterator first, you can use the method iterator to get it. Iterator<String> aIterator = collection.iterator(); //Determine whether a collection has elements while(aIterator.hasNext()){ String str = aIterator.next(); System.out.println(str); } } }
Enhanced for cycle
Collection interface inherits Iterable interface, which is a new feature after JDK 1.5.
format
For (collection variable name: collection name) {}
import java.util.*; public class ForeachDemo{ public static void main(String[] args){ ArrayList<String> arraylist = new ArrayList<>(); arraylist.add("Clouds think of clothes and flowers. "); arraylist.add("Spring breeze blows the sill and luxuriant. "); arraylist.add("If I hadn't seen you at the top of the mountain,"); arraylist.add("I will meet Yaotai next month. "); for(String a : arraylist){ System.out.println(a); } } }
generic paradigm
Generics are unknown data types
Define classes with generics
public class GenericDemo<E>{ private E e; public void setName(E e){ this.e = e; } public E getName(){ return e; } }
public class GenericDemoa{ public static void main(String[] args){ GenericDemo<String> gd = new GenericDemo<>(); gd.setName("Li Bai"); String str = gd.getName(); System.out.println(str); } }
//Define methods with generics public class GenericDemob{ public <E> void method1(E e){ System.out.println(e); } public static <E> void method2(E e){ System.out.println(e); } }
public class GenericDemoc{ public static void main(String[] args){ GenericDemo1 gd1 = new GenericDemo1(); gd1.method1(123); gd1.method1("Clouds think of clothes and flowers"); gd1.method1(8.8); GenericDemo1.method2("Spring breeze blows the sill and luxuriant"); } }
Generic wildcards
public static void main(String[] args){ Collection<Integer> list1 = new ArrayList<Integer>(); Collection<String> list2 = new ArrayList<String>(); Collection<Number> list3 = new ArrayList<>(Number); Collection<Object> list4 = new ArrayList<Object>(); /* Inheritance relationship between classes Integer extends Number extends Object String extends Objects */ getElement1(list1);//Compilation passes and is a subclass of Number getElement1(list2);//Error reporting, not a subclass of Number getElement1(list3);//Compilation passes, and it's Number itself. getElement1(list4);//Error reporting, not a subclass of Number getElement2(list1);//Report errors getElement2(list2);//Report errors getElement2(list3); getElement2(list4);{ } //The upper limit of a generic: this generic? Must be a subclass of Number type or Number type public static void getElement1(Collection<? extends Number> coll){} //Lower Limit of Generics: This Generic? Must be a parent of Number type or Number type public static void getElenment2(Collection<? super Number> coll)
I made a game of fighting against landlords. This case analysis was done by watching videos. After watching the analysis, I wrote the code myself. It was good or bad. I thought about it for a long time. I wrote it for 2 hours, and finally finished it.
package demo12; import java.util.ArrayList; //Make a landlord game. There are three players and one card. public class doudizhu { public static void main(String[] args) { //Create a set of players, including the bottom cards Players aplayers = new Players(); //This collection is used to hold 54 cards. ArrayList<String> pokerArray = new ArrayList<>(); //This set is used to decorate the color of the card. When it is ready, it should be combined with the number of the card. ArrayList<String> color = new ArrayList<String>(); color.add("♠"); color.add("♥"); color.add("♣"); color.add("♢"); //This collection is used to hold the number of playing cards. ArrayList<String> number = new ArrayList<String>(); number.add("2"); number.add("A"); //Generate poker cards for (int i = 2; i < 13; i++) { int j = i + 1; String str = j + ""; number.add(str); } for (int i = 0; i < color.size(); i++) { for (int j = 0; j < number.size(); j++) { String str1 = color.get(i) + number.get(j); pokerArray.add(str1); } } //Add the King and King to the poker Array collection, and the cards are generated here. pokerArray.add(0, "king"); pokerArray.add(1, "Xiao Wang"); //Transitive set aplayers.play(pokerArray); } }
import java.util.ArrayList; import java.util.Collections; public class Players { public void play(ArrayList<String> arrayList) { //Disorder incoming collections Collections.shuffle(arrayList); //Collection of players ArrayList<String> player1 = new ArrayList<String>(); ArrayList<String> player2 = new ArrayList<String>(); ArrayList<String> player3 = new ArrayList<String>(); ArrayList<String> player4 = new ArrayList<String>(); //Licensing for(int i=0;i<arrayList.size();i++) { //Why subtract three, because three cards are added to the deck, and the cards with index 51, 52, 53 are added to the deck? if(i%3==0&&i<arrayList.size()-3){ String p1=arrayList.get(i); player1.add(p1); }else if(i%3==1&&i<arrayList.size()-2){ String p2 =arrayList.get(i); player2.add(p2); }else if(i%3==2&&i<arrayList.size()-2){ String p3 = arrayList.get(i); player3.add(p3); }else{ String p4 = arrayList.get(i); player4.add(p4); } } //Display board System.out.println(player1); System.out.println(player2); System.out.println(player3); System.out.println(player4); } }
Data organization
Stack: First in, last out, with only one entrance and exit
Queue: First in, first out, one entrance, one exit
Array: The address in the array is continuous and can be queried directly according to the index value. The query is fast and the increase or deletion is slow.
Link list: slow query: the address in the list is not continuous, and every query must start from scratch.
Increase or delete faster: the structure of the linked list, add or delete an element, the overall structure of the linked list has no impact, so increase or delete faster.
Red and Black Trees: Quick Query
List set
`
import java.util.*; //List collection inherits from Collection interface //List set is an ordered set, which is stored in 1, 2, 3 and taken out in 1, 2, 3. //List collections can store duplicate elements //List collection is an indexed collection public class ListDemo{ public static void main(String[] args){ //Take a look at the common methods of List collections //List collection is an interface that needs to use polymorphism to invoke its methods List<String> aList = new ArrayList<>(); aList.add("Clouds think of clothes and flowers, "); aList.add(" Spring breeze blows the sill and luxuriant."); aList.add("If I hadn't seen you at the top of the mountain, "); //Change the specified element, add it, and change it to the specified location in the collection aList.add(3,"I will meet Yaotai next month. "); //Returns the element at the specified location in the collection String str1 = aList.get(0); System.out.println(str1); //Remove the element in the list at the specified location and return the removed element String str2 = aList.remove(1); System.out.println(str2); //Replace elements in the set with specified elements at specified locations, returning elements before updating values String str3 = aList.set(1,"Spring breeze does not pass through Yumen Gate"); System.out.println(str3); System.out.println(aList);//[Clouds think of clothes and flowers, spring breeze does not pass through Yumen Gate, will meet Yao Tai on the next moon. ] //Ergodic set for(int i = 0;i<aList.size();i++){ String str = aList.get(i); System.out.println(str); } //The iterator method of the iterator List collection returns an implementation class of the Iterator interface Iterator<String> iterator = aList.iterator(); //Judge first whether there is the next element. while(iterator.hasNext()){ String str5 = iterator.next(); System.out.println(str5); } for(String s : aList){ System.out.println(s); } } }
Subclasses of List Sets
ArrayList Collection: Elements add and delete slowly, find quickly, see for querying data, traverse data
LinkedList collection:
Common Methods of LinkedList Sets
AddFirst (E); inserts the specified element at the beginning of the list
AddLast (E); adds the specified element to the end of the list
push (E); push elements into the stack represented by the sub-list, equal to addFirst
getFirst(); returns the first element of the next list
getLast(); returns the last element of this list
removeFirst(); removes the first element of this list
removeLast (); removes the last element of this list
pop(); equivalent to removeFirst
isEmpty(); Determines whether a collection contains elements, returns false if there is one, and true if there is none.