我正在學習java類,但已經離開了一段時間。Java:當將數組傳遞給函數時,會出現'.class'期望的錯誤
試圖讓這個排序程序的工作:
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();
}
}
我得到的錯誤是這樣的:
Assignment1.java:82: error: '.class' expected
a.sort(array[]);
我知道這是一些基本的東西躲避我,但基本是事這是最朦朧的...
爲什麼使用'array []'?你認爲'[]'做什麼?你爲什麼這麼認爲? –
只是陣列沒有[] – nachokk
@SotiriosDelimanolis,諷刺和批判是沒有幫助的。只要讓他知道他是錯的而不是批評。 –