1.選択ソート用のコード、int型へのソート配列を書きます
パブリック クラスTestSelectSort { 公共 静的 ボイドメイン(文字列[]引数){ INT [] ARR = {87,65,5,5,43,21 }。 System.out.print( "排序前:[" ); 用(INT {:ARR I) System.out.print(I +」 " )。 } のSystem.out.println( "]" )。 selectSort(ARR)。 System.out.print( "排序后:[" ); 用(INT {:ARR I) System.out.print(I +」 " )。 } のSystem.out。); } パブリック 静的 ボイド selectSort(INT [] ARR){ ため(INT I = 0、I <arr.length - 1; I ++ ){ int型 minIndex = I。 用(int型 J = I; J <arr.length - 1; J ++ ){ 場合(ARR [minIndex]> ARR [J + 1 ]){ minIndex = J + 1 。 } } もし(!minIndex = I){ int型の TEMP = ARR [minIndex]。 ARR [minIndex] = ARR [I]。 ARR [I]= 温度; } } } }
結果:
2. IO exam.txtテキストファイルの内容は、補強の収集および使用に汎用コレクションArrayListの内容として、各入力ラインのクラスEパッケージトレイ、テキストの各リード線を読み取ります表示のための出力。
パブリック クラスTestReader { 公共 静的 ボイドメイン(文字列[]引数){ 文字列のパス = "E:/exam.txt" ; outputMethod(パス); } プライベート 静的 ボイドoutputMethod(文字列のパス){ / ** *コレクションオブジェクトを作成 * / リスト <文字列>一覧= 新しい新しいのArrayList <文字列> (); / ** *オブジェクトのバッファを作成します * / BufferedReaderのBR = ヌル; 試み{ BR = 新しい新しいをBufferedReader(新新FileReaderの(パス))。 / ** *读取数据每次读取一行 * / 文字列の行 = br.readLine(); 一方、(!ライン= NULL ){ list.add(ライン) 行 = br.readLine(); } } キャッチ(にFileNotFoundException電子){ e.printStackTrace(); } キャッチ(IOExceptionを電子){ e.printStackTrace(); } 最後に{ 試み{ 場合(BR!= NULL ){ br.close()。 } } キャッチ(IOExceptionを電子){ e.printStackTrace(); } のために(文字列S:リスト){ System.out.printlnは(S); } } } }
テキスト:
プログラムの結果:
注:(ANSIテキスト形式で保存された)は、以下に示すように、UTF-8形式のテキストを保存するには、または他のプログラムが文字化け読み取りを報告されます。
3.ライト二つのスレッド、1-52のいずれかのスレッドプリント整数、文字AZを印刷する別のスレッド。注文12A34B56Cを印刷... .5152Zを。すなわち、小から大整数と印刷文字と、2つの整数、印刷された文字の各々を印刷するための後、交互のサイクルは文字Zの端部と整数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(); } }
运行结果: