標籤:
1.字串的長度
String str = new String(" abcd");
int length = str.length();
2.數組的長度
2.1對於 a[][] a.length代表a的行數 a[i].length代表a的列數2.2對於a[] a.length代表a的長度
3.字串與字元數組的轉化
String str = new String("abcd");
char [] a = str.toCharArray();
4.字串數字與數位轉化
4.1 String—>int
String str = "1234";
int i = Integer.parseInt(str); //可能會拋出異常 ,(如果報錯)在 main(String[] args)後面加上throws Exception
4.2 int —>String
int i=235;
String s = String.valueOf(i); //Of大寫
5.從.in檔案匯入資料
import java.IO.*;
public static void main(String [] args) throws IOException{ //會拋出異常
FileReader a = new FileReader("D-small-attempt1.in");//檔案與源碼在同一目錄下
BufferedReader read = new BufferedReader(a);
String textLine="";
String str="";
while(( textLine=read.readLine())!=null){
str+=textLine+" ";
}
String[] numbersArray=str.split(" "); //將str按空格分開,儲存成字元數組
}
6.導處資料到.txt檔案
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(); //下一行
bufw.flush(); //更新
}
bufw.close();
7.保留有限位小數
import java.text.*
DecimalFormat sim = new DecimalFormat("0.000000");//保留多少位小數點後面就有幾位
double six = sim.format(source); //將source保留六位小數
比較簡單的方法(對於輸出結果保留有限位小數)
System.out.printf("%.3f",a);
ACM競賽 Java編程總結