You can pay attention to the author's account and the Java notebook from scratch. You can also go to the author's blog Garden to learn from the catalog. This film will be based on the black horse programmer job class video for learning and data sharing, and take notes and their own views. Welcome to study and discuss together.
[learn Java notes from scratch] directory
Architecture of collections
Because of different data structures (data organization and storage mode), Java provides us with different sets, but different sets have similar functions. They are constantly afraid of upward extraction and extract the commonness, which is why the set architecture is formed
Architecture:
How to learn? Start at the top level, because the top level contains all the commonalities
How to use? Use the lowest layer, because the lowest layer is the concrete implementation
Collection -> List -> ArrayList
Collection class
Common functions of Collection class
- boolean add(E e)
void clear()
boolean contains(Object o)
boolean isEmpty()
boolean remove(Object o)
int size()
Object[] toArray()
import java.util.ArrayList; import java.util.Collection; public class CollectionDemo { public static void main(String[] args) { //Create collection object //Collection c = new Collection();//Collection is an interface and cannot be instantiated Collection c = new ArrayList();//Polymorphic, parent references to child objects //boolean add(E e) System.out.println(c.add("hello"));//You can always add successfully because ArrayList allows repetition System.out.println(c.add("world")); //void clear(): empty collection //c.clear(); //boolean contains(Object o): determines whether a specified element is contained in a collection //System.out.println(c.contains("java")); //boolean isEmpty(): empty or not //System.out.println(c.isEmpty()); //boolean remove(Object o): delete elements //System.out.println(c.remove("java")); //int size(): returns the number of elements in the collection //System.out.println(c.size()); //Object[] toArray(): converts a collection to an array of object type Object[] objs = c.toArray(); for (int i = 0; i < objs.length; i++) { System.out.println(objs[i]); } System.out.println(c); } }
Traversal of sets
1.toArray(), you can convert a collection into an array, and then traverse the array
2.iterator() can return an iterator object. We can iterate the collection through the iterator object
3.foreach method
4. Cannot use for loop, because the Collection class has no index, but the subclass of collaboration can, because they have index
Iterator: can be used to traverse a collection
-
E next(): returns the next element
Note: Exception in thread "main" java.util.NoSuchElementException use the next method to get the next element. If there is no element to get, NoSuchElementException will appear
-
boolean hasNext(): judge whether there are elements to get
import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public class InterationDemo { public static void main(String[] args) { // method(); // method1(); } private static void method1() { //Create collection object Collection c = new ArrayList(); //Additive elements c.add("hello"); c.add("world"); c.add("java"); //Get iterator object Iterator it = c.iterator(); //Object next(): returns the next element //boolean hasNext(): judge whether there are elements to get while(it.hasNext()) { System.out.println(it.next()); } } private static void method() { //Create collection object Collection c = new ArrayList(); //Additive elements c.add("hello"); c.add("world"); c.add("java"); //Get array Object[] objs = c.toArray(); //Traversing array for (int i = 0; i < objs.length; i++) { System.out.println(objs[i]); } } }
Class List
Features of List
- Orderly (the order of storage and read is the same)
With integer index
Allow duplicate
Unique functions of List (add, delete, modify and query)
- void add(int index, E element)
E get(int index)
E remove(int index)
E set(int index, E element)
import java.util.ArrayList; import java.util.List; public class ListDemo { public static void main(String[] args) { //List objects created List list = new ArrayList(); //void add(int index, E element): add the specified element at the specified index location list.add(0, "hello"); list.add(0, "world"); list.add(1, "java"); //E get(int index): return elements according to the index //ergodic for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } System.out.println("-----------------"); //E remove(int index): delete the specified element and return System.out.println(list.remove(2)); System.out.println("-----------------"); //E set(int index, E element): replace the element at the specified index position with the specified element, and return the original element System.out.println(list.set(0, "android")); System.out.println(list); } } //Output result world java hello ----------------- hello ----------------- world [android, java]
Subclass of List
Common subclasses of List:
ArrayList: array structure at the bottom, fast query, slow addition and deletion
L inkedList: the underlying structure is linked list, which is slow to query and fast to add and delete
How do I choose to use different sets?
If there are more queries and fewer additions and deletions, use ArrayList
If there are fewer queries and more additions and deletions, use LinkedList
LinkedList features
- void addFirst(E e)
void addLast(E e)
E getFirst()
E getLast()
E removeFirst()
E removeLast()
public class LinkedListDemo { public static void main(String[] args) { LinkedList list = new LinkedList(); list.add("hello"); list.add("world"); System.out.println(list); System.out.println("-----------------"); // void addFirst(E e): add element to location with index 0 // void addLast(E e): adds an element to a location with an index of size()-1 list.addFirst("java"); list.addLast("android"); System.out.println(list); System.out.println("-----------------"); // E getFirst(): get element with index 0 // E getLast(): get the element with index size()-1 System.out.println(list.getFirst()); System.out.println(list.getLast()); System.out.println("-----------------"); // E removeFirst(): delete the element with index 0 and return // E removeLast(): delete the element with index size()-1 and return System.out.println(list.removeFirst()); System.out.println(list.removeLast()); System.out.println(list); System.out.println("-----------------"); } } //Output result [hello, world] ----------------- [java, hello, world, android] ----------------- java android ----------------- java android [hello, world] -----------------
Class ArrayList
Characteristics of set class: variable length
ArrayList: implementation of variable size array: is a special data type, generic. Where E appears, we can replace it with reference data type, for example: arrayl ist, arrayl ist
Construction method
ArrayList<String> array = new ArrayL ist<String>();
Member method
Additive elements
Public Boolean add (e e e): add elements
public void add(int index,E element): add an element at the specified index
Get elements
public E get(int index): returns the element at the specified index
Set length
public int size(): returns the number of elements in the collection
Delete elements
public boolean remove(0bject o): the specified element of the volume balance, which returns whether the deletion is successful
public E remove(int index): delete the element at the specified index and return the deleted element
Modifying elements
public E set(int index,E element): modify the element at the specified index to return the modified element
Here is a simple example of the old student management system. However, due to its long length, it will not be written here. For details, please look for it in the corresponding chapter blog.