作完這一步,把你的程序簽入源代碼控制(git push)。html
簽入全部代碼。java
在(2)獲得的程序中繼續擴展,用同一個程序文件,寫不一樣的函數來實現這個功能。 仍然用 nZhangSan(你的名字)來表示元素的總數。node
簽入全部代碼。git
System.out.println("請鍵入所須要的數字: (須要用空格隔開)"); Scanner scanner = new Scanner(System.in);//把20 18 23 21形式的數字串加入棧 String shuru = scanner.nextLine(); StringTokenizer str = new StringTokenizer(shuru); for (int a = 0; a < shuru.length(); a++) { while (str.hasMoreTokens()) { String fanxu = str.nextToken(); Number num = new Number(Integer.parseInt(fanxu)); shiyanyi1.add(num); } } System.out.println("棧中全部元素:" + shiyanyi1.toString()); System.out.println("棧中元素數量:" + shiyanyi1.size());`
public void addFirstNumList(Number data) { NumList node = new NumList(data); node.next = list; list = node; nWuhengyi++; }
通俗點說就是每次找到後面元素的最小值而後與之交換。如圖:
數組
結果:
服務器
protected String[] Array; public shiyanyi2(String string) { Array = string.split(" "); nwuhengyi = 0; }
Scanner scanner = new Scanner(System.in); System.out.println("輸入空格隔開的整數"); String string = scanner.nextLine(); shiyanyi2 array = new shiyanyi2(string);
而後數組的插入、刪除就很容易了。數據結構
//--------------選擇排序法 class Select{ public void sort(int arr[]){ //中間值 int temp = 0; //外循環:我認爲最小的數,從0~長度-1 for(int j = 0; j<arr.length-1;j++){ //最小值:假設第一個數就是最小的 int min = arr[j]; //記錄最小數的下標的 int minIndex=j; //內循環:拿我認爲的最小的數和後面的數一個個進行比較 for(int k=j+1;k<arr.length;k++){ //找到最小值 if (min>arr[k]) { //修改最小 min=arr[k]; minIndex=k; } } //當退出內層循環就找到此次的最小值 //交換位置 temp = arr[j]; arr[j]=arr[minIndex]; arr[minIndex]=temp; System.out.print("第" + (j+1)+"次排序後的結果是:"); for (int h = 0 ; h<arr.length;h++) { System.out.print(arr[h] + " "); } System.out.println(" "); } //輸出結果 System.out.print("最後排序結果:"); for(int i = 0;i<arr.length;i++){ System.out.print(arr[i]+" "); } }
import java.io.*; public class aaa { public static void main(String[] args) throws IOException { File file = new File("C:\\Users\\Administrator", "aaa.txt"); // //使用try-catch處理異常 // try { if (!file.exists()) { file.createNewFile(); } } catch (IOException Exception) { System.out.println("錯誤,指定路徑不存在"); } InputStream inputStream1 = new FileInputStream(file); while (inputStream1.available() > 0) { System.out.print((char) inputStream1.read() + " "); } inputStream1.close(); System.out.println(" "); byte[] buffer = new byte[1024]; String content = ""; int flag = 0; InputStream inputStream2 = new FileInputStream(file); BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream2); while ((flag = bufferedInputStream.read(buffer)) != -1) { content += new String(buffer, 0, flag); } System.out.println(content); bufferedInputStream.close(); String [] a =new String[10]; a = content.split(" "); String B = a[0]; String C = a[1]; int i = Integer.valueOf(B).intValue(); int ii = Integer.valueOf(C).intValue(); System.out.println(i); System.out.println(ii); } }
File file = new File("C:\\Users\\Administrator", "aaa.txt"); // //使用try-catch處理異常 // try { if (!file.exists()) { file.createNewFile(); } } catch (IOException Exception) { System.out.println("錯誤,指定路徑不存在"); } InputStream inputStream1 = new FileInputStream(file); while (inputStream1.available() > 0) { System.out.print((char) inputStream1.read() + " "); } inputStream1.close();
這部分,而後直接提取,獲得的數字一直是32和49,百思不得其解。函數
byte[] buffer = new byte[1024]; String content = ""; int flag = 0; InputStream inputStream2 = new FileInputStream(file); BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream2); while ((flag = bufferedInputStream.read(buffer)) != -1) { content += new String(buffer, 0, flag); } System.out.println(content); bufferedInputStream.close(); String [] a =new String[10]; a = content.split(" "); String B = a[0]; String C = a[1]; int i = Integer.valueOf(B).intValue(); int ii = Integer.valueOf(C).intValue(); System.out.println(i); System.out.println(ii);
問題2:在排序輸出每一輪次的元素時,會出現這樣的狀況
測試
解決:是選擇排序法用的int類型的數組,可是以前獲得的是string類型的,因此致使元素的錯亂。.net