2016-03-21 31 views
1

我有一个有2个类之间的关系:一个是抽象类区域和另一个是类KenKen。我有KenKen类中的Region类型的ArrayList。我如何访问Region类中的addValue使用抽象类构成关系-java

的类如下:

啃啃:

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"); 


    } 

} 

地区:

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(){ 


    } 

} 

回答

1

要访问ArrayList的一员,你应该使用get(int index)

在此列表中指定位置返回元素。

所以:

regions.get(index).addValue(0); 
1

您可以使用get()法列表中的到达区域对象。

regions.get(0).addValue(0);

1

区域是地区的一个ArrayList,你应该这样做

regions.get(index).addValue(x); 
0

您需要在KENKEN类的公共方法来存取权限的区域列表。

public List<Region> getRegions() { 
    return regions; 
} 

,然后你可以调用的addValue()

getRegions().get(index).addValue(value);