API Part 1

1. Use of API

Application Programming Interface (API) : Application Programming Interface. Java apis: Java classes that provide functionality in the JDK that encapsulate the underlying implementation. When we call a method, if the method has an explicit return value, we accept it as a variable, either manually or by using a shortcut (Ctrl+Alt+V).

2. The String class

1. Summary of the String.

The String class is in the java.lang package, so you don’t need to use the guide. The String class represents strings, and all String literals in a Java program (such as “ABC”) are implemented as instances of this class. That is, all double-quoted strings in a Java program are objects of the String class. A characteristic of strings: strings are immutable, and their values cannot be changed after creation. Although the values of the String class are immutable, they can be shared. Strings have the same effect as arrays of characters (char[]), but the underlying principle is arrays of bytes (byte []).

2.String constructor

The method name instructions
public String() Creates an empty string object with no content
public String(char[] chs) Creates a string object from the contents of the character array
public String(byte[] bys) Creates a string object from the contents of the byte array
String s = “ABC” Create a string object by direct assignment, and the content is ABC

3. Features of String objects

The string object is created by new. Each time new requests a memory space, the content is the same, but the address value is different. If the String column is the same (in order and case), no matter how many times it appears in the program code, the JVM will only create a String object and maintain it in the String pool.

4. Comparison of strings

Use == to compare basic types: Compare whether the data values are the same. Reference type: compares whether the address values are the same. Public Boolean equals(Object anObject): equals(Object anObject): compares this string to a specified Object. Since we are comparing a string object, we pass a string directly as an argument.

5. Iterate over the string

Public char charAt(int index) : returns the char value at the specified index. The index of the string also starts from 0. Public int length() : returns the length of the second string. Length of the array: array name. length of the string: string object. length()

6. User login

Requirements: known user name and password, simulated user login with the program. A total of three opportunities, login, give the corresponding prompt. (1) The user name and password are already known. (2) keyboard input to log in the user name and password, with Scanner. (3) take the keyboard input user name, password and known user name, password for comparison, give the corresponding prompt, string content comparison, using equals () method.

7. Count the number of string counts

Requirements: keyboard input a string, statistics the number of uppercase letters, small letters and digits in the string characters (do not test other characters). ② To count the number of three types of characters, we need to define three statistical variables, the initial value is 0 ③ Traversed the string, get each character ④ judge which type of the character belongs to, and then the corresponding type of statistics + 1

If ch is one character at A time, I will judge it belongs to the capital letters, write letters, or Numbers directly determine whether the characters in the corresponding scope can be capitalized mother: ch > = 'A' && ch < = 'Z' low lowercase letters: ch > = 'A' && ch < = 'Z' low Numbers: ch > = '0' && ch < = '9'

⑤ Displays the number of characters of the three types

8. Concatenation of strings

Requirements: Define a method that returns the data in an int array as a string in a specified format. Call this method and output the result in the console. For example, the array is int[]arr= {1,2,3}; , after the execution method of the output is: [1, 2, 3] : (1) define an array of type int, static initialization complete initialization of an array element (2) to define a method, is used to put int array according to the specified format joining together into a string of data in return. Return type String, argument list int[] arr. ④ Call the method, receive the result with a variable ⑤ output the result

9. String inversion

Requirement: Define a method for character reversal. Keyboard input a string, after calling the method, output results in the console, for example, keyboard input ABC, cba output results thought: ① Keyboard input a string, with Scanner implementation ② define a method, to achieve string reversal. Return a value of type String, and the parameter String s ③ iterates the String backwards through the method, then concatenates each of the resulting characters into a single character. ④ Calls the method, receives the result with a variable ⑤ and prints the result

3. The StringBuilder class

1. Summary of StringBuilder

StringBuilder is a mutable string class that we can think of as a container. By mutable, I mean that the contents of a StringBuilder object are mutable. The difference between String and StringBuilder: String contents are immutable; The StringBuilder content is variable.

2. The StringBuilder constructor

Method name: public StringBuilder() Description: Create a blank mutable string object with no content. Method name: public StringBuilder(String STR) Description: To create a mutable String object based on the contents of the String.

3. Add and reverse methods of StringBuilder

Method name: public StringBuilder Append (any type) Description: Adds data and returns the object itself. Method name: public StringBuilder reverse() For example: StringBuilder sb = new StringBuilder();

Sb. Append (" hello "). Append (" world "). Append (100); System.out.println(sb); sb.reverse(); System.out.println(sb);

Output: helloworld100

     001dlrowolleh

4. Convert StringBuildr to String

(1) Convert StringBuilder to String: Sb = new StringBuilder(); public String toString(); String s = sb.toString(); Public StringBuilder(String s) String s= “hello”; StringBuilder sb = new StringBuilder(s);

4. ArrayList collection

1. Set foundation

The characteristics of the collection class: it provides a storage model with variable storage space, and the storage data capacity can be changed. ArrayList

: resizable array implementation;

: is a special data type, generics.

2.ArrayList constructor and addition method

Method Description public ArrayList() Create an empty collection object. Public Boolean add(E E) Append the specified element to the collection. Public void add(int index, int index) E element) inserts the specified element at the specified position in this collection

3. Common methods of ArrayList collection

Public Boolean remove(Object o) Remove the specified element, Returns whether the deletion is successful. Public E remove(int index) Deletes the element at the specified index, and returns the deleted element. Public E set(int index,E element) Modifs the element at the specified index, and returns the modified element public E Get (int index) Returns the element at the specified index public int size() Returns the number of elements in the collection

4. Example: Store strings and iterate over them

Requirements: Create a collection of stored strings, store 3 string elements, use the program to achieve in the console traversal of the collection: ① Create a collection object ② add a string object in the collection ③ iterate over the collection, first of all to be able to get every element in the collection, this through get(int index) method implementation ④ iterate over the collection, second to be able to get the length of the collection, this through the size () method implementation ⑤ iterate over the general format of the collection

for(int i=0; I < collection object.size0; I + +){collection object. Get (I) is the element at the specified index}

5. Storing and traversing student objects (1)

Requirements: create a collection of storing student objects, storing three student objects, using the program to realize traversal of the collection in the console train of thought: ① Define student class ② create collection object ③ create student object ④ add student object to the collection ⑤ traversal collection, using the general traversal format

6. Storing and traversing student objects (2)

Requirements: Create a collection of student objects, store 3 student objects, use the program to implement the console traversal of the collection student name and age from the keyboard input ideas: ① Define the class of students. For the convenience of data entry on the keyboard, All the member variables in the student class are defined as String type; ② Create the collection object; ③ keyboard input the data required by the student object; ④ create the key student object; assign the keyboard input data to the member variable of the student object; ⑤ add the student object to the collection; ⑥ traversal the collection, and implement the general traversal format