【问题标题】:Getting access to an ArrayList within an ArrayList访问 ArrayList 中的 ArrayList
【发布时间】:2012-04-04 19:07:39
【问题描述】:

我确定第一个响应会是“wtf,不要使用数组列表!”,但我真的只是想尽我所能让它工作。

它基本上是三场比赛的“比赛观察者”。我无法访问匹配列表中的匹配数据。见下文..

    public void FindAndRemoveMatches() {

    ArrayList foundMatches = LookForMatches();

    //just checking if we're getting the right amount for now
    Debug.Log("We found " + foundMatches.Count + " 'Match 3's");

    foreach(Object el in foundMatches){
       //   Debug.Log(el.ToString());
    }

}

ArrayList LookForMatches(){

    //List<int> matchList = new List<int>();
    ArrayList matchList = new ArrayList();

    // search for horizontal matches
    // note that we're subtracting two rows here.
    // We don't need to check the last two rows because we're matching 3.
    for (int i = 0; i < BOARD_WIDTH; i++){
        for (int j = 0; j < BOARD_HEIGHT-2; j++){
            ArrayList match = GetMatchHoriz(i,j);
            if (match.Count > 2) {
                matchList.Add(match);
                i += match.Count-1;
            }
        }
    }

    // search for vertical matches
    for (int i = 0; i < BOARD_WIDTH; i++){
        for (int j = 0; j < BOARD_HEIGHT-2; j++){
            ArrayList match = GetMatchVert(i,j);
            if (match.Count > 2) {
                matchList.Add(match);
                j += match.Count-1;
            }

        }
    }


    return matchList;
}


// look for horizontal matches starting at this point
ArrayList GetMatchHoriz(int col,int row){
    ArrayList match = new ArrayList();
    match.Add(mBoard[col,row]);

    for(int i = 1; (col+i)<8; i++) {
        if (mBoard[col,row] == mBoard[col+i,row]) {
            if(mBoard[col+i,row] > mPieces.GetNumPieceTypes()) match.Add(mBoard[col+i,row]);
        } else {
            return match;
        }
    }
    return match;
}   

// look for horizontal matches starting at this point
ArrayList GetMatchVert(int col,int row){
    ArrayList match = new ArrayList();
    match.Add(mBoard[col,row]);

    for(int i = 1; (row+i)<8; i++) {
        if (mBoard[col,row] == mBoard[col,row+i]) {
            if(mBoard[col,row+i] > mPieces.GetNumPieceTypes()) match.Add(mBoard[col,row+i]);
        } else {
            return match;
        }
    }
    return match;
}   

好消息是,我知道它可以正确找到匹配项。它使用调试日志找到的匹配数与屏幕上发生的事情相关。耶!但是,我需要访问该数据,以便将其与棋盘 (mBoard[col,row]) 进行比较并从游戏棋盘中删除这些对象。

findandremovematches 中的“foreach”循环给出了关于强制转换的错误。有什么帮助吗?

谢谢!

【问题讨论】:

标签: c# arrays arraylist unity3d


【解决方案1】:

希望我能正确理解你的问题

foreach(Objecct obj in list)
{
 ArrayList inner = obj as ArrayList;
 if(inner != null)
 {
  //this is what you are looking for
  //you can then iterate the inner array list and get the int[,] you saved
 }
}

但是,按照建议,如果我猜对了,请使用 List&lt;ArrayList&gt;List&lt;List&lt;int[,]&gt;&gt;

【讨论】:

    【解决方案2】:

    如果您使用 ArrayLists 是因为您认为它们更简单,请不要这样做。使用列表。 如果您使用 ArrayLists 是因为您必须这样做并且没有办法使用 List(我对此表示怀疑),那么您将拥有如下内容:

    foreach(ArrayList innerList in foundMatches)
    {
      foreach(SomeClassThatYouAddToInnerLists item in innerlist)
      {
        //use item;
      }
    }
    

    SomeClassThatYouAddToInnerLists 替换为您要添加到内部ArrayLists 的任何类型。 (mBoard的类型。)

    如果您使用列表,那么列表中所有内容的类型非常清楚。 List&lt;List&lt;int&gt;&gt; 易于使用。这是一个数字列表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-19
      • 2015-06-07
      • 1970-01-01
      • 2012-02-22
      • 2020-11-12
      • 1970-01-01
      相关资源
      最近更新 更多