数组和方法的问题

时间:2014-04-21 00:52:01

标签: java arrays netbeans

所以我试图用这个程序做两个数组,然后将它们合并到第三个数组中。 main方法从指定文件中获取数据,然后为每一行创建数组,并且只有两行。文件的格式如下:

11 -5 -4 -3 -2 -1 6 7 8 9 10 11
8 -33 -22 -11 44 55 66 77 88

创建数组后,我尝试调用我创建的合并方法,但是我从Netbeans那里得到了这个错误:

'的.class'预期

意外类型

必需:值

发现:上课

并且我不知道这意味着什么,Netbeans尝试通过创建其他类来修复它。 我的所有代码都在下面:

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

public class ArrayMerge
{

/**
 * @param args the command line arguments
 */
public static void main(String[] args)
{
   File inputDataFile = new File(args[0]);
   try
   {
       Scanner inputFile = new Scanner(inputDataFile);   
       Scanner keyboardin = new Scanner(System.in);
       int aSize = inputFile.nextInt();
       int a[] = new int[aSize];
       for (int i= 0 ; i<aSize; i++ )
           {
               a[i] = inputFile.nextInt();
           }
       int bSize = inputFile.nextInt();
       int b[] = new int[bSize];
       for (int j = 0; j < bSize; j++)
           {
               b[j] = inputFile.nextInt();
           }
       int c[] = ArrayMerge.merge(a, b);
       System.out.println(Arrays.toString(c));




   }
   catch(FileNotFoundException e)
   {
       System.err.println("FileNotFoundException: " + e.getMessage());
   }



}

public static int[] merge(int[] a, int[] b) 
{
            // merge 2 sorted arrays

            int[] c = new int[a.length + b.length];
            int i=0, j=0, k=0;

            while (i<a.length && j<b.length) {
                    c[k++] = (a[i]<b[j]? a[i++]: b[j++]);
            } // end while

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

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

            return c;
    } // end merge()

}

更新:我解决了自己的问题。我忘了数组不是用它们的名字和括号调用的,而只是变量名。意识到这一点后,我发现我必须将有错误的行移到Try {}块中。我已更新上述代码以反映这些更改。

1 个答案:

答案 0 :(得分:1)

您的merge()方法声明类之外。

将类的右大括号从上方 merge()方法移动到下面的