Explain the array and string related knowledge in Java

Definition and use of Java array if you want to save a group of data with the same type, you can use array. Array definition and memory allocation

There are two syntax for defining arrays in Java:

Type is any data type in Java, including basic type and combined type. Arrayname is array name, which must be a legal identifier. [] indicates that the variable is an array type variable. For example:

There is no difference between the two forms, and the use effect is exactly the same. Readers can choose according to their own programming habits.

Unlike C and C + +, Java does not allocate memory for array elements when defining arrays. Therefore, there is no need to specify the number of array elements, that is, the array length, in []. Moreover, an array defined above cannot access any of its elements. We must allocate memory space for it. At this time, the operator new is used. Its format is as follows: arrayname = new type [arraysize]; Where arraysize is the length of the array and type is the type of the array. For example: demoarray = New Int [3]; Allocate the memory space occupied by three int integers for an integer array.

Generally, you can allocate space while defining. The syntax is:

For example:

Initialization of array

You can initialize the array while declaring it (static initialization) or after declaring it (dynamic initialization). For example:

Array reference

Arrays can be referenced by subscripts:

Different from C and C + +, Java needs to check the array elements to ensure security.

Each array has a length attribute to indicate its length, such as intarray Length indicates the length of the array intarray.

[example] write a piece of code that requires you to input any five integers and output their sum.

Run result: Please enter 5 integers separated by spaces: 10 20 15 25 50 the sum of all array elements is: 120 array traversal

In actual development, it is often necessary to traverse the array to obtain each element in the array. The easiest way to think of is the for loop, for example:

Output results:

However, Java provides an enhanced version of the for loop, which is specifically used to traverse arrays. The syntax is:

Arraytype is an array type (which is also the type of array elements); Varname is a variable used to save the current element, and its value will change every time you cycle; Arrayname is the array name.

Each time we loop, we will get the value of the next element in the array and save it to the Varname variable until the end of the array. That is, the value of Varname in the first loop is the 0 element, and the second loop is the 1 element For example:

The output is the same as above.

This enhanced version of for loop is also called "foreach loop", which is a special simplified version of ordinary for loop statements. All foreach loops can be rewritten into for loops.

However, if you want to use array indexes, the enhanced version of the for loop cannot. Two dimensional array

The declaration, initialization and reference of a two-dimensional array are similar to that of a one-dimensional array:

In the Java language, because the two-dimensional array is regarded as an array of arrays, and the array space is not allocated continuously, the size of each dimension of the two-dimensional array is not required to be the same. For example:

[example] calculate the product of two matrices through a two-dimensional array.

Operation results:

A few notes: the above is a static array. Once a static array is declared, its capacity is fixed and cannot be changed. Therefore, when declaring an array, we must consider the maximum capacity of the array to prevent insufficient capacity. If you want to change the capacity when running the program, you need to use array list (also known as dynamic array) or vector. It is precisely because of the fixed capacity of static arrays that they are not frequently used in actual development and are replaced by ArrayList or vector, because they often need to add or delete elements to the array in actual development, and its capacity is not easy to estimate.

Java string on the surface, a string is the data between double quotation marks, such as "weixueyuan"“ http://www.weixueyuan.net ”Wait. In Java, you can define strings using the following methods:

For example:

Strings can be connected through "+", and the "+" operation between basic data types and strings will generally be automatically converted to strings, for example:

Operation results:

String strings and arrays have one thing in common, that is, after they are initialized, their length and content remain unchanged. If you want to change its value, a new string will be generated, as follows:

This assignment expression looks a bit like a simple Solitaire, adding a "world!" directly after str String to form the final string "Hello world!". The operation principle is as follows: the program first generates the STR1 string and applies for a space in memory. It is impossible to append a new string at this time, because the length of the string is fixed after it is initialized. If you want to change it, you can only give up the original space and reapply to accommodate "Hello world!" String, and then "Hello world!" Put the string in memory.

In fact, string is Java Lang package. According to the standard object-oriented syntax, its format should be:

For example:

However, because strings are particularly commonly used, Java provides a simplified syntax.

Another reason for using simplified syntax is that there is a large waste of memory in accordance with the standard object-oriented syntax. For example, string STR = new string ("ABC"); In fact, two string objects are created, one is the "ABC" object, which is stored in the constant space, and the other is the space applied for the object STR using the new keyword. String operation

String object has many methods, which can easily manipulate strings. 1) Length() method

Length() returns the length of the string, for example:

Output results:

It can be seen that no matter letters, numbers or Chinese characters, the length of each character is 1. 2) Charat() method

The charat () method is used to obtain the specified character in the string according to the index value. Java stipulates that the index value of the first character in a string is 0, the index value of the second character is 1, and so on. For example:

Output results:

3) Contains() method

The contains () method is used to detect whether a string contains a substring, for example:

Output results:

4) Replace() method

String replacement is used to replace all specified substrings in the string, for example:

Output results:

Note: the replace () method does not change the original string, but generates a new string. 5) Split() method

Split the current string with the specified string as the separator. The result of the split is an array, for example:

Operation results:

The above only lists several common methods of string objects. For more methods and detailed explanations, please refer to the API documentation.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>