作完这一步,把你的程序签入源代码控制(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