int arrays and string arrays convert each other and function to each other

Source: Internet
Author: User
Tags array to string

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.