【问题标题】:I keep getting one type of error persistently: "The type of the expression must be an array type but it resolved to long"我不断收到一种类型的错误:“表达式的类型必须是数组类型,但它解析为长”
【发布时间】: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


【解决方案1】:
  • a 的声明修正为
       private long[] a;
    

另外:

  • 需要将find的返回类型改为long并添加默认的返回语句,例如:

    public long find(long searchKey) {
    
        for (int i = 0; i < nElems; i++)
            if (searchKey == a[i])
                return a[i];
        return -1;
    }
    
  • 关闭Scanner资源sc.close();

【讨论】:

  • 然而构造函数中的long[] a = new long[size];应该是a = new long[size];
【解决方案2】:

行号不显示,但我已经通读了几次代码并发现了一些错误。 首先,私有变量 a 应该被声明为一个长数组,而不仅仅是一个长数组。原因是因为稍后您将 a 初始化为数组,但您之前将其定义为 long。

        private long a[];

find() 函数的返回类型应该是 long 而不是 int。

public long find(long searchKey) {
        
        for(int i=0; i < nElems; i++)
            if(searchKey == a[i])
                return a[i];
}

此外,如果您在以下行中遇到某种逻辑错误,我不会感到惊讶。

temp[o++] = a[i];

最好像这样分两步递增

temp[o]  = a[1];
o++;

原因是你跳过了索引0,直接到了索引1;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-16
    • 2021-07-24
    • 2019-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多