Interface Basics
Packagech09; Public classJichu {//interface: A set of specifications, a standard//Collection Interface Family: List and set//the collection interface stores a set of not unique, unordered de objects//the list interface stores a set of objects that are not uniquely ordered (insert order)//ArrayList: Implements a variable-length array, allocating contiguous space in memory, and traversing elements and random-access elements is more efficient//LinkedList: The use of linked list storage method. High efficiency when inserting and deleting elements (infrequently used)//the set interface stores a unique, unordered set of objects//HashSet//TreeSet//Map Interface Family//The map interface stores a set of key-valued objects that provide a key-to-value mapping//HashMap//TreeMap}
===========================================================================================
Detailed basic knowledge focus
Packagech09;Importjava.util.List;Importjava.util.ArrayList;ImportJava.util.Iterator;Importjava.util.LinkedList; Public classTest { Public Static voidMain (string[] args) {//creates a set of variable-length arrays (default length is ten)list<object> list =NewArraylist<object>(); //using collections to manage data//1: Save DataList.add ("a"); List.add ("B"); //2: TakeSystem.out.println (List.get (0)); //3: Get the length of the collectionSystem.out.println (List.size ()); //4: DeleteList.remove (0); /****************************** Practice ***********************************/ /** Define a student class, including school number, name, age define a list set to hold student information data not less than 5*/List.add (NewStudent ("jredu", "Zhang", 12)); List.add (NewStudent ("jrwedu", "Zang", 123)); //iterating through an array for(inti = 0; I < list.size (); i++) { if(List.get (i)instanceofStudent) {Student s=(Student) list.get (i); System.out.println (S.getname ()); } } //three ways to traverseSYSTEM.OUT.PRINTLN ("******************* generic **********************"); /** Generic collection: Specifies that the collection stores data types and arrays similar to*/List<Student> students =NewArraylist<student>(); Students.add (NewStudent ("jredu", "Zhang", 12)); Students.add (NewStudent ("Jredu2", "Zhang2", 122)); //Traverse 1 for(inti = 0; I < students.size (); i++) {Student s=(Student) students.get (i); System.out.println (S.getno ()); } //Traverse 2System.out.println ("***************foreach*****************"); for(Student s:students) {System.out.println (S.getno ()); } //Traverse 3: iteratorsSystem.out.println ("*************** iterator *****************"); Iterator<Student> it =Students.iterator (); while(It.hasnext ()) {Student s=It.next (); System.out.println (S.getno ()); } /**********************************/System.out.println ("*********linkedlist**************"); LinkedList<Student> lnk =NewLinkedlist<student>(); //1, add a collection of data come inLnk.addall (students); for(Student s:lnk) {System.out.println (S.getname ()); } System.out.println ("******************************"); //2. Add a student to the headLnk.addfirst (NewStudent ("jredu003", "Wang5", 24)); for(Student s:lnk) {System.out.println (S.getname ()); } }}
=====================================================================================
Examples
Packagech09; Public classStudent {PrivateString No; PrivateString name; Private intAge ; PublicStudent () {Super(); } PublicStudent (String No, string name,intAge ) { Super(); This. No =No; This. Name =name; This. Age =Age ; } PublicString Getno () {returnNo; } Public voidSetno (String no) { This. No =No; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; }}
Packagech09;Importjava.util.ArrayList;Importjava.util.List;ImportJava.util.Scanner; Public classStudenttest { Public Static voidMain (string[] args) {List<Student> list =NewArraylist<student>(); Student[] s=NewStudent[5]; Scanner input=NewScanner (system.in); for(inti = 0; I < 5; i++) {System.out.println ("Please enter the school number"); String Num=Input.next (); System.out.println ("Please enter a name"); String name=Input.next (); System.out.println ("Please enter your age"); intAge =Input.nextint (); S[i]=NewStudent (num, name, age); List.add (S[i]); } for(inti = 0; I < 5; i++) {Student a=(Student) list.get (i); System.out.println (A.getno ()+ a.getname () +a.getage ()); } }}
The Java interface