【发布时间】:2020-08-08 18:06:17
【问题描述】:
每当我引用长数组“a”时(即第 21、22、28、36、38、46、49、60 行),我都会不断收到此错误。我是 Java 初学者,真的不明白错误信息。即使对数组进行类型转换也不起作用。
错误:表达式的类型必须是数组类型但解析为long
import java.util.*;
public class MyLongArray
{
private long a;
private int nElems;
public MyLongArray(int size)
{
Scanner sc= new Scanner(System.in);
long[] a = new long[size];
for(int i=0; i < nElems; i++)
a[i] = sc.nextLong();
nElems = a.length;
}
public int find(long searchKey) {
for(int i=0; i < nElems; i++)
if(searchKey == a[i])
return a[i];
}
public void insert(long value) {
}
public long getElem(int index) {
return a[index];
}
public boolean delete(long value) {
long[] temp = new long[nElems];
int f = 0;
int o = 0;
for(int i=0; i < nElems; i++)
{
if(value != a[i])
{
temp[o++] = a[i];
}
else
f = 1;
}
for(int j=0; j < nElems; j++)
a[j] = temp[j];
for(int i=0; i < nElems; i++)
System.out.print(a[i] + " ");
if (f==1)
return true;
else
return false;
}
public void display()
{
for(int i =0; i < nElems; i++)
System.out.print(a[i] + " ");
}
}
【问题讨论】:
-
请edit您的问题指出是哪一行代码产生了错误。
-
您使用
a作为长值和长数组值。下定决心要成为什么样的人 -
您将 a 定义为 long,而不是 long[]。
-
您已经定义了两次
a。使用正确的、描述性的变量名称以避免此类混淆。
标签: java arrays long-integer