Reputation: 35
I am doing a school project in java and I am trying, in a method, to refer to the class.
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
So in order for me to finish this code, all I need is a way to reference the class object NumberIndex itself.
Upvotes: 0
Views: 52
Reputation: 811
Since add word is a member function use this. that refers to the current NumberIndex object.
EDIT:
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
}
}
}
Upvotes: 2