ACM competition Java programming summary, acm competition java programming

Source: Internet
Author: User

ACM competition Java programming summary, acm competition java programming

1. String Length

String str = new String ("abcd ");

Int length = str. length ();

2. Length of the array

2.1 For a [] [] a. length represents the number of rows of a [I]. length represents the number of columns of a. 2.2 for a [] a. length represents the length of.

3. Conversion of strings and character Arrays

String str = new String ("abcd ");

Char [] a = str. toCharArray ();

4. Conversion of string numbers and numbers

4.1 String-> int

String str = "1234 ";

Int I = Integer. parseInt (str); // an Exception may be thrown. If an error is reported, throws Exception is added after main (String [] args ).

4.2 int-> String

Int I = 235;

String s = String. valueOf (I); // Of uppercase

5. import data from the. in File

Import java. IO .*;

Public static void main (String [] args) throws IOException {// an exception is thrown.

FileReader a = new FileReader ("D-small-attempt1.in"); // The file is in the same directory as the source code
BufferedReader read = new BufferedReader ();
String textLine = "";
String str = "";
While (textLine = read. readLine ())! = Null ){
Str + = textLine + "";
}
String [] numbersArray = str. split (""); // separate str by space and save it as a character array

}

6.import data to the. txt file

Import java. IO .*;

FileWriter fw = new FileWriter ("output.txt ");
BufferedWriter bufw = new BufferedWriter (fw );
String line = null;
For (int I = 1; I <n + 1; I ++ ){
Bufw. write ("Case #" + I + ":" + result [I]);
Bufw. newLine (); // The next row
Bufw. flush (); // update
}
Bufw. close ();

7. Reserved limit Decimals

Import java. text .*

DecimalFormat sim = new DecimalFormat ("0.000000"); // The number of digits that are retained after the decimal point.
Double six = sim. format (source); // keep the source six decimal places

Relatively simple method (there is a limit number for the output result)

System. out. printf ("%. 3f", );

Related Article

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.