Java: '. Class' Expected Error When Passing Array to Function [Closed]
I'm Taking a Java Class, but Have Been Away from It for a Bit. Trying to Get This Sorting Program to Work: Import Java. Util. *; Public Class Assignment1 {...
I'm taking a java class, but have been away from it for a bit.
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...
I believe the compiler thinks you are trying to pass in an class type into sort because you added the [] after array in your call to sort which is how you declare an array type.
Just change
a.sort(array[])
to
a.sort(array)
because you have already initialized array as an int[] type/array earlier in your method.
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.
Your sort (int[] vals) takes an int[] parameter.
Your int[] array is already of that type, and you simply need to pass array by itself. The name of the variable is array and 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.
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[].
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).
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 array and the type is still int[].
Assignment1.java:82: error: '.class' expected a.sort(array[]);
That means the error is on line 82 of Assignment1.java. That line reads:
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:
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, array would 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:
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.