Java array
An array is a collection of data of the same data type. Java supports mostly arrays. Each basic unit of a one-dimensional array is data of a basic data type. A two-dimensional array means that each basic unit is one dimension of a one-dimensional array. Array, and so on, each basic unit of an n-dimensional array is an n-1-dimensional array of n-1 arrays. The following uses a one-dimensional array as an example to illustrate the usage of Java arrays.
1. Array declaration
Array declarations have the following two forms (the positions of square brackets are different):
int arr[];int[] arr2;
2. Array initialization
There are also two forms of array initialization, as follows (using new or not using new):
int arr[] = new int[]{1, 3, 5, 7, 9};int[] arr2 = {2, 4, 6, 8, 10};
3. Traverse the array
You can use for/foreach to traverse the array, as follows:
public static void main(String[] args) { int arr[] = new int[]{1, 3, 5, 7,9}; int[] arr2 = {2, 4, 6, 8, 10}; for (int i = 0; i < arr.length; ++i) { System.out.print(arr[i] + "/t"); // 1 3 5 7 9 } for (int x: arr2) { System.out.print(x + "/t"); // 2 4 6 8 10 } }
4. Arrays.fill() fills the array
To use the static methods of the Arrays class, you need to import the package java.util.Arrays, which defines many overloaded methods.
void fill(int[] a, int val) fill all
void fill(int[] a, int fromIndex, int toIndex, int val) fills the element at the specified index
int[] arr3 = new int[5]; for (int x: arr3) { System.out.print(x + "/t"); // 0 0 0 0 0 All initialized to 0 } System.out.println (); Arrays.fill(arr3, 10); for (int x: arr3) { System.out.print(x + "/t"); // 10 10 10 10 10 Fill all to 10 } System.out.println(); Arrays.fill(arr3, 1, 3, 8); for (int x: arr3) { System.out.print(x + "/t"); // 10 8 8 10 10 Fill in the specified index } System.out.println();
5. Arrays.sort() sorts the array
void sort(int[] a) sort all
void sort(int[] a, int fromIndex, int toIndex) sorts the elements at the specified index
int[] arr4 = {3, 7, 2, 1, 9}; Arrays.sort(arr4); for (int x: arr4) { System.out.print(x + "/t"); // 1 2 3 7 9 } System.out.println(); int[] arr5 = {3, 7, 2, 1, 9}; Arrays.sort(arr5, 1, 3); for (int x: arr5) { System.out.print(x + "/t"); // 3 2 7 1 9 } System.out.println();
6. Arrays.copyOf() copies an array
int[] copyOf(int[] original, int newLength) copies the array and specifies the new array length
int[] copyOfRange(int[] original, int from, int to) copies an array, specifying the index of the copied original array
int[] arr6 = {1, 2, 3, 4, 5}; int[] arr7 = Arrays.copyOf(arr6, 5); // 1 2 3 4 5 int[] arr8 = Arrays.copyOfRange(arr6, 1 , 3); // 2 3 for (int x: arr7) { System.out.print(x + "/t"); } System.out.println(); for (int x: arr8) { System.out.print(x + "/t"); } System.out.println();
Java string
The Java string type is the String class. The following describes how to operate strings.
1. String concatenation
String concatenation uses the "+" symbol, as in the following example:
String s = new String("Hello"); String s2 = new String("World"); System.out.println(s + " " + s2); // Hello World
2. Get the string length
To get the length of a string, use str.length(), as in the following example:
String s3 = new String("Hello Java"); System.out.println(s3.length()); // 10
3. Get the index of the specified string
To obtain the index of a specified string, use str.indexOf(substr), str.lastIndexOf(substr), as shown in the following example:
String s4 = new String("how are you"); System.out.println(s4.indexOf("o")); // 1 Find System.out.println(s4.lastIndexOf("o")) from the beginning ; // 9 Search from the end
4. Get the character at the specified index
To get the character at the specified index, use str.charAt(index), as in the following example:
String s5 = new String("Hello Java"); System.out.println(s5.charAt(4)); // o
5. Remove spaces from strings
There are many ways to remove spaces in a string. Use str.trim() or str.replaceAll(regex, replacement). You can also use the StringTokenizer class to separate strings with spaces. You need to import the package java.util.StringTokenizer before use. , the following example:
String s6 = new String(" Hello Java "); String s7 = s6.trim(); // Remove spaces at the beginning and end of the string String s8 = s6.replaceAll(" ", ""); // Replace characters All spaces in the string StringTokenizer st = new StringTokenizer(s6, " "); // Use spaces to separate strings StringBuffer sb = new StringBuffer(); while (st.hasMoreTokens()) { sb.append(st.nextToken()); } System.out.println("/"" + s6 + "/"" + "length = " + s6.length()); // "Hello Java "length = 14 System.out.println("/"" + s7 + "/"" + "length = " + s7.length()); // "Hello Java"length = 10 System.out.println("/"" + s8 + "/"" + "length = " + s8.length()); // "HelloJava"length = 9 System.out.println(" /"" + sb.toString() + "/"" + "length = " + sb.toString().length()); // "HelloJava"length = 9
6. Replace string
Replacement string can replace all substrings or the first substring, as shown in the following example:
String sr = new String("abc abd bcd"); String sr2 = sr.replace("ab", "xx"); // Replace all substrings String sr3 = sr.replaceFirst("ab", "xx") ; // Replace the first string System.out.println(sr2); // "xxc xxd bcd" System.out.println(sr3); // "xxc adb bcd"
7. String judgment
There are many situations for string equality, such as string content equality, whether to ignore case, memory address equality, string start or end judgment, etc., as shown below:
String se = new String("Summer is so Hot"); String se1 = new String("Summer is so Hot"); String se2 = new String("summer is so hot"); String se3 = se; System.out .println(se == se1); // false compares memory instead of string content System.out.println(se == se3); // true System.out.println(se.equals(se1)); // true Compare string contents System.out.println(se.equals(se2)); // false System.out.println(se.equalsIgnoreCase(se2)); // true ignore case System.out.println(se2.startsWith ("summer")); // true starts the string System.out.println(se2.endsWith("cold")); // false ends the string
8. String case conversion
Examples of string case conversion are as follows:
String sc = new String("hello WORLD"); String scl = sc.toLowerCase(); // hello world is converted to lowercase String scu = sc.toUpperCase(); // HELLO WORLD is converted to uppercase System.out.println( scl + " " + scu);
9. String separation
String delimiters are as follows:
String ss = new String("abc,def,g,h"); String[] ss2 = ss.split(","); // Separate by commas for (String x: ss2) { System.out.print( x + "/t"); // abc def gh }
10. Format string
There are many forms of string formatting, such as date formatting, time formatting, decimal conversion, etc. The use of the Date class requires the import package java.util.Date, as shown in the following example:
Date d = new Date(); System.out.println(d); // Wed Jul 22 16:00:36 CST 2015 Default format System.out.println(String.format("%tm", d)); // 07 two-digit month System.out.println(String.format("%tH", d)); // 16 Two-digit 24-hour system.out.println(String.format("%x", 256)); // 100 hexadecimal
11. Comparison of String, StringBuffer, and StringBuilder classes
String: String constant, immutable object. When the variable content is changed, a new String object is actually generated. When the variable content is changed multiple times and frequently, it will have an impact on system performance, especially causing the GC to start working. , the program speed will become very slow.
StringBuffer: String variable, thread-safe. When the variable content is changed, the same object is actually operated, and the efficiency is higher than the String type.
StringBuilder: String variable, compatible with StringBuffer, but not thread-safe. If it is single-threaded, use StringBuilder first, it is faster than StringBuffer.