1. Write the code for selection sort, sort the array to an int
public class TestSelectSort { public static void main(String[] args) { int [] arr = {87,65,5,5,43,21}; System.out.print("排序前:[ "); for (int i : arr){ System.out.print(i+" "); } System.out.println("]"); selectSort(arr); System.out.print("排序后:[ "); for (int i : arr){ System.out.print(i+" "); } System.out.println("]"); } public static void selectSort(int[] arr){ for (int i = 0;i < arr.length - 1;i++){ int minIndex = i; for (int j = i;j < arr.length - 1;j++){ if (arr[minIndex] > arr[j + 1]){ minIndex = j + 1; } } if (minIndex != i){ int temp = arr[minIndex]; arr[minIndex] = arr[i]; arr[i] = temp; } } } }
operation result:
2. the contents of the text file exam.txt IO read class E package tray, each read line of text in each input line as a generic collection ArrayList contents into the collection and use of reinforcing outputs for display.
public class TestReader { public static void main (String [] args) { String path = "E: /exam.txt" ; outputMethod (path); } Private static void outputMethod (String path) { / ** * Create collection object * / List <String> List = new new the ArrayList <String> (); / ** * Create Object buffer * / the BufferedReader br = null ; the try { br = new new the BufferedReader ( new new FileReader(path)); /** * 读取数据每次读取一行 */ String line = br.readLine(); while (line != null){ list.add(line); line = br.readLine(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { if (br != null){ br.close(); } } catch (IOException e) { e.printStackTrace(); } for (String s : list){ System.out.println(s); } } } }
Text:
Result of the program:
Note: To save the text in UTF-8 format, or else the program will be reported garbled reading, as shown below (saved in ANSI text format):
3. Write two threads, one thread print integer of 1-52, another thread printing letters AZ. Print order 12A34B56C ... .5152Z. That is, after the order from small to large integers and letters printed, and printing each of two integers, a printed letter, alternating cycles print until the end of the letter Z and the integer 52.
/** * 打印类 */ public class Printer { /** * 设为1,方便计算3的倍数 */ private int index = 1; /** * 打印数字的方法,每打印两个数字,等待打印一个字母 */ public synchronized void print(int i){ while (index % 3 == 0){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.print(""+i); index++; notifyAll(); } /** * 打印字母,每打印一个字母等待打印两个数字 */ public synchronized void print(char c){ while (index % 3 != 0){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.print(""+c); index++; notifyAll(); } }
/** * 打印数字线程 */ public class NumberPrinter extends Thread { private Printer p; public NumberPrinter(Printer p) { this.p = p; } public void run() { for (int i =1;i <= 52;i++){ p.print(i); } } }
/** * 打印字母线程 */ public class LetterPrinter extends Thread { private Printer p; public LetterPrinter(Printer p){ this.p = p; } public void run(){ for (char c = 'A';c <= 'Z';c++){ p.print(c); } } }
public class TestThread { public static void main(String[] args) { /** * 创建打印机对象 */ Printer p = new Printer(); /** * 创建线程对象并启动线程 */ Thread t1 = new NumberPrinter(p); Thread t2 = new LetterPrinter(p); t1.start(); t2.start(); } }
运行结果: