參考 Sort的實現。提交博客連接。java
import java.util.*; public class MySort { public static void main(String [] args) { String [] toSort = {"aaa:10:1:1", "ccc:30:3:4", "bbb:50:4:5", "ddd:20:5:3", "eee:40:2:20"}; System.out.println("Before sort:"); for (String str: toSort) { System.out.println(str); } int [] a = new int[toSort.length]; for (int i = 0; i < toSort.length; i++){ String [] tmp = toSort[i].split(":"); a[i] = Integer.parseInt(tmp[1]); } Arrays.sort(a); System.out.println("After sort:"); for (int i = 0; i < a.length; i++) { for (int j = 0; j < toSort.length; j++) { if (a[i] == Integer.parseInt((toSort[j].split(":"))[1])) { System.out.println(toSort[j]); } } } } }
課上沒法完成的課下完成須要答辯linux
從學號.bin 中讀取內容,轉化爲十進制數輸出至學號_1.txt 中,用到了bufferedReaded 類中的readLine() 和newLine() 方法數組
import java.io.*; import java.util.Random; public class GenNumber { public static void main(String[] args) { String filepath = System.getProperty("user.dir"); filepath += "\\20175329.txt"; System.out.println(filepath); try { File file = new File(filepath); if (!file.exists()) { //若是不存在data.txt文件則建立 file.createNewFile(); System.out.println("20175329.txt建立完成"); } FileWriter fw = new FileWriter(file); //建立文件寫入 BufferedWriter bw = new BufferedWriter(fw); //產生隨機數據,寫入文件 Random random = new Random(); for (int i = 1; i < 329; i++) { int randint = (int) Math.floor((random.nextDouble() * 5329.0)); bw.write(String.valueOf(randint)); //寫入一個隨機數 bw.newLine(); //新的一行 } bw.close(); fw.close(); } catch (IOException e) { e.printStackTrace(); } } }