Summary of the contents of learning materials eighth Chapter string class
Java specifically provides a string class to handle character sequences.
The string class is in the Java.lang package, because the classes in the Java.lang package are introduced by default, so the program can use the string class directly.
It is important to note that Java declares the string class as the final class, so the user cannot extend the string class, that is, the string class may not have subclasses.
You can use the string class to declare an object and create an object, such as:
String s = new String("we are students");
String t = new String("we are students");
The user cannot output a reference to a string object:
Syste.out.println (s);
The output is the entity of the object.
For example:
char a[] = {‘零‘,‘壹‘,‘贰‘,‘叁‘,‘肆‘,‘伍‘,‘陆‘,‘柒‘,‘捌‘,‘玖‘}; String s = new String(a,2,4); 相当于 String s = new String("贰叁肆伍");
Common methods of the String class:
- public int Length (): Gets the length of a string
- public boolean equals (String s): Determines whether the character sequence of the current string object is the same as the character sequence of the string object specified by the parameter s
- public Boolean startsWith (String s)
Determines whether the character sequence prefix of the current string object is the character sequence of the string object s specified by the argument
- public int CompareTo (String s): Compares the size of the character sequence specified by the dictionary order with the parameter S.
- 5.public Boolean contains (string s): String object calls the Contains method to determine whether the character sequence of the current String object contains the character sequence of the parameter s
public int indexOf (String str): The string object invokes the method to retrieve the position of the first occurrence of the character sequence of STR, starting at the 0 index position of the character sequence of the current String object, and returns the position. If not retrieved, the value returned by the method is –1
public string trim (): Gets a new string object that is a sequence of characters after the character sequence of the current string object is removed from the front and back spaces.
The transformation of the character string and the basic data
The integer class in the Java.lang package calls its class method:
public static int parseInt(String s)
You can use the class method of the String class:
public static String valueOf(byte n) public static String valueOf(int n) public static String valueOf(long n) public static String valueOf(float n) public static String valueOf(double n)
The string representation of the object
All classes are by default the subclass or indirect subclass of the object class in the Java.lang package.
The object class has a
public String toString()
- How to construct the String class:
String(char[]);
String(char[],int offset,intlength)
Create a string object with all and part of the characters in the character array, respectively
- A method of storing part of a string object's sequence of characters into an array:
public void getChars(int start,int end,char c[],int offset )
Method of storing a string object's sequence of characters into an array
public char[] toCharArray()
- String (byte[]) constructs a string object with the specified byte array.
- String (byte[],int offset,int length) constructs a string object using the specified byte array, starting at offset from the beginning of the array, taking a length of bytes.
- Public byte[] GetBytes () uses the platform's default character encoding to store the character sequence of the current string object into a byte array and returns a reference to the array.
Public byte[] GetBytes (string charsetname) uses a parameter to specify a character encoding, stores the character sequence of the current String object into a byte array, and returns a reference to the array.
15th Chapter Generic Type
Generics (generics), whose main purpose is to establish a type-safe collection framework, such as linked lists, hash maps, and other data structures.
You can class
declare a class by using name < generic list > to distinguish it from ordinary classes, so that the declared class is called a generic class, such as:
class People<E>
Where is the name of the People
generic class, E
which is the generic, that is, we do not specify E
what type of data it is, it can be any object or interface, but it cannot be a basic type of data.
When a generic class declares and creates an object, the class name is followed by a pair of "<>", and you must replace the generics in "<>" with a specific type. For example:
Cone<Circle> coneOne; coneOne =new Cone<Circle>(new Circle());
LinkedList
LinkedList
Create an empty doubly linked list.
Add (E obj) adds nodes to the list in turn
LinkedList
public boolean add(E element)
Adds a new node to the end of the list that is the data specified by the parameter elememt.
- public void add(int index ,E element)
adds a new node to the list at the specified location, and the data in that node is the data specified by the parameter elememt.
public void clear()
Deletes all nodes of the linked list, making the current list an empty list.
public E remove(int index)
Deletes the node at the specified location.
- public boolean remove(E element)
Delete the first node that contains the data elemen.
- public E get(int index)
Gets the data in the node at the specified position in the linked list.
public E getFirst()
Gets the data from the first node in the linked list.
- public E getLast()
get the data from the last node in the list.
public E removeFirst()
Deletes the first node and returns the data in that node.
Linked list
public static void shuffle(List<E> list)
The data in list is re-arranged randomly by shuffling algorithm.
static void rotate(List<E> list, int distance)
Rotates the data in the linked list.
public static void reverse(List<E> list)
Flipping data in a list
Stack
public E push(E item);
Implementation of the pressure stack operation
public E pop();
Implement the stack operation.
public boolean empty();
Determine if the stack still has data.
public E peek();
Gets the data at the top of the stack, but does not delete the data.
public int search(Object data);
Get the position of the data in the stack
Problems in code debugging and the resolution process
Code debugging in the textbook have a problem first go to https://shimo.im/doc/1i1gldfsojIFH8Ip/to see, if others did not ask the same question, you can edit the document to add, and then copy their questions to the following:
- Issue 1: Compiling the Xxx.java file prompts for an untested or unsafe operation
method One: add in front of the main method@SuppressWarnings("unchecked")
method Two: is the use of generics.ArrayList<String> list = new ArrayList<String>();
For example:
import java.util.*;public class ListDemo {//@SuppressWarnings("unchecked")public static void main(String[] args) {StringBuilder buf = new StringBuilder("美丽"); //字符串生成器buf.append("中"); // 输出: 美丽中buf.append("国"); //输出:美丽中国buf.insert(1,"达"); //输出:美达丽中国buf.insert(0,"欢迎"); //输出:欢迎美达丽中国System.out.println(buf);//ArrayList list = new ArrayList();ArrayList<String> list = new ArrayList<String>();list.add("中");System.out.println(list);}}
- Issue 2: Compile XXX Complete, when running the program prompt: NO X11 DISPLAY variable is set,but This programs perforemed an operation which requires it.
- Problem 2 Solution: Translate to
没有设置X11 DISPLAY变量,但该程序执行了需要它的操作。
Online said the general saying that there is no visual graphical interface variables configured, I use the sub-system under Windows, so only the command line does not have a visual interface, such as the need for the interface to call third-party software, the situation can be viewed using virtual machine.
- Issue 3: Compile XXX Complete, run is prompt: Exception in thread "main" java.lang.NoSuchMethodError:Student.
- Problem 3 Solution: Prompt class with the same name in the same package or project, change the class name, save to run normally.
Code Hosting
Learning progress Bar
|
lines of code (new/cumulative) |
Blog Volume (Add/accumulate) |
Learning Time (new/cumulative) |
Important Growth |
Goal |
5000 rows |
30 Articles |
400 hours |
|
First week |
200/200 |
1/4 |
10/10 |
|
Second week |
300/500 |
1/5 |
10/20 |
|
Third week |
800/1300 |
1/6 |
20/40 |
|
Week Four |
700/2000 |
2/8 |
20/60 |
|
Week Five |
1100/3100 |
2/10 |
30/90 |
|
Week Six |
1100/4200 |
1/11 |
15/105 |
|
Seventh Week |
|
|
|
|
Eighth Week |
|
|
|
|
Resources
20165231 2017-2018-2 "Java Programming" 6th Week study Summary