【问题标题】:Why is my writefile() not compiling?为什么我的 writefile() 没有编译?
【发布时间】:2016-02-18 07:46:24
【问题描述】:

writefile() 方法应该将给定数组中的所有整数写入给定的输出文件,每行一个。对于这部分,merge 方法应该返回一个足够大的新数组来保存前两个数组(a 和 b)的内容,然后将前两个复制到该数组中,不考虑顺序。

这是运行程序时在命令行中输入的内容:

java Merge1 sorted1.txt sorted2.txt sortedout.txt

这是 sorted1.txt 中的内容。

12 51 80 138 212 237 306 316 317 337 356 413 422 511 534 577 621 708 717 738 738 846 850 900

这就是sorted2.txt:

33 41 77 101 157 164 192 235 412 415 484 499 500 533 565 630 667 786 846 851 911 949 968 986

我该怎么做?

到目前为止,这是我的代码:

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

public class Merge1
{

public static void main(String[] args)
{

    File sorted1 = new File (args[0]);
    File sorted2 = new File (args[1]);
    File sortedout = new File (args[2]);
    try{
    Scanner input = new Scanner(sorted1);
    readfile(input);
    }
    catch (FileNotFoundException e) {
        System.out.println("File not found");   
    }
    try{
    Scanner input = new Scanner(sorted2);
    readfile(input);
    }
    catch (FileNotFoundException e) {
        System.out.println("File not found");   
    }
    try{
    Scanner input = new Scanner(sortedout);
    readfile(input);
    }
    catch (FileNotFoundException e) {
        System.out.println("File not found");   
    }




} // end main

static int[] readfile(Scanner input)
{

    String num = "";
    while(input.hasNextInt())
    {
        num += input.nextInt() + " ";
    }
    String[] array = num.split(" ");
    int[] list = new int[array.length];
    for(int i = 0; i < array.length; i++)
    {
        list[i] = Integer.parseInt(array[i]);
        System.out.println(list[i]);
    }
    return list;

} // end readfile

static void writefile(PrintStream output, int[] a)
{   
    output.println(merge(int[] a, int[]b)); 

} // end writefile 

static int[] merge(int[] a, int[] b)
{
        int[] answer = new int[a.length + b.length];

    int i = 0;
    int j = 0;
    int k = 0;

    while (i < a.length && j < b.length)
        {
            if (a[i] < b[j])
            {       
                    answer[k] = a[i];
            k++;
            i++;
                }

            else  
            {      
                    answer[k] = b[j]; 
            k++;
            j++;
            }              
        }

        while (i < a.length)  
        {
            answer[k] = a[i];
        k++;
        i++;
    }

    while (j < b.length)
    {    
            answer[k] = b[j];
        k++;
        j++;
        }   

    return answer;

} // end merge  


} // end Merge1

【问题讨论】:

  • 哪个部分不能编译?用什么信息?
  • 它不喜欢行 output.prinln(merge(int[] a, int [] b);
  • 因为你使用 new 关键字创建数组,如果你想传递一个已经存在的数组,你可以只使用它的名字 a 或 b。

标签: java arrays compiler-errors command-line-arguments printstream


【解决方案1】:

总是有System.arraycopy 方法可以将一个数组复制到另一个数组中,但您也可以使用动态数组,例如ArrayListSystem.arraycopy description

至于 write 方法,所需要的只是您传递的PrintStream,以FileOutputStream 和简单的println 为每个值打开。排序方面有很多排序算法,有些集合已经实现了排序方法。

【讨论】:

  • 如何打开 FileOutputStream 并打印数组?
  • @anderkat 无论您在何处创建 PrintStream(可能在您的 main 中),您都可以使用 PrintStream stream = new PrintStream (new FileOutputStream ("filename.txt")),然后将其传递给您的 write 方法。但是你应该试着用谷歌搜索一下,我为System.arraycopy 提供的链接非常容易找到。 google和stackoverflow上有很多官方文档和例子,你不能错过。
  • @anderkat97 不幸的是,由于我的声誉,我无法回复您的问题评论,但关于编译器因为您的合并方法而抱怨您的 write 方法,println 接受单个值,而不是数组,您需要将数组存储在本地临时变量中并遍历每个值并单独打印它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 2022-11-27
  • 1970-01-01
  • 2019-04-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多