Today in the group to see a person talking about the interview hangs a question, the topic is an int array to string array.
Although it is the underlying problem. But it is less used in the basis of the problem, so everyone work is not how to meet, will not be very normal. Here, let's start with a string and int convert each other.
int to string (for example, two types)
<span style= "FONT-SIZE:18PX;" >int num = 2; String st = "" + num;</span>
<span style= "FONT-SIZE:18PX;" >int num = 2; String st = string.valueof (num) </span>
string to int (for example, two types)
<span style= "FONT-SIZE:18PX;" >string st = "one"; int num = integer.parseint (ST);</span>
<span style= "FONT-SIZE:18PX;" >string st = "one"; int num = integer.valueof (ST). Intvalue ();</span>
There are two ways to convert, so what good is it?
The first method: s=i+ ""; //Generates two string objects Another method: S=string.valueof (i); Static methods that use the string class directly. Produce only one object The first method: I=integer.parseint (s);//Use static methods directly, do not produce extra objects. But it throws an exception. Another method: I=integer.valueof (s). Intvalue ();//integer.valueof (s) equals the new Integer (Integer.parseint (s)) and throws an exception, but produces an additional object |
int array turns into a string array
<span style= "FONT-SIZE:18PX;" >int [] num = new int[1,2,3,4,5]; String [] st = new String[num.length]; <span style= "White-space:pre" ></span> for (int i=0; i<num.length;i++) { <span style= " White-space:pre "></span>st[i] = num[i]. ToString ();} </span>
convert string array to int array
<span style= "FONT-SIZE:18PX;" >string [] st= new string["1", "2", "3", "4", "5"];int[] num = new Int[st.length]; <span></span> for (int i=0; i<st.length;i++) { <span></span>num[i] = Integer.parseint (St[i])}</span>
int arrays and string arrays convert each other and function to each other