合并排序。错误 - 类型不匹配:无法从double转换为String

时间:2012-07-09 09:46:40

标签: java file-io mergesort type-mismatch

下面的程序使用合并排序来排列文件中的前10,000个单词。我在他的算法导论,第二版中遵循了Thomas Cormen的伪代码。

import java.io.*;
import java.util.*;

public class SortingAnalysis {

    public static void merge(String[] A, int p, int q, int r) {
        int n1 = q-p+1;
        int n2 = r-q;
        double infinity = Double.POSITIVE_INFINITY;
        int i, j;
        String[] L = null;
        String[] R = null;
        for (i=1; i<=n1; i++) {
            L[i] = A[(int) (p+i-1)];
        }
        for (j=1; j<=n2; j++) {
            R[j] = A[(int) (q+j)];
        }
        L[n1+1] = infinity; //type mismatch: cant convert from double to string
        R[n2+1] = infinity; //same as above
        i=1;
        j=1;
        for (int k=(int) p; k<=r; k++) {
            int comparison = L[i].compareTo(R[j]);
            if (comparison<=0) {
                A[k] = L[i];
                i++;
            }
            else {
                A[k] = R[j];
                j++;
            }
        }

    }

    public static void mergeSort(String[] A, int p, int r) {
        if (p<r) {
            int q = (int) Math.floor((p+r)/2); //I typecasted q here so I can still pass the variables
            mergeSort(A, p, q);
            mergeSort(A, q+1, r);
            merge(A, p, q, r);
        }
    }

    public static void main(String[] args) {
        final int NO_OF_WORDS = 10000;
        try {
            Scanner file = new Scanner(new File(args[0]));
            String[] words = new String[NO_OF_WORDS];

            int i = 0;
            while(file.hasNext() && i < NO_OF_WORDS) {
                words[i] = file.next();
                i++;
            }
            long start = System.currentTimeMillis();
            mergeSort(words, 0, words.length-1);
            long end = System.currentTimeMillis();
            System.out.println("Sorted Words: ");
            for(int j = 0; j < words.length; j++) {
                System.out.println(words[j]);
            }   
            System.out.print("Running time of insertion sort: " + (end - start) + "ms");

        }
        catch(SecurityException securityException) {
            System.err.println("Error");
            System.exit(1);
        }
        catch(FileNotFoundException fileNotFoundException) {
            System.err.println("Error");
            System.exit(1);
        }
    }
} 

控制台中显示错误
Exception in thread "main" java.lang.Error: Unresolved compilation problems: Type mismatch: cannot convert from double to String Type mismatch: cannot convert from double to String

at SortingAnalysis.merge ... mergeSort and main </code>


我认为这是因为Math.floor方法应该是一个双重但是我把它转换为int,所以传递参数时没有问题。

另外,我认为将字符串分配给无穷大时出错。但我只是关注Cormen的伪代码。这似乎是正确的,因为我自己手动“调试”了代码。但是,当我把它放入代码时,它不起作用。哪里可以出错?伙计们,我需要你的帮助。我是Java的新手,我还处于缓冲过程中。非常感谢你!

2 个答案:

答案 0 :(得分:3)

  

我认为这是因为Math.floor方法应该是一个双重但我把它转换为int,所以在传递参数时没有问题。

不,它比那简单。您正尝试将 double 值分配到字符串数组中。你根本做不到。

我强烈怀疑您应该将整个代码更改为使用double[]而不是String[]。请注意,即使它正在编译(并且您不应该尝试运行它,直到您确实修复了所有编译错误),因此会出现问题:

String[] L = null;
String[] R = null;
for (i=1; i<=n1; i++) {
    L[i] = A[(int) (p+i-1)];
}

这显然会抛出一个NullPointerException。您没有初始化数组变量以引用数组对象。你想要这样的东西:

double[] L = new double[n1 + 1];
double[] R = new double[n1 + 1];
for (i=1; i<=n1; i++) {
    L[i] = A[(int) (p+i-1)];
}

以1为基础的方式使用数组是很奇怪的,通过...它会很多更像惯用语来做类似的事情:

double[] L = new double[n1];
double[] R = new double[n1];
for (i = 0; i < n1; i++) {
    L[i] = A[p + i];
}

感觉你正在努力,因为你在这里尝试学习两件事:

  • MergeSort如何运作
  • Java的工作原理

我将专注于首先理解Java语言 - 此时您将处于将伪代码转换为实际代码的更好位置。

答案 1 :(得分:0)

无论如何,施放到(int)都会发言。

  L[n1+1] = infinity; //type mismatch: cant convert from double to string

这是一个错误,因为您无法将double强加给String即使您使用toString()转换它也不会是正确的,因为您只会得到“NaN”这不是maixmum String值,它似乎是你的目标。

您可以在不添加虚拟“无限”值的情况下对此进行编码,我建议您这样做。

相关问题