我必须重新创建 arraylist 类,并且我遇到了一些方法的问题。首先,我在最初的 IF 语句中的 FOR 循环中的 .equal 方法周围不断出现错误,告诉我它找不到符号,指向 getData() 和 contains 之间的句点。另外,当我制作 main 时,我不确定是否应该添加一个构造函数,难道我不能以某种方式使用 myIntArrayList 中的那个吗?我遇到了 main 不调用子类方法的问题。你能帮我解决这个问题吗?我的方法是否正确?他希望我们使用父类中的构造函数,但是当我通过创建一个新对象来使用它们时,它不会调用子类中的方法。
public class Project7 extends myIntArrayList{
public int[] copy(myIntArrayList aList){
int[] temp = new int[aList.size()];
for(int i =0; i<temp.length;i++){
temp[i]=aList.getData(i);
}
return temp;
}
public boolean equal(myIntArrayList aList){
boolean check = false;
boolean flag = true;
if(aList.size() == getData().length){
while(flag == true){
for(int i=0;i<getData().length;i++){
if(getData().contains(aList.getData(i)))//error
check=true;
else{
check=false;
flag=false;
}
}
}
}
else
check = false;
return check;
}
public void congruent(myIntArrayList aList){ //Not Done
boolean check = false;
boolean flag = true;
}
public int[] simpleSort(){
int[] temp = new int[getSize()];
for(int i=0; i<temp.length;i++){
temp[i] = getData(i);
}
for(int b=1;b<temp.length;b++){
int a=b;
while(temp[a-1]>temp[b]){
temp[a]=temp[a-1];
a--;
}
temp[a]=temp[b];
}
return temp;
}
public void bubbleSort(){
int[] temp = new int[getSize()];
for(int i=0; i<temp.length;i++){
temp[i] = getData(i);
}
for(int i=0;i<temp.length;i++){
for(int j=i;j<temp.length;j++){
if(temp[i]<temp[j]){
int swap=temp[j];
temp[j]=temp[i];
temp[i]=swap;
}
}
}
}
public static void main(String[] args){
int[] x = new int[6];
x[0] = 5;
x[1] = 8;
x[2] = 3;
x[3] = 4;
x[4] = 1;
x[5] = 9;
myIntArrayList example = new myIntArrayList(x); //Do i need a Project7 constructor?
example.print();
example.bubbleSort();
}
}
假设 myIntArrayList 中的所有方法和构造函数都可以正常工作,否则我也可以上传到 mediafire 或发布它。