Reputation: 437
I have a has a relationship between 2 classes: one is abstract class Region and the other is class KenKen. I have an ArrayList of type Region in class KenKen. How can I access the method addValue in Region class?
The classes are as follows:
Kenken:
public class KenKen {
private ArrayList<Region> regions;
public KenKen(String filename)throws FileNotFoundException{
//constructor
regions = new ArrayList<Region>();
//regions.addValue(0);//
}
public static void main(String[] args) throws FileNotFoundException{
KenKen kenken1 = new KenKen("input.1.txt");
}
}
Region:
public abstract class Region{
private int number = 0;
protected int target = 0;
protected ArrayList<Integer> values;
public Region(int number , int target){
this.number = number;
this.target = target;
//constructor
values = new ArrayList<Integer>();
}
public void addValue(int val){
}
public abstract boolean verify();
public String toString(){
}
}
Upvotes: 1
Views: 531
Reputation: 3956
You can use get()
method to reach region objects in the list.
regions.get(0).addValue(0);
Upvotes: 1
Reputation: 131
You need a public method in the KenKen class to acces the region list.
public List<Region> getRegions() {
return regions;
}
and then you can call addValue()
getRegions().get(index).addValue(value);
Upvotes: 0
Reputation: 2135
regions is an ArrayList of Regions, you should do something like
regions.get(index).addValue(x);
Upvotes: 1
Reputation: 15320
To access a member of an ArrayList
you should use get(int index)
:
Returns the element at the specified position in this list.
So:
regions.get(index).addValue(0);
Upvotes: 1