0

I keep getting this error whenever I refer to the long array 'a' (i.e on line 21, 22, 28, 36, 38, 46, 49, 60). I am a beginner in Java and really don't understand the error message. Even typecasting the array does not work.

Error: The type of the expression must be an array type but it resolved to 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] + " ");
        }   
    }
Mr Duda
  • 1
  • 1

2 Answers2

0
  • Fix your declaration of a to
       private long[] a;
    

ADDITIONALLY:

  • You need to change the return type of find to long and add a default return statement, example:

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

Amit
  • 36
  • 4
0

The line numbers don't show up, but I've read through the code a few times and a found a few bugs. First, the private variable a should be declared as an long array, not just a long. The reasons being is because later you initialize a as an array but you have it previously defined as a long.

        private long a[];

The find() functions should have a return type of long not int.

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

Also, I would not be surprised if you got some sort of logical error with the following line.

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

It would be best to increment in two steps like so

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

The reason being is that you skip index 0 and go right to index 1;

Joel
  • 11
  • 1