In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to use the string of Java data structure". In the daily operation, I believe that many people have doubts about how to use the string of Java data structure. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to use the string of Java data structure". Next, please follow the editor to study!
1. Basic knowledge of string
A string is a finite sequence of zero or more characters, also known as a name string.
We can know from the basic concepts of this paragraph:
Zero or more characters: indicates that the internal element type of the string is a character.
Limited: indicates that the content length of the string is limited, less than a maximum range, but within that range, the actual length is uncertain.
Sequence: indicates that the adjacent characters in the string have a relationship between the precursor and the successor.
There is no built-in string type in Java, and each string enclosed in double quotes is an instance of the String class in Java.
That is to say, String is not a data type in Java, and all strings in Java are instantiated objects of String.
String in Java
The String class in Java represents a string, and all string literals in the Java program (such as "abc") are instances of this class.
In other words, all double-quote strings in Java programs are objects of the String class. The String class is under the java.lang package, so you don't need a guide package when using it!
The most important features of String in Java are:
The String class is immutable, so once you create a String object, its value cannot be changed. We call this feature the immutability of String.
The immutability of strings
Immutability: when you reassign a string, the old value is not destroyed in memory, but opens up a new space to store the new value.
That is, once a String object is created in memory, it will be immutable, and the methods in all String classes do not change the String object itself, but recreate a new String object.
For example:
String s = "dcm"; String s = "ddccmm"
When the value of s changes, the value of ddccmm does not override dcm, but redevelops a new space to store ddccmm and point s to it.
If we modify the traversal assignment of a string containing a large number of characters in the actual development, it will produce a lot of string objects that can not be released in memory, resulting in memory garbage.
Because of the immutability of String object, if you need to modify a large number of strings, add characters, delete characters and other operations, try not to use String objects, because this will frequently create new objects and reduce the efficiency of the program.
At this point, we can use another string class in Java, StringBuilder.
When we do the problem, we usually use the String class for strings, but considering that we sometimes use the StringBuilder class, I will explain the StringBuilder class in a little more detail here.
2.StringBuilder class
StringBuilder is a mutable string class, and we can think of it as a container, where mutability means that the content in the StringBuilder object is mutable.
2.1 methods commonly used in the StringBuilder class
As you can see, a StringBuilder object can only be constructed using its constructor, unlike String, which can be created directly by String s = "123".
Because the StringBuilder class object is mutable, it is generally defined as the StringBuilder class when we need to make more changes to a string.
2.2 differences between String and StringBuilder
The String object is immutable. Each time you use one of the methods in the String class, a new string object is created in memory, which requires new space for that new object.
The StringBuilder object is a dynamic object that allows you to expand the number of characters in the string it encapsulates, but you can specify a value for the maximum number of characters it can hold, and when you modify StringBuilder, it does not reallocate space for itself until it reaches its capacity. When capacity is reached, new space is automatically allocated and capacity is doubled. That is, when a string is changed, the state of the current object is updated.
You can use one of the overloaded constructors to specify the capacity of the StringBuilder class.
2.3 conversion between String class and StringBuilder class
Convert String class to StringBuilder class
Public class String {public static void main (String [] args) {String s = "baibai"; StringBuilder S1 = new StringBuilder (s); System.out.println (S1);}}
Convert StringBuilder class to String class
Public class String {public static void main (String [] args) {StringBuilder S1 = new StringBuilder (); / / continuous connection s1.append ("abc"). Append ("efg"); String s = s1.toString (); System.out.println (s);}} 3. Initialize the String class
3.1There are two ways to initialize String objects:
/ / method 1: directly create String S1 = "super smart"; / / method 2: object creation String S2 = new String ("super smart"); String S3 = new String (); / / you can also create an empty string
Although the two methods look the same, they are different in nature.
Strings created by String are stored in a public pool, while string objects created by new are on the heap. Is there any difference between storing in a common pool (constant pool) and in a heap?
Let's give an example:
String S1 = "super smart"; / / String directly creates String S2 = "super smart"; / / String directly creates String S3 = S1; / / the same reference String S4 = new String ("super smart"); / / String object creates String S5 = new String ("super smart") / / String object creation System.out.println (System.identityHashCode (S1)); System.out.println (System.identityHashCode (S2)); System.out.println (System.identityHashCode (S3)); System.out.println (System.identityHashCode (S4)); System.out.println (System.identityHashCode (S5))
Output:
It can be seen that the addresses of the first three strings are the same, while the last two are different!
This is because when you create a string directly, you will first look for such a string in the public pool, and if so, point the reference directly to it without developing a new space. In this case, the three references to S1 and S2 point to the same block of memory in the common pool.
When an object is created, each time a new space is opened on the heap to store strings, that is, s4Die S5 points to two different chunks of memory on the heap, but both blocks of memory store the same thing.
API is commonly used in 4.String class
Again, when we come across problems related to strings, we almost always use the String class to solve the problem, except that we may temporarily use the StringBuilder class when making a lot of string changes.
The temporary thing here is that we usually convert the string to the String class after we change the string and so on.
So the API we want to learn is mainly the API of the String class. Corresponding to the API of my StringBuilder, we only need to learn the two mentioned above.
The String class is under the java.lang package, so you don't need a guide package when using it!
4.1 conversion of basic data types to strings
There are three ways:
(1) the value of basic type data + "(most commonly used, simplest)
(2) use the static method static String toString (int I) in the wrapper class to return a String object that represents the specified integer. For example, in Integer: Integer.toString (6)
(3) use the static method static String valueOf (int I) in the String class to return a string representation of the int parameter. Such as: String.valueOf (6)
The overloading of the parameter String.valueOf (), the static method that converts the basic data type to String, is already provided in the String category:
String.valueOf (boolean b) / / convert boolean variable b to string String.valueOf (char c) / / convert char variable c to string String.valueOf (char [] data) / / convert char array data to string String.valueOf (char [] data, int offset Int count) / / convert the char array data from data [offset] to String.valueOf (double d) / / convert double variable d to string String.valueOf (float f) / / convert float variable f to string String.valueOf (int I) / / convert int variable I to string String.valueOf (long l) / / convert long variable l to string String.valueOf (Object obj) / / convert obj object to string Equal to obj.toString ()
Instantiation is not required because it is a static method.
4.2 convert strings to basic data types
Generally use the static method parseXX ("string") of the wrapper class
To convert String to basic data types, you need to use wrapper categories of basic data types. For example, you can convert String to byte using Byte.parseByte (String s).
Byte.parseByte (String s) / / convert s to byte Byte.parseByte (String s, int radix) / / convert s to byteDouble.parseDouble (String s) / / convert s to double Float.parseFloat (String s) / / convert s to float Integer.parseInt (String s) / / convert s to int Long.parseLong (String s) / / convert s to long
Note that there are also static methods here, but they are all static methods corresponding to the wrapper class.
4.3 use length () to get the number of characters in a string
Int len = String.length ()
4.4 use toCharArray () to convert a string into a character array
Char [] arr = String.toCharArray ()
4.5 true/false is returned to determine whether the contents of two strings are equal.
String1.equals (String2); / / case-sensitive String1.equalsIgnoreCase (String2); / / case-insensitive
4.6 location-related strings
CharAt (int) / / get the character indexOf (String) / / corresponding to the specified subscript position / / get the subscript lastIndexOf (String) for the first occurrence of the specified content / / get the last subscript for the specified content
4. 7 splits a string into split (String) according to the specified content and returns an array of strings.
String s = "wa,dcm,nb!"; String [] str = s.split (","); / / return str [1] = dcm in the result
4.8 contains (String) determines whether a string contains the specified content and returns true/false
Boolean a = String1.contains (String2)
4.9 use substring () to intercept the string and return the substring
String.substring (int) / / truncate from the specified subscript to the last String.substring (int,int) / / from the subscript x to the element corresponding to subscript ymer1
4.10 string case conversion
String.toUpperCase () / / converts a string to uppercase String.toLowerCase () / / converts a string to lowercase
4.11 replace string contents with replace ()
String.replace (String,String) / / replace all content with specified content String.replaceAll (String,String) / / replace all content with specified content, support regular String.repalceFirst (String,String) / / replace some content that appears for the first time with specified content 5. String advance exercise
three hundred and eighty seven。 The first unique character in a string
Answer to the question:
Convert the single character of the string into the corresponding array subscript, traverse the string to get 26 letters that appear several times. Then after traversing the string to see which character appears first with the number of times 1, the corresponding subscript is output.
Class Solution {public int firstUniqChar (String s) {int len = s.length (); int [] vis = new int [26]; int temp =-1; for (int I = 0; I < len; I + +) {vis [s.charAt (I) -'a'] + +;} for (int I = 0; I < len) I + +) {if (vis.charat (I) -'a'] = = 1) {return I;}} return-1;}}
Or we can first convert the string into a character array to solve the problem, the principle is the same!
Class Solution {public int firstUniqChar (String s) {int [] arr = new int [26]; char [] chars = s.toCharArray (); for (int I = 0; I < chars.length; icards +) {arr [chars [I] -'a'] +;} for (int I = 0; I < chars.length) If (arr [chars [I] -'a'] = = 1) {return I;}} return-1;} at this point, the study on "how to use strings of Java data structures" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.