Notes on the 18th learning of java

Posted by karlovac on Fri, 11 Feb 2022 14:47:41 +0100

Notes on the 18th learning of java

preface

I feel de

1, What is a Collection?

1.Collection overview

● the top-level interface of a singleton Collection, which represents - group objects, which are also called Collection elements
● JDK does not provide any direct implementation of this interface. It provides more specific implementation of sub interfaces (such as Set and List)
Create the object of the Collection
Polymorphic way
● specific implementation class ArrayList

2. Set class architecture

3. Common methods

The code is as follows (example):

import java.util.ArrayList;
import java.util.Collection;

public class Demo01Collection {
	public static void main(String []args) {
		// ArrayList<String> coll = new ArrayList<>()
		
		Collection<String> coll= new ArrayList<>(); //It embodies polymorphism
		
		System.out.println(coll);
		
		boolean b1 =coll.add("Zhang San");
		
		System.out.println(b1);
		System.out.println(coll);
		coll.add("Li Si");
		coll.add("Li Si");
		coll.add("Li Wu");
		coll.add("Li Liu");
		coll.add("Li Qi");
		
		System.out.println(coll);
		
		boolean b2=coll.remove("Li Wu");
		System.out.println(b2);
		System.out.println(coll);
		
		boolean b3=coll.remove("Li Liu");
		System.out.println(b3);
		System.out.println(coll);
		
		boolean b4= coll.contains("Li Wu");
		System.out.println(b4);
		boolean b5= coll.isEmpty();
		System.out.println(b5);
		int b6 =coll.size();
		System.out.println(b6);
		
		Object [] arr=coll.toArray();
				System.out.println(arr[0]);
		
		System.out.println("======================");
		coll.clear();
		System.out.println(coll);
		b5=coll.isEmpty();
		System.out.println(b5);
	}

}

4. Traversal of collection

Iterator: iterator, a special traversal method of a collection
Iterator iterator(): returns the iterator of the elements in this collection, which is obtained through the iterator0 method of the collection

The iterator is obtained through the iterator() method of the set, so we say that it depends on the set
Common methods in Iterator

E next0: returns the next element in the iteration
● boolean hasNext(): returns true if the iteration has more elements

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;





public class Demo01Iterator {
	public static void main(String[] args) {
		Collection<String> coll = new ArrayList<>();
		
		coll.add("Yao Ming");
		coll.add("McGrady");
		coll.add("Curry");
		coll.add("James");
		coll.add("Irving");
		coll.add("Iverson");
		
		Iterator<String> it =coll.iterator();
		
		if(it.hasNext()) {
			String e=it.next();
			System.out.println(e);
		}
		System.out.println("=======================");
		
		while(it.hasNext()) {
			String e=it.next();
			System.out.println(e);
		}
		
		System.out.println("=======================");
		
		for(Iterator<String> it2=coll.iterator();it2.hasNext();){
			String e=it2.next();
		System.out.println(e);
		}
		
		System.out.println("==========third time=============");
		//Enhanced for loop
		for(String s:coll) {
			
			System.out.println(s);
		}
		
		System.out.println("==========Fourth time=============");
		int[] arr1= {4,5,6,7,8,9,10};
		for(int i: arr1) {
			System.out.println(i);
		}
				
	}
	

}

2, List

1.List set overview

● there is a set (also known as a sequence), which can accurately control the insertion position of each element in the list. Donkey can access elements through integer index,
And search for elements in the list
● unlike Set sets, lists usually allow duplicate elements
List set features
● order: the elements stored and taken out are in the same order
Repeatable: stored elements can be repeated

2.List set specific methods

3. Listlterator

Listiterator: list iterator
● it is obtained through the listliterator0 method of the List set, so it is a unique iterator of the List set
● in the list iterator that allows the programmer to traverse the list in either direction, modify the list during the iteration and obtain the current position of the iterator in the list
Common methods in listlter

E next(): returns the next element in the iteration

boolean hasNext(): returns true if the iteration has more elements

E previous(): returns the previous element in the list

boolean hasPrevious(): returns true if this list iterator has more elements when traversing the list in the opposite direction
● void add (E): insert the specified element into the list

3, Abnormal

1. Exception overview

2. try...catch...


Execution process:
The program starts from the code of the try face
If an exception occurs, an exception class object will be automatically generated, and the exception object will be submitted to the Java runtime system
When an exception is found in the Java class, the system will find the matching exception when the exception is received
After execution, the program can continue to execute

package Demo02;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Demo02Exception {
	public static void main(String[] args) {
		//Exception during compilation
		SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
		
		Date date =null;
		//Abnormal operation
		System.out.println("First sentence test");
		try {
			date = sdf.parse ("2021-05-11");
		} catch (ParseException e) {
			
			e.printStackTrace();
		}
		
		System.out.println(date);
		System.out.println("Second sentence test");
		
		//Abnormal operation
		int [] arr= {1,122,3};
		System.out.println(arr[0]);
		//There is a problem, but there will be no prompt, because the compilation can pass, and there will be exceptions during execution
		try {
			System.out.println(arr[2]);

		}catch(Exception e) {
			System.out.println("First sentence test");
			System.out.println(e);

		}
		//Error error
		int [] arr2= new int[1024*10000000];
		System.out.println("Subsequent code");
	}

}

3.throw and throws


Note: this format follows the parentheses of the method
Exceptions must be handled during compilation. There are two handling schemes: try... catch Or throws. If the throw scheme is adopted, who will call and who will handle it in the future
Run time exceptions can not be handled. After a problem occurs, we need to come back and modify the code

Exceptions in Java are divided into two categories: compile time exceptions and run-time exceptions, also known as checked exceptions and non checked exceptions
All RuntimeException classes and their subclasses are called runtime exceptions, and other exceptions are compile time exceptions

Compile time exception: the processing must be displayed, otherwise the program will have an error and cannot be compiled
Run time exception: no display processing is required, and it can also be handled like compile time exception

package Demo02;
//Five keywords of java exception handling: try catch finally throw throws
public class Demo02Throw {
	public static void main(String[] args) {
		int[] arr=null;
		//int[] arr=new int[3];
		
		int e =getElement(arr,3);
		System.out.println(e);
	}
	
	public static int getElement(int[] arr, int index) {
		
		if (arr==null) {
			throw new NullPointerException("The array value passed is NULL");
		}
		
		if(index<0||index<arr.length-1) {
			throw new ArrayIndexOutOfBoundsException("The index passed is outside the normal range of use of the array");
		}
		
		int ele=arr[index];
		return ele;
		
	}

}

package Demo02;

import java.io.FileNotFoundException;
import java.io.IOException;

public class Demo02Throws {
	public static void main(String[] args)  throws Exception{
		readFile("c:\\a.txt");
		System.out.println("Subsequent code");
	}
	
	public static void readFile(String fileName) throws FileNotFoundException,IOException{
		if(!fileName.equals("c:\\a.txt")) {
            throw new FileNotFoundException("The file passed is not c:\\a.txt");
	}
		
		if(!fileName.endsWith(".txt")) {
		    throw new IOException("Is there a nickname after the file");	
		
		}
		System.out.println("File OK");
		}

}

Topics: Java