04-JavaSE [common API s, Collection sets, Iterator iterators]

1, Packaging

Concept: eight basic data types in java are encapsulated into classes (wrapper classes)

Packaging

  • 8 types: Byte, Short, Integer, Long, Float, Double, Character, Boolean

  • Function: encapsulate based on 8 basic data types, and provide some functions related to the encapsulation type

    • //Maximum value of Long type:
      Long.MAX_VALUE;//Use static constants in wrapper classes
      
    • Role in development:

      • Realize the exchange between String type and basic data type
  • Key points: the interchange between String type and basic data type

    • Basic data type = > string

      • int num=100;
        String s = num+"";//The simplest way: use + ""
        
    • String = > basic data type

      • //Use the static method in the wrapper class object: parseXxx(String s)
        String s ="3.14";
        double num = Double.parseDouble(s); 
        
  • Question: can basic data types and wrapper classes be used with each other?

    • Answer: Yes. Automatic packing and unpacking

Case Integer class:

//Construction method
Integer num1 = new Integer(100);//100 stored in num1
Integer num2 = new Integer("123");//Convert string "123" to integer 123


//Use: (use packaging as a common variable)
num1 = num1 + 100;



//A wrapper class can be assigned without a constructor
Integer num3 = 100;
//operation
num3 = num3 + 1;
//Usually decompile to view the source code:

public class IntegerDemo1
{
	public IntegerDemo1()
	{
	}

	public static void main(String args[])
	{
        //Boxed operation: static method in Integer class: valueof (numeric)
		Integer num3 = Integer.valueOf(100);
        //Unpacking operation: Method in Integer class: intValue()
		num3 = Integer.valueOf(num3.intValue() + 1);
		System.out.println(num3);
	}
}

//Conclusion: it is not necessary to write the code for packing and unpacking
 You only need to use the wrapper class type as a normal data type

Question 1: how to convert normal type data to String type?

Simplest approach: use+""    //Add any type of data and String type, and the result is String

Question 2: convert String type to normal type data?

Use static methods in the wrapper class: parseXxx(String str)
Xxx: Int,Float,Double,Long,Boolean
    Example:
        int num = Integer.parseInt("122")
        double num = Double.parseDouble("3.14");

//Note: there is no parseXxx() method in the character class
String => char
    String str="a";
    char ch = a.charAt(0);

2, String class

  • String class: used to store data in string format
    • Instantiation: public String(), public String(String s), public String(char[] chs)
      • The simplest method: String s= "aaa";
    • Common methods:
      • Judgment mode
      • Acquisition method
      • Interception method
      • Replacement method
      • Cutting method
      • Conversion method
String s1=" itcast ";
String s2="itcast";

boolean result = s1.equals(s2)//false 
                 s1.trim()//Remove front and back spaces
                 s1.trim().eqauls(s2)//true
    
 //split("cut symbol")
 String str="Zhang San,23,male,2020-03-20,13800138000";
 String[] strs = str.split(",");//Zhang San 23, male, March 20, 2020 13800138000
         //strs={"Zhang San", "23", "male", "2020-03-20", "13800138000"}

3, Arrays class

  • Class: a tool class that operates on arrays
  • Instantiation: no instantiation required
  • Common methods:
    • Sorting: sort (array)
    • Convert array to string: toString()

4, Large integer, large floating point class

  • BigInteger class

    • Class: large integer (used to store integer values beyond the range of integers)
    • Instantiation: BigInteger num = new BigInteger("12312312312313123123132")
    • Common methods:
      • Add, subtract, multiply, divide
  • BigDecimal class

    • Class: large floating point (used to store high-precision decimals)
    • Instantiation: BigDecimal num = new BigDecimal("3213123123.123123123123123123123123123123123123");
    • Common methods:
      • Add, subtract, multiply, divide
        • Division: exception occurs when division is not complete
          • Solution: it is necessary to specify the decimal digits after division and the choice method

5, Regular expression

Regular expression:

  • A rule based on a combination of specific symbols to verify that a string conforms to a standard
    • Example: mobile number string, ID number string
      • The mobile phone number cannot be given any characters at will. It must be given according to the rules of the mobile phone number
        • Regular: Rules for constraining mobile number strings

Conclusion: regular expressions are used to manipulate strings

Methods in the String class:

public boolean matches(String Regular expression string) 
    //Function: used to verify whether the string object conforms to the specified regular expression rules
    
Example:
    String s="add";
    boolean flag = s.matches("[a-z]ad");//Result: false
System.out.print("ab\nc");//Result: ab newline c

//You want to output: "ab\nc"
System.out.print("ab\\nc")
    
split("\\.")    //The particularity of grammar

6, Collection collection

Tips for learning Collections:

  • Know what data structure is used at the bottom of each collection object
    • Only when you understand the data structure can you know which set should be used to store data during development
  • Functions in the set: add, delete, modify, query and traverse

Collection collection

  • java.uilt.Collection interface (cannot be instantiated)

    • Is the top-level collection in the collection framework
  • Instantiation: instantiate using subclasses

  • Common methods:

    • Add element

      • public boolean add(Object obj)
    • Delete element

      • public boolean remove(Object obj)
    • Modify element

      • No,
    • Find elements

      • No,

7, Iterator iterator

effect:

Used to traverse the Collection. The purpose of learning iterators is to traverse collections

What is the function of iterators?

Traversal set

Please briefly describe the steps to use iterators?

1. get the iterator object of the collection first

2. use the iterator object to call hasNext() to determine whether the next element exists

3. if the next() method is called, get the next element

4. loop through steps 2 and 3 until the hasNext method returns false.

Considerations for using iterators
1. Cannot continue after iterator iterates elements next Get element,
Otherwise, it will report: NoSuchElementException

2. Collection objects cannot be used to add or remove elements while iterators are in use. Will cause error
ConcurrentModificationException. If you want to delete, you can use the iterator to delete

8, Enhanced for loop

Enhanced for loop (foreach), specifically used to traverse a set or array, and the underlying implementation uses iterators.

For (variable type variable: array / set){

//Processing data

}

Note: variables are the elements obtained in each cycle, and variable types are the element types of arrays or collections

After that, you cannot continue to get the element next,
Otherwise: NoSuchElementException will be reported

  1. Collection objects cannot be used to add or remove elements while iterators are in use. Will cause error
    ConcurrentModificationException. If you want to delete, you can use the iterator to delete
#### 8, Enhanced for loop

enhance for Cycle( foreach),It is specially used to traverse collections or arrays, and the underlying implementation uses iterators.



for( Variable type variable : array/aggregate ) { 

 //Processing data

}

Note: variables are the elements obtained in each cycle, and variable types are the element types of arrays or collections



**IDEA Quick build enhancements for Loops: Collections/array.for** 

Tags: Java JavaSE Back-end string

Posted by EsOne on Fri, 03 Jun 2022 10:49:26 +0530