2016-08-13 73 views
1

当我学习Robert Sedgewick编写的Algorithms Book时,当我完成Selection.java代码时,我发现没有输出,我几乎不知道为什么。在Eclipse中没有输出

以下是我的代码。

import java.util.Comparator; 

import edu.princeton.cs.algs4.StdIn; 
import edu.princeton.cs.algs4.StdOut; 

public class Selection { 
    public static void sort(Comparable[] a) { 
     int n = a.length; 
     for (int i = 0; i < n; i++) { 
      int min = i; 
      for (int j = i + 1; j < n; j++) 
       if (less(a[j], a[min])) 
        min = j; 
      exch(a, i, min); 
     } 
    } 

    private static boolean less(Comparable v, Comparable w) { 
     return v.compareTo(w) < 0; 
    } 

    private static boolean less(Comparator comparator, Object v, Object w) { 
     return comparator.compare(v, w) < 0; 
    } 

    private static void exch(Object[] a, int i, int j) { 
     Object swap = a[i]; 
     a[i] = a[j]; 
     a[j] = swap; 
    } 

    /*************************************************************************** 
    * Check if array is sorted - useful for debugging. 
    ***************************************************************************/ 

    // is the array a[] sorted? 
    private static boolean isSorted(Comparable[] a) { 
     return isSorted(a, 0, a.length - 1); 
    } 

    // is the array sorted from a[lo] to a[hi] 
    private static boolean isSorted(Comparable[] a, int lo, int hi) { 
     for (int i = lo + 1; i <= hi; i++) 
      if (less(a[i], a[i - 1])) 
       return false; 
     return true; 
    } 

    // is the array a[] sorted? 
    private static boolean isSorted(Object[] a, Comparator comparator) { 
     return isSorted(a, comparator, 0, a.length - 1); 
    } 

    // is the array sorted from a[lo] to a[hi] 
    private static boolean isSorted(Object[] a, Comparator comparator, int lo, 
      int hi) { 
     for (int i = lo + 1; i <= hi; i++) 
      if (less(comparator, a[i], a[i - 1])) 
       return false; 
     return true; 
    } 

    // print array to standard output 
    private static void show(Comparable[] a) { 
     for (int i = 0; i < a.length; i++) { 
      StdOut.println(a[i]); 
     } 
    } 

    public static void main(String[] args) { 
     String[] a = StdIn.readAllStrings(); 
     // Selection.sort(a); 
     show(a); 
    } 
} 

然后是我的两个测试

1. http://algs4.cs.princeton.edu/21elementary/tiny.txt

S 0 - [R T E X A量m P L E

2. http://algs4.cs.princeton.edu/21elementary/words3.txt

bed bug dad yes zoo now for tip ilk dim tag jot sob nob sky hut men egg few jay owl joy rap gig wee was wad fee tap tar dug jam all bad yet

我期望输出中的顺序增加,但没有输出。 我设置运行配置 - Commman选项卡 - 输入文件为〜/ tiny.txt 但是,当我申请并运行,没有输出(我使用Eclipse火星4)

然后,我猜可能参数或键入Comparable,因为它有很多警告,但我无法处理它。 任何人都可以告诉我如何解决问题:)

+0

我想'edu.princeton。 cs.algs4.StdOut'是贵机构提供的课程。不知道它做了什么(并且不),很难帮助你。抱歉。 –

+0

我发现测试文件中没有EOF,所以我使用Ctrl + D来终止输入,然后得到输出 – FrancisGeek

回答

1

edu.princeton.cs.algs4.StdOut内部使用PrintWriter初始化为out = new PrintWriter(new OutputStreamWriter(System.out, CHARSET_NAME), true);println方法只写入流,但不刷新。你既可以做:

  • StdOut.print()在你的写作到底是哪实习生要求out.flush()
  • StdOut.print(Object o)方法,它总是调用flush()

参考:StdOut.java