此问题已在此处找到答案:
“找不到符号”或“无法解析符号”错误是什么意思((17个答案)
昨天关门了。
这是我正在处理数据结构的类的代码。现在我们正试图和max heap合作,我想我遇到了麻烦。当我尝试在jgrasp中编译这些数据时,我在build方法周围得到了一个“java:82错误:找不到符号”,特别是“data.insert(arr[z]);”它说找不到insert方法。
通常,当我遇到这个错误时,我认为是拼写或大小写错误,但经过四重检查,我肯定不是这样。在build方法之外运行insert方法似乎很好,只是我似乎无法在任何其他方法中运行insert方法。我觉得我在这里犯了一个明显的错误,但我无法确定。
import java.util.*;
class HeapMax {
// we go with arraylist instead of array for size flexibility
private ArrayList<Integer> data;
int Size;
// default constructor
public HeapMax() {
data = new ArrayList<Integer>(0);
//adding an integer to the dataset just makes it so that I can work from position 1 rather than 0
//I think this is somewhat easier for the math that needs to be done
//being max value ensures it is always at the front and doesn't accidentally get sorted, like a 0 would
data.add(Integer.MAX_VALUE);
//size variable for future reference
Size = 0;
}
// DO NOT MODIFY THIS METHOD
public ArrayList<Integer> getData() {
return data;
}
// insert a new element and restore max heap property
public void insert(int element) {
data.add(element);
//increases size for future reference
Size++;
int CurrentSize = Size;
//organizes leaves in accordance with their parents
while(data.get(CurrentSize) > data.get(CurrentSize/2)){
int reserveInt = data.get(CurrentSize);
data.set(CurrentSize, data.get(CurrentSize/2));
data.set(CurrentSize/2, reserveInt);
CurrentSize = (CurrentSize/2);
}
}
// return max
public int getMax() {
// remove this line
return 0;
}
// remove max and restore max heap property
public int removeMax() {
return 0;
}
// print out heap as instructed in the handout
public void display() {
System.out.print("Current heap is: ");
int currentTierSize = 1;
int currentSpot = 1;
//iterates over the array, printing everything but the very last tier
//this is because the final tier is not necessarily full, and continuing would result in null pointer exception
while (currentSpot < (Size-1)){
for(int i = 0; i < currentTierSize; i++){
System.out.print(data.get(currentSpot)+ " ");
currentSpot++;
}
System.out.print(",");
//tiers are in iterations of two, so the next will always be twice as large as the last
currentTierSize = currentTierSize * 2;
}
//the size of the final tier will always be determinable by the differce between the place the previous iteration left off and the size of the full array
//this iterates over those spaces, finishing the display without an exception
int finalTierSize = (Size - (currentSpot-1));
for(int j = 0; j < finalTierSize; j++){
System.out.print(data.get(currentSpot) + " ");
currentSpot++;
}
System.out.println(".");
}
// heap builder
public void build(int[] arr) {
for(int z = 0; z < arr.length; z++){
data.insert(arr[z]);
}
}
// you are welcome to add any supporting methods
public static void main(String[] args){
HeapMax data = new HeapMax();
int[] testArray = {5,3,17,10,84,19,6,22,9};
data.build(testArray);
data.display();
}
}
2条答案
按热度按时间dgsult0t1#
答案是相当琐碎的‘嗯,你为什么认为ArrayList有一个
insert
方法?因为。。他们没有。编译器没有骗你。如果您想在列表的最后添加一项,
data.add(arr[z])
这大概是你想要的。如果你打算
insert
意思是:“把这个项目放在正确的地方”,然后ArrayList
是错误的数据类型,您可能想要一个TreeSet
相反,此时您只需调用.add(arr[z])
同样,树集将确保将其添加到适当的位置。阅读是基础。这对javadoc来说是两倍。
cclgggtu2#
是的,因为arraylist没有内置的insert方法。如果要向数据添加元素,请使用add方法