Java:将数组传递给函数时出现“.class”预期错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21947455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:34:11  来源:igfitidea点击:

Java: '.class' expected error when passing array to function

javaarraysparameter-passing

提问by ShengLong916

I'm taking a java class, but have been away from it for a bit.

我正在学习 Java 课程,但已经离开它一段时间了。

Trying to get this sorting program to work:

试图让这个排序程序工作:

import java.util.*;
public class Assignment1 
{
   private int[] nums;
   private int[] ast;

   private int n;

   public void sort(int[] vals)
   {
      this.nums = vals;
      n = vals.length;
      this.ast = new int[n];
      merges(0, n - 1);
   }   

   private void merges(int bot, int top)
   {
      if (bot < top)
      {
         int mid = bot + (top - bot) / 2;
         merges(bot, mid);
         merges(mid + 1, top);
         merge(bot, mid, top);
      }
   }

   private void merge(int bot, int mid, int top)
   {
      for (int i = bot; i <= top; i++)
      {
         ast[i] = nums[i];
      }
      int i = bot;
      int j = mid + 1;
      int k = bot;
      while (i <= mid && i <= top)
      {
         if (ast[i] <= ast[j])
         {
            nums[k] = ast[i];
            i++;
         }
         else
         {
            nums[k] = ast[j];
            j++;
         }
         k++;
      }
      while (i <= mid)
      {
         nums[k] = ast[i];
         k++;
         i++;
      }
   }

   private void show()
   {
      System.out.print("Sorted Array:  ");
      for(int l=0;l<nums.length;l++)
      {
         System.out.print(nums[l] + "  ");
      }

   }
   public static void main(String[] args) 
   {
      Assignment1 a = new Assignment1();
      Scanner scan = new Scanner(System.in);
      System.out.print("\nArray Size: ");
      int s = scan.nextInt();
      int[] array = new int[s];
      for(int x=0;x<s;x++)
      {
         System.out.print("Enter Element " + (x + 1) + ":");
         array[x] = scan.nextInt();
      }


      a.sort(array[]);
      a.show();
   }
}

The error I'm getting is this:

我得到的错误是这样的:

Assignment1.java:82: error: '.class' expected
  a.sort(array[]);

I know it's something basic eluding me, but the basics are the things that are most hazy...

我知道这是我无法理解的基本知识,但基本知识是最模糊的东西......

回答by mdewitt

I believe the compiler thinks you are trying to pass in an class type into sortbecause you added the []after array in your call to sortwhich is how you declare an array type.

我相信编译器认为您正在尝试将类类型传入,sort因为您[]在调用中添加了after 数组,sort这就是您声明数组类型的方式。

Just change

只是改变

a.sort(array[]) 

to

 a.sort(array)

because you have already initialized arrayas an int[]type/array earlier in your method.

因为您已经在方法的前面初始化arrayint[]类型/数组。

You only need to add the []during the intialization. After that, the variable name for an array is referenced just like the variable name for any other variable.

您只需要[]在初始化期间添加。之后,数组的变量名就像任何其他变量的变量名一样被引用。

回答by Jason C

Your sort (int[] vals)takes an int[]parameter.

sort (int[] vals)需要一个int[]参数。

Your int[] arrayis already of that type, and you simply need to pass arrayby itself. The name of the variable is arrayand its type is int[]. When you pass the variable to a function, you refer to it by its name. There is no "array[]", that is not a valid construct.

int[] array已经是那种类型了,你只需要自己通过array。变量的名称是array,它的类型是int[]。当您将变量传递给函数时,您可以通过其名称来引用它。没有“ array[]”,这不是一个有效的结构。

Syntactically, array[]is invalid, but hypothetically if it were valid, the type I imagine it would be is int(the type of one of the elements) not int[].

在语法上,array[]是无效的,但假设它是有效的,我想象int的类型是(元素之一的类型) not int[]

The ".class expected" compiler error is misleading. The array[]syntax is invalid, and I guess the compiler's confusion leads to an unexpected error message (edit: good explanations for the error are given in mdewitt and meriton's answers).

“.class 预期”编译器错误具有误导性。该array[]语法是无效的,我想编译器的混乱导致了意外的错误消息(编辑:对于错误良好的解释在mdewitt和梅里的答案给出)。

By the way, Java does support the syntax int array[];as an equivalent of int[] array;. Personally I never liked this ambiguity and I use the former syntax where possible. If you see this syntax, do not be confused -- the name is still arrayand the type is still int[].

顺便说一句,Java 确实支持语法int array[];作为int[] array;. 就我个人而言,我从不喜欢这种歧义,我在可能的情况下使用前一种语法。如果您看到此语法,请不要混淆——名称仍然array是,类型仍然是int[]

回答by meriton

Assignment1.java:82: error: '.class' expected a.sort(array[]);

Assignment1.java:82: 错误: '.class' 预期 a.sort(array[]);

That means the error is on line 82 of Assignment1.java. That line reads:

这意味着错误在 Assignment1.java 的第 82 行。那行写着:

  a.sort(array[]);

which clearly isn't valid java syntax. You can not postfix a variable with empty square brackets. If you mean to pass the array object, simply use the variable by itself:

这显然不是有效的 java 语法。您不能使用空方括号对变量进行后缀。如果您想传递数组对象,只需单独使用变量:

  a.sort(array);

Put differently, there is no special syntax for passing an array. You can pass an array like any other object or primitive value.

换句话说,传递数组没有特殊的语法。您可以像任何其他对象或原始值一样传递数组。

Empty square brackets can only be used to denote an array type, in which case they follow the component type. If we take that interpretation, arraywould have to be a type rather than a variable, and array[]would denote a type rather than an expression. Of course, you can not pass a type in a method parameter, but you could pass the class object for that type:

空方括号只能用于表示数组类型,在这种情况下,它们跟在组件类型之后。如果我们采用这种解释,array则必须是类型而不是变量,并且array[]表示类型而不是表达式。当然,您不能在方法参数中传递类型,但可以传递该类型的类对象:

  a.sort(array[].class);

That's what the compiler thinks you're trying to do. Of course, that's nonsense, because this wouldn't be type correct, as the sort methods expect an array rather than a class object, and array doesn't denote a type to begin with. Put differently, the compiler guessed wrong what you mean to say, and therefore confused you with an unrelated error message.

这就是编译器认为您正在尝试做的事情。当然,这是无稽之谈,因为这不会是类型正确的,因为排序方法需要一个数组而不是一个类对象,而数组并不表示开始的类型。换句话说,编译器猜错了您的意思,因此将您与不相关的错误消息混淆。