【问题标题】:Recreate ArrayList Methods Error and Constructor Error for Child Class为子类重新创建 ArrayList 方法错误和构造函数错误
【发布时间】:2013-11-20 19:51:57
【问题描述】:

我必须重新创建 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 或发布它。

【问题讨论】:

    标签: java methods arraylist constructor recreate


    【解决方案1】:

    您需要使用父类的构造函数为子类定义构造函数。您可以通过调用 super 来完成此操作,其中 super 是对父级构造函数的调用,并将所需的任何参数放在括号中。

    例如,如果我的父类在其构造函数中不使用任何参数,我只需为我的构造函数执行此操作:

    public Subclass() {
        super();
    }
    

    然后,您将可以访问父类所做的一切以及您为子类定义的一切。

    对于getData() 错误部分,请尝试使用this.lengththis.size(),或者您将获得对象的大小,看看是否有效。

    【讨论】:

      【解决方案2】:

      当我使用 super();并添加一个数组作为参数,我只得到“{}”作为我的输出。而且 this.length 根本不起作用。

      【讨论】:

      • 没关系。我让它工作了。我必须在方法中创建新对象,而不是使用 contains,而是使用 for 循环和 if 语句。
      猜你喜欢
      • 2018-11-15
      • 1970-01-01
      • 1970-01-01
      • 2012-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多