user7378542 user7378542 - 9 months ago 31
Java Question

if ( l <= A_heap_size && A.get(l).key < A.get(i).key ) // ERROR: cannot find symbol

I have an error message "cannot find symbol" associated with this part of the condition:

PQHeap.java:26: error: cannot find symbol
if ( l <= A_heap_size && A.get(l).key < A.get(i).key )
^
symbol: variable key
location: class Object
PQHeap.java:26: error: cannot find symbol
if ( l <= A_heap_size && A.get(l).key < A.get(i).key )
^
symbol: variable key
location: class Object
PQHeap.java:31: error: cannot find symbol
if ( r <= A_heap_size && A.get(r).key < A.get(smallest).key )
^
symbol: variable key
location: class Object
PQHeap.java:31: error: cannot find symbol
if ( r <= A_heap_size && A.get(r).key < A.get(smallest).key )
^
symbol: variable key
location: class Object
PQHeap.java:35: error: cannot find symbol
Collections.swap( A, i, smallest );
^
symbol: variable Collections
location: class PQHeap
PQHeap.java:42: error: constructor Element in class Element cannot be applied to given types;
Element max = new Element();
^
required: int,Object
found: no arguments
reason: actual and formal argument lists differ in length
PQHeap.java:44: error: no suitable method found for set(int)
A.set(1).key = A.get(A_heap_size).key;
^
method List.set(int,Element) is not applicable
(actual and formal argument lists differ in length)
method AbstractList.set(int,Element) is not applicable
(actual and formal argument lists differ in length)
method ArrayList.set(int,Element) is not applicable
(actual and formal argument lists differ in length)
PQHeap.java:58: error: cannot find symbol
Collections.swap(A, i, i/2);
^
symbol: variable Collections
location: class PQHeap
8 errors


Below is extra info.
Encoding: UTF-8.
ArrayLIst is imported (import java.util.ArrayList;) prior to class definition.
A_heap_size defined outside method as private int.
A defined outside method as public ArrayList .
Element constructor has an instance variable int key.
(Element class is a facit I got, so error is not from there. Element.java compiled flawlessly).

Thanks in advance

P.S.: when I remove .key I get another error but I'm not sure though whether above described problem stems from that

Answer Source

You're calling A.get(). That returns a java.lang.Object. You're trying to access the key attribute f this object. But there is no attribute key in the class Object. Hence the error.

You're probaly using raw types, i.e. using a List instead of a List<MyClassWhichHasAKeyAttribute>. Don't use raw types.

There are several other potential explanations, but since you won't post your code, this is the best guess we can make.

The other errors come from not importing a class, or from calling methods that don't exist. Read the javadoc.