Java ArrayList类:如何获取引用该类的方法

时间:2016-01-17 22:08:59

标签: java arraylist

我正在用java做一个学校项目,我正在尝试用一种方法来引用这个类。

import java.util.ArrayList;

public class NumberIndex extends ArrayList<Integer> {
  private int s;
  public NumberIndex (){
    super();
    s = 10; // would be better if it was class.size() 
            //but I don't know how to refer to the class
  }
  public NumberIndex (int x){
    super(x);
    s = x;
  }
  public void addWord(int num) {
    for(i = 0; i < s; i++)
      //trying to make it so that for each Integer in ArrayList,
      // if there exists an Integer that has the value num, nothing would
      //happen. Else creates new Integer and adds it to the List

所以为了让我完成这段代码,我只需要一种方法来引用类对象NumberIndex本身。

1 个答案:

答案 0 :(得分:2)

由于添加单词是成员函数使用此。它引用当前的NumberIndex对象。

编辑:

public class NumberIndex extends ArrayList<Integer> {

    public NumberIndex() {
        super(10);//setting the size of your NumberIndex object -> list size
    }

    public NumberIndex(int x) {
        super(x);//setting the size of your NumberIndex object -> list size
    }

    public void addWord(int num) {
        if(!this.contains(num)){//if the current NumberIndex object (list) does not contain num
            this.add(num);//to the current NumberIndex object (list) add num
        }
    }

}