sorttestint.java

来自「Java经典例程 从外国一大学计算机教授出版物下载的代码 经典」· Java 代码 · 共 68 行

JAVA
68
字号
import java.io.*;
import javagently.*;
import myutilities.*;

class SortTestInt {

    /* The SortTesInt program    by J M Bishop Dec 1996
     *                           Java 1.1 October 1997
     *                           revised August 2000
     * for sorting a table of integers
     * Illustrates linking up to an independent sorter.
     * as well as putting integers in envelopes.
     */

    public static void main(String[] args) throws IOException {
      new SortTestInt();
    }

    SortTestInt () throws IOException {
        OurInteger[] t = new OurInteger[10];
        Stream in = new Stream(System.in);

        for (int i = 0; i < 10; i++)
            t[i] = new OurInteger(in.readInt());

        System.out.println("Original");
        System.out.println("========");
        for (int i = 0; i < t.length; i++) {
            System.out.print(t[i].val+"  ");
        }
        System.out.println();

        Sort.selectionSort(t,t.length);

        System.out.println();
        System.out.println("Sorted");
        System.out.println("======");
        for (int i = 0; i < t.length; i++) {
            System.out.print(t[i] +"  ");
        }
        System.out.println();
    }

  class OurInteger implements Sortable {

    /* This member class encapsulates an integer
     * as an object and implements Sortable
     * for integers.
     */

    int val;

    public OurInteger(int i) {
      val = i;
    }

    public boolean lessThan(Sortable a) {
      OurInteger i = (OurInteger)a;
      return val < i.val;
    }

    public String toString () {
      return String.valueOf(val);
    }
  }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?