【问题标题】:Java quick sort, reading from a user input file into any array (to be sorted)Java 快速排序,从用户输入文件中读取到任意数组(待排序)
【发布时间】:2009-11-05 22:48:51
【问题描述】:

你们怎么了,

我正在尝试用 Java 编写一些代码,这些代码将从文件中读取数字(.txt 文件的每一行都有一个 #)将它们放入数组中,然后对数组运行快速排序。 Eclipse 显示了一些我遇到问题的红色。我的错误是用cmets标记的,是什么错误,如果有人能帮我把这个跑起来,谢谢大家!

-凯尔

好的,我更新了前两个答案,到目前为止,谢谢,但还有两个错误我不太理解。

import java.io.*;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.File;


public class Lab3 {

public static void main(String[] args) throws IOException{


    System.out.print("Name of file with array: ");
    Scanner readIn = new Scanner(System.in);
    String input=readIn.nextLine();}
**testScan1(input);** //Return Type for method is missing (but I am trying to call the method here)


public static void testScan1(String filename)

{
    File file = new File(filename);
    Scanner scan;
    int [] array = new int[5];
    try{


        scan = new Scanner( file );
    }
    catch ( java.io.FileNotFoundException e )
    {
        System.out.println( "couldn't open. file not found "  );
        return;
    }
    while(scan.hasNext())
    {
        for( int i = 0; i <= file.length(); ++i)
        {

            **array[i]=scan.next();** /*Type mismatch, cannot convert from sting to int. (I moved the declaration about try?)*/




        }

        int partition(int arr[], int left, int right)
        {
            int i=left; int j = right;
            int tmp;
            int pivot = arr[(left+right)/2];
            while (i<=j){
                while(arr[i]<pivot)
                    i++;
                while (arr[j]>pivot)
                    j--;
                if (i<=j){
                    tmp=arr[i];
                    arr[i]=arr[j];
                    arr[j]=tmp;
                    i++; j--;
                }
            }
            return i;
        }
        void quickSort(int arr[], int left, int right){
            int index = partition(arr, left, right);
            if (left<index-1);
            quickSort(arr, left, index-1);
            if (index<right)
                quickSort(arr, index, right);
        }
    }

【问题讨论】:

  • 我只是在回答这个问题,因为除了提问者之外,其他任何人都不会普遍感兴趣。没有冒犯的意思。祝你在课堂上好运。

标签: java arrays file quicksort


【解决方案1】:

每当您处理递归算法并遇到堆栈溢出时,这是因为您的算法没有明确定义的边缘情况会导致您的递归终止。 (或者你的输入太大了,但这种情况很少发生,这里也不是这样。)

你应该看看你的quickSort() 方法,看看是什么让它无限地调用自己。想象一下用两个镜子观察一个反射,反射从另一个反射反射回来,然后它进入无限远......这就是这里发生的事情。

此外,在 Java 语言中,建议您的类名始终以大写字母开头。我会把你的班级命名为QuickSortHomework 或类似的名字。

此外,您可能想了解if 语句在Java 中的工作原理,以及“块”是如何定义的。您在分号附近有一个 if 语句和一对大括号,它可能没有按照您的想法执行。

【讨论】:

    【解决方案2】:

    老实说,我对这里所有的转发和转发感到有点恼火。

    这不是您想听到的,但我觉得您将此网站用作拐杖。您似乎没有花时间自己弄清楚发生了什么。这个令人费解的过程,无论多么痛苦,都是真正学习的来源。

    在这种情况下,如果您查看该错误的含义,然后您只是查看了您的 quickSort() 实现,我认为您必须注意到它有一些非常明显的错误。

    编辑:如果你在想“但我确实试图把它弄明白”......在你的帖子中加入真的很有帮助,“我以为可能是这样,但那没有不行,所以我想也许它可能是……”等等。有一半的时间你会突然意识到这个问题,而你正试图像这样谈论它。另一半,至少我们看到你在努力。

    【讨论】:

    • 其实我也觉得这个问题似曾相识,但在这之后我又回过头去看了Benzle之前的问题——上次我给出了这个答案:stackoverflow.com/questions/1597831/…。值得注意的是,我写道:“我怀疑你根本不明白自己要做什么。这不能在这里解决——你需要花更多的时间学习,直到你真正明白为止。”
    • 按照最新到最旧的顺序排列他的问题,甚至更糟。在过去的几个小时里,他已经问了 3 次这个问题。
    • 是的,但是你还是帮了他。你真是太酷了。你可能对他的作业考虑得比他多(严厉,抱歉)。
    • @Kevin:好吧,我试着做个酷人。但事实证明,直到 我发布了我的答案之后,我才将两个和两个放在一起。 (你的回答比我晚一分钟。)所以是的,我是个好人,但如果我意识到是谁在问这个问题,我可能不会那么好。
    【解决方案3】:

    一些错误:

    • public testScan1(String filename) 实际上没有任何返回类型,它也是从静态上下文中调用的,但它不是静态的。应该改为public static void testScan1(String filename)
    • file.hasNext() 的用途是什么?当然它不存在,因为它没有任何意义。我想你的意思是scan.hasNext()
    • 找不到array,因为它是在try/catch 块内定义的,因此它仅存在于该范围内。在尝试之前移动定义。

    另外尝试以更易读的方式缩进代码,因为很难发现错误。例如,为什么在调用 testScan 之前有一个括号 } 超出了我想你想调用它的 main 方法?

    【讨论】:

    • 太好了,谢谢 Jack,“array[i]=scan.next() 仍然很麻烦,即使在移动有关 try 块的声明之后。
    • 现在错误不同了,Scanner 类通过指定要从流中读取的内容来工作。如果你使用普通的 scan.next() 返回类型是字符串,所以你应该把它转换成你想要的数字类型。在您的情况下,您可以使用 array[i] = Integer.parseInt(scan.next())array[i] = scan.nextInt() 来解决它。它们是同一回事。
    • 好的,谢谢 Jack,我已经做了很多修改并准备重新发布,我认为问题已经发生了足够的变化,以至于它是新的。
    【解决方案4】:

    正如我在上一个问题中告诉您的那样,无法找到数组,因为它仍在尝试块中。

    那么对于打印,您不能以有用的方式直接打印数组,您应该遍历每个元素并按以下方式打印:

    for (int i = 0; i < array.length; ++i)
         System.out.println(array[i]+" ");
    

    你在这里:

    import java.io.*;
    import java.io.File;
    import java.util.Scanner;
    
    public class sdfs
    {
        public static void main(String[] args) throws IOException
        {
            System.out.print("Name of file with array: ");
            Scanner readIn = new Scanner(System.in);
            String input = readIn.nextLine();   
        }
    
        public static void testScan1(String filename)
        {
            File file = new File(filename);
            Scanner scan;
            int[] array;
    
            try
            {
                array = new int[5];
                scan = new Scanner(file);
            }
            catch (java.io.FileNotFoundException e)
            {
                System.out.println("couldn't open. file not found ");
                return;
            }
            while (scan.hasNext())
            {
                    for (int i = 0; i <= file.length(); ++i)
                    {
                        array[i] = Integer.parseInt(scan.next()); 
    
                        for (int j = 0; j < array.length; ++j)     
                            System.out.println(array[i]+" ");
                    }
            }
        }
    
        int partition(int[] arr, int left, int right)
        {
            int i = left;
            int j = right;
            int tmp;
            int pivot = arr[(left + right) / 2];
            while (i <= j) {
                    while (arr[i] < pivot)
                            i++;
                    while (arr[j] > pivot)
                            j--;
                    if (i <= j) {
                            tmp = arr[i];
                            arr[i] = arr[j];
                            arr[j] = tmp;
                            i++;
                            j--;
                    }
            }
            return i;
        }
    
        void quickSort(int[] arr, int left, int right)
        {
            int index = partition(arr, left, right);
            if (left < (index - 1)) {
                    ;
            }
            quickSort(arr, left, index - 1);
            if (index < right) {
                    quickSort(arr, index, right);
            }
        }
    }
    

    看看缩进如何帮助阅读代码..

    【讨论】:

    • 谢谢先生!我的括号还在某处,你能帮我放置吗?:(前 3 个和最后一个的错误;在 sn-p 中。}}} array[i] = Integer.parseInt(scan.next()) ; for (int i = 0; i
    【解决方案5】:

    您的问题是 testScan1 需要返回类型,即使该类型为 void。

    【讨论】:

      【解决方案6】:

      IIRC,我认为您不能像在 Python 或 Scala 中那样打印数组并查看所有值。您必须遍历数组以打印值:

      for (int i = 0; i < array.length; i++) {
         System.out.println(array[i]);
      }
      

      【讨论】:

        【解决方案7】:

        你有两个问题。您需要在 try 块之外定义数组。像这样:

         int[] array = new int[5];
         Scanner scan;
         try {
            scan = new Scanner(file);
         } catch (java.io.FileNotFoundException e) {
              //etc.
        

        即使这只是因为您在异常块中返回才真正起作用,否则编译器会抱怨可能引发了异常并且从未分配过scan

        要打印,请使用System.out.println(java.util.Arrays.toString(array));

        这将使它成为可读的格式。如果您只打印数组的 toString() 值(如果您只是将它传递给 println方法)。

        【讨论】:

          猜你喜欢
          • 2015-10-18
          • 2020-12-20
          • 1970-01-01
          • 1970-01-01
          • 2023-04-06
          • 2013-08-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多