preface
API (Application Programming Interface): application programming interface
The following content will use the Java API: it refers to the Java classes of various functions provided in the JDK
1, String
The class String includes methods for checking individual characters of a sequence, for comparing strings, for searching strings, for extracting substrings, and for creating copies of strings with all characters translated to uppercase or lowercase.
1. Features
(1) Strings are immutable and their values cannot be changed after creation
(2) String effect is equivalent to character array (char []), but the underlying principle is byte array (byte [])
(3) Although String values are immutable, they can be shared
2. Characteristics of string object
1) For string objects created through new, each new will apply for a memory space. Although the contents are the same, the address values are different
2) As long as the character sequence is the same (order and case), no matter how many times it appears in the program code,
JVM will only create a String object and maintain it in the String pool
3. Construction method
public String(char[] chs): creates a string object according to the contents of the character array
Public string (byte [] bytes): creates a string object according to the contents of the byte array
String s = “abc”; Create a string object by direct assignment. The content is ABC
public String(): create a blank string object without any content
4. String comparison
Use = = for comparison:
Basic type: compares whether the data values are the same
Reference type: compares whether the address values are the same
equals(): a string is an object. It compares whether the contents are the same. It is implemented through the equals() method
2, StringBuffer
1. Concept
StringBuilder is a variable string class. We can regard it as a container. The variable here means that the content in the StringBuilder object is variable
2. Method
(1) Construction method
public StringBuilder(): creates a blank variable string object without any content
public StringBuilder(String str): creates a variable string object according to the contents of the string
(2) Common methods
Public StringBuilder append (any type): adds data and returns the object itself
public StringBuilder reverse(): returns the opposite character sequence
3. Conversion between StringBuilder and String
Converting StringBuilder to String: public String toString(): you can convert StringBuilder to String through toString()
String to StringBuilder: public StringBuilder(String s): you can convert a string to StringBuilder by constructing a method
4. Difference between string and StringBuffer
(1) String: the content is immutable
(2) StringBuilder: the content is mutable
3, ArayList
ArrayList:
Resizable array implementation
: is a special data type, generic.
1. Construction method
public ArrayList(): create an empty collection object
public void add(int index,E element): appends the specified element to the end of this collection
Public Boolean add (E): appends the specified element to the end of this collection
The code is as follows (example):
import java.util.ArrayList; public class project { public static void main(String[] args) { ArrayList<String> array = new ArrayList<>(); //Output set //System.out.println(array.add("hello")); array.add("hello"); array.add("world"); array.add("java"); //Adds an element at the specified location //array.add(1,"javase"); //array.add(3,"javase"); //The index of the IndexOutOfBoundsException collection is out of bounds // array.add(4,"javase"); System.out.println("array:"+array); //If there is no element, return array: []. If any, it will be displayed in [] } }
2. Common methods
public E remove(int index): deletes the element at the specified index and returns the deleted element
public E set(int index,E element): modifies 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
The code is as follows (example):
import java.util.ArrayList; public class project { public static void main(String[] args) { ArrayList<String> array = new ArrayList<>(); //Output set //System.out.println(array.add("hello")); array.add("hello"); array.add("world"); array.add("java"); //Deletes the specified element //System.out.println(array.remove("world")); //Deletes the element at the specified index //System.out.println(array.remove(1)); //Modifies the element at the specified index //System.out.println(array.set(1,"javaee")); //IndexOutOfBoundsException index out of bounds //System.out.println(array.set(3,"javaee")); //Returns the element at the specified index //System.out.println(array.get(0)); //System.out.println(array.get(1)); //System.out.println(array.get(2)); //System.out.println(array.get(3));//IndexOutOfBoundsException //Returns the number of elements System.out.println(array.size()); //Output set System.out.println("array:"+array); } }
3. Cases
General format for traversing collections
For (int i = 0; I < collection object. size(); i + +){
The collection object. get(i) is the element at the specified index
}
Store the string and traverse the code as follows (example):
import java.util.ArrayList; public class project { public static void main(String[] args) { //Create collection object ArrayList<String> array = new ArrayList<>(); array.add("hello"); array.add("world"); array.add("java"); //System.out.println(array.get(0)); //System.out.println(array.get(1)); //System.out.println(array.get(2)); //General format for (int i=0;i<array.size();i++){ String s = array.get(i); System.out.println(s); } } }
Store the student object and traverse the code as follows (example):
import java.util.ArrayList; public class project { public static void main(String[] args) { //Create collection object ArrayList<Student> array = new ArrayList<>(); Student s1 = new Student("lin",20); Student s2 = new Student("zhang",10); Student s3 = new Student("huang",23); array.add(s1); array.add(s2); array.add(s3); //General format for (int i=0;i<array.size();i++){ Student s = array.get(i); System.out.println(s); } } }
Store the student object and traverse the upgraded code as follows (example):
public class Student { private String name; private String age; public Student(){ } public Student(String name,String age){ this.name=name; this.age=age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } } import java.util.ArrayList; import java.util.Scanner; public class project { public static void main(String[] args) { //Create collection object ArrayList<Student> array = new ArrayList<>(); //In order to improve the reusability of code, we use methods to improve the program //Two explicit: return value type (void), parameter (ArrayList < student > array) addStudent(array); addStudent(array); addStudent(array); for (int i=0;i<array.size();i++){ Student s = array.get(i); System.out.println(s.getName()+","+s.getAge()+". "); } } public static void addStudent(ArrayList<Student> array){ Scanner sc = new Scanner(System.in); System.out.println("Please enter student name:"); String name = sc.nextLine(); System.out.println("Please enter student age"); String age = sc.nextLine(); Student s = new Student(); s.setName(name); s.setAge(age); array.add(s); } }
The student management system code is as follows (example):
public class Student { private String sid; private String name; private String age; private String address;//Place of residence public Student(){ } public Student(String sid,String name,String age,String address){ this.sid=sid; this.name=name; this.age=age; this.address=address; } public String getSid() { return sid; } public void setSid(String sid) { this.sid = sid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } } import java.util.ArrayList; import java.util.Scanner; public class project { public static void main(String[] args) { ArrayList<Student> array = new ArrayList<Student>(); while(true){ System.out.println("---------Welcome to the student management system----------"); System.out.println("1 Add student"); System.out.println("2 Delete student"); System.out.println("3 Modify student"); System.out.println("4 View all students"); System.out.println("5 sign out"); System.out.println("Please enter your choice:"); Scanner sc = new Scanner(System.in); String line = sc.nextLine(); switch (line){ case "1": //System.out.println("add student"); addStudent(array); break; case "2": //System.out.println("delete student"); deleteStudent(array); break; case "3": //System.out.println("modify student"); updateStudent(array); break; case "4": //System.out.println("view all students"); findAllStudent(array); break; case "5": System.out.println("Thank you for using"); //break; System.exit(0);//JVM exit } } } public static void addStudent(ArrayList<Student> array){ Scanner sc = new Scanner(System.in); String sid; while (true){ System.out.println("Please enter student ID:"); sid = sc.nextLine(); boolean flag = isUsed(array, sid); if (flag){ System.out.println("The student number you entered has been used, please re-enter!"); }else { break; } } System.out.println("Please enter student name:"); String name = sc.nextLine(); System.out.println("Please enter student age:"); String age = sc.nextLine(); System.out.println("Please enter student residence:"); String address = sc.nextLine(); Student s = new Student(); s.setName(name); s.setAge(age); s.setSid(sid); s.setAddress(address); array.add(s); System.out.println("Student added successfully!"); } public static boolean isUsed(ArrayList<Student> array,String sid){ boolean flag = false; for (int i=0;i<array.size();i++){ Student s = array.get(i); if (s.getSid().equals(sid)){ flag=true; break; } } return false; } public static void findAllStudent(ArrayList<Student> array){ if (array.size()==0){ System.out.println("No information, please add information first!"); return;//To prevent the program from executing further, return is given; } System.out.println("Student number\t\t\t full name\t\t Age\t\t Place of residence");//\t is actually the location of the tab key for (int i=0;i<array.size();i++){ Student s = array.get(i); System.out.println(s.getSid()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getAddress()); } } public static void deleteStudent(ArrayList<Student> array){ Scanner sc = new Scanner(System.in); System.out.println("Please enter the student number of the student you want to delete:"); String sid = sc.nextLine(); int index=-1; for (int i=0;i<array.size();i++){ Student s = array.get(i); if (s.getSid().equals(sid)){ index=i; break; } } if (index==-1){ System.out.println("This information does not exist, please re-enter!"); }else { array.remove(index); System.out.println("Delete student successfully!"); } } public static void updateStudent(ArrayList<Student> array){ Scanner sc = new Scanner(System.in); System.out.println("Please enter the student number of the student you want to modify:"); String sid = sc.nextLine(); System.out.println("Please enter the new student name:"); String name = sc.nextLine(); System.out.println("Please enter the new age of the student:"); String age = sc.nextLine(); System.out.println("Please enter the student's new residence:"); String address = sc.nextLine(); Student s = new Student(); s.setName(name); s.setAge(age); s.setAddress(address); int index=-1; for (int i=0;i<array.size();i++){ Student student = array.get(i); if (student.getSid().equals(sid)){ index=i; break; } } if (index==-1){ System.out.println("This information does not exist, please re-enter!"); }else { array.set(index,s); System.out.println("Modify student successfully!"); } } }
Finally, the main point is the difference between nexLine() and next(); next() ends with a space and nextLine() ends with enter
The code is as follows (example):
for (int i=1;i<num+1;i++){ System.out.println("Please enter page"+i+"Names of students:"); String name = sc.next();//The difference between next() and nextline() Student s = new Student(); s.setName(name); array.add(s); }