ACM Contest Java Programming Summary

Source: Internet
Author: User

1. Length of the string

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 a[i].length represents a column count of 2.2 for a[] A.length represents the length of a

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 appended to main (string[] args) with throws Exception

4.2 int->string

int i=235;

String s = string.valueof (i); of uppercase

5. Importing data from an. in file

Import java. io.*;

public static void Main (String [] args) throws IOException{//throws an exception

FileReader a = new FileReader ("d-small-attempt1.in");//file and source in the same directory
BufferedReader read = new BufferedReader (a);
String textline= "";
String str= "";
while ((Textline=read.readline ())!=null) {
Str+=textline+ "";
}
String[] numbersarray=str.split(""); Separates Str into a character array by a space

}

6. Guide data to a. 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 (); Next line
Bufw.flush (); Update
}
Bufw.close ();

7. Reserved limited decimal places

Import java.text.*

DecimalFormat sim = new DecimalFormat ("0.000000");//reserved How many digits after the decimal point there are several
Double six = Sim.format (source); Keep Source Six decimal places

A simpler approach (preserving a limited number of decimal places for output results)

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

ACM Contest Java Programming Summary

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.