Java 为子类重新创建ArrayList方法错误和构造函数错误
我必须重新创建arraylist类,而且有些方法有问题。首先,我在初始IF语句中FOR循环中的.equal方法周围不断出现错误,它告诉我找不到符号,指向getData()和contains之间的周期。另外,当我创建main时,我不确定是否应该添加构造函数,难道我不能使用myIntArrayList中的构造函数吗?我对main没有调用子类方法有问题。你能帮我吗?我的方法正确吗?他希望我们使用父类中的构造函数,但当我通过创建新对象来使用它们时,它不会调用子类中的方法Java 为子类重新创建ArrayList方法错误和构造函数错误,java,methods,arraylist,constructor,recreate,Java,Methods,Arraylist,Constructor,Recreate,我必须重新创建arraylist类,而且有些方法有问题。首先,我在初始IF语句中FOR循环中的.equal方法周围不断出现错误,它告诉我找不到符号,指向getData()和contains之间的周期。另外,当我创建main时,我不确定是否应该添加构造函数,难道我不能使用myIntArrayList中的构造函数吗?我对main没有调用子类方法有问题。你能帮我吗?我的方法正确吗?他希望我们使用父类中的构造函数,但当我通过创建新对象来使用它们时,它不会调用子类中的方法 public class Pro
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();
}
公共类项目7扩展myIntArrayList{
公共int[]副本(myIntArrayList列表){
int[]temp=new int[aList.size()];
对于(int i=0;i,您需要使用父类的构造函数来定义子类的构造函数。您可以通过调用super
来实现这一点,其中super
是对父类构造函数的调用,并且可以将所需的任何参数放在括号中
例如,如果我的父类在其构造函数中不使用参数,我只需对构造函数执行以下操作:
public Subclass() {
super();
}
然后,您将可以访问父类所做的一切以及为子类定义的一切
对于getData()
error部分,尝试使用this.length
或this.size()
或者你可以得到你的对象的大小,看看这是否有效。当我使用super()时;在数组中添加一个参数,我只得到“{}”作为我的输出。而this.length根本不起作用。没关系。我让它起作用了。我必须在方法中创建新对象,而不是使用contains,而是用于循环和if语句。