This chapter introduces you to two predefined classes in Java: Arrays and strings.
Arrays can be divided into one-dimensional arrays, two-dimensional arrays, and multidimensional arrays, depending on the complexity of the elements being stored. Declaration format for one-dimensional arrays: array-type array-name [], such as declaring an integer array score, used to save student scores, int score[];
Array element representation: Java array index numbering starts with 0, the example declares a one-dimensional array with a length of 3, and then outputs the array's number of elements after using the for loop to output the contents of the array.
Run results
Initialization of one-dimensional arrays: arrays are initialized in two ways, the first array type array name []={value 1, value 2, value 3 ...} The second array type array name []=new array type [constant value].
Assignment of a one-dimensional array: The array is initialized to be assigned a value. Examples are as follows:
Run results
Use of arrays: the traversal of an array is the most common operation of an array operation. The example creates an array and then sequentially outputs each element in the array.
Run results
Find the maximum and minimum values for the array, as shown in the following example:
Run results
Array sorting: There are several ways to sort the array, and the most common is to use the Arrays.sort () method to sort the arrays. Examples are as follows:
Run results
Copy of array: Java provides the System.arraycope () method to copy an array, formatted as follows System.arraycope (a (source array) b (offset, that is, the index from which to start copying) C (target array) d (offset) f ( The number of elements to copy from the source array to the target array). Examples such as the following
Run results
Array padding: Java provides the Arrays.fill () method to implement. Format: Arrays.fill (the array name, the value to be populated). Example below
Run results
Comparison of arrays: Compares two arrays for equality. Using the Array.equals () method, the format is as follows Array.equals (target array A, target array b). The example is as follows:
Run results
Array traversal: Traversing an array with the Arrays.aslist () method is much easier than using the For loop output array contents. Format: arrays.aslist (array a), example below
Run results
Two-dimensional arrays: A typical multidimensional array. The declaration of a two-dimensional array is as follows array-type array name []; Initialize format array name =new array type [number of rows] [number of columns], note: The number of rows in a two-dimensional array subscript can be omitted, but the number of columns subscript cannot be omitted. Assignment of two-dimensional arrays: Array type array name [][]={row initial value}}. such as int a[][]={{1,2,3},{4,5,6}}; the example computes the sum using two-dimensional array input and output, as follows:
Run results
Multi-dimensional arrays: As long as the array is declared when the index is added to a group, such as int a[][][], three-dimensional array, int a[][][][] four-dimensional array.
String: In essence, the string is actually a char array, represented by the Java.lang.String class, which has a series of properties and methods that provide various operations on the string. In addition to the string class, Java provides a string that the StringBuffer class uses to handle variable lengths.
String String declaration: String variable name. such as String str;
Create string: There are two forms of creating strings in Java, one is to create string variables directly using string constants, and the other is to use the New keyword to create a string variable.
1. Directly use string constants directly to create string variables, such as String str= "Hello", or string str;str= "Hello";
2. Use the new keyword to create a string variable, such as
String Str=new string ();//Creates an empty string.
String Str=new string (char[]a)//Creates a string using all elements in a character array.
String Str=new string (char[] a,offset,count)//Creates a string using a subset of elements in a character array, offset represents the starting intercept position, and count represents the number of intercepts.
String Str=new string (original)//use string constants to create strings.
String connection: Concatenate multiple strings into a string using the (+) operator. Examples are as follows:
Run results
Comparison of strings:
The CompareTo () method under the String class, which compares two strings in dictionary order, returns an int value. Examples are as follows:
Run results
The Equals () method of a String object that compares the current string object to the string specified by the parameter by calling the Equals () method in the String class. Examples are as follows:
Run results
Equalslgnorecase () Method: The function and usage of the method are basically the same as the Equals function and usage, except that the Equalsignorecase () method ignores the case. Examples such as the following
Run results
String method:
The 1.length () method, which returns the length of the string, example Str.length (), Example:
Run results
2.indexOf () Method: Returns the index position of the first occurrence of the character (string) in the string, if found, returns the index value, otherwise returns-1. Example below
Run results
3.substring () method: A substring can be obtained by manipulating the string. Examples are as follows:
Run results
4.replace () Method: The Replace () method in the String class returns a new string that is not changed by replacing all occurrences of the specified substring in this string with a new string. Examples are as follows:
Results
5.startsWith () method and Endswth () method: You can determine whether a string starts or results with the specified string.
6.toLowerCase () method and toUpperCase () method: A method specifically used to convert the case of a string.
Declaration and creation of the StringBuffer class:
Because the string stored in the string object cannot be modified, the StringBuffer class is required if you require frequent additions, deletions, and modifications to the contents of the string. To create a StringBuffer object, declare it first, declare the format stringbuffer the variable name; 1. Construction method without parameters StringBuffer (), StringBuffer str =new stringbuffer (); 2. constructor with integer type parameter stringbuffer (int length), StringBuffer str=new StringBuffer (10); 3. Method of constructing a string object as a parameter StringBuffer (String str), StringBuffer str=new StringBuffer ("Hello");
The StringBuffer class is a dynamic string class that can dynamically perform edits to add, delete, insert, and so on strings.
1.append () Method: Used to append content to a string.
2.delectCharAt () method: Used to delete the character at the specified position and then form the remaining content into a new string.
3.insert () Method: Inserts the contents into the StringBuffer object and then forms a new string. It differs from the Append () method in that the insert () method inserts a value of any data type at any specified location.
4.setChart () Method: Modifies the character of the index value in the object to a new character in the specified position.
Summary: This chapter focuses on one-dimensional arrays, multidimensional arrays, and strings, with a focus on the use of one-dimensional and two-dimensional arrays, as well as the use and mastery of the methods of the string class and the StringBuffer class.
Arrays and strings (object-oriented article)