2012-04-04 60 views
0

我确定第一个响应是'wtf,不要使用arraylist!',但我真的只是试图以我能做到的任何方式工作。访问ArrayList中的ArrayList

它基本上是一场比赛3场比赛的'比赛看台'。我无法访问匹配列表中的匹配数据。见下面..

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'循环给出了有关转换的错误。任何帮助?

谢谢!

+0

这将有助于获得确切的错误消息。 – mobiGeek 2012-04-04 19:12:05

+0

你对第一个回应错了:) – 2012-04-04 19:14:41

+3

wtf,不要使用数组列表! – 2012-04-04 19:15:29

回答

2

我希望我明白你的问题正确

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<ArrayList>List<List<int[,]>>如果我猜对。

2

如果您使用ArrayLists因为您认为它们更容易,请勿。使用列表。 如果您使用的ArrayList,因为你必须要和有没有方法可以让你使用List(我怀疑),那么你就会有这样的事情:

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

更换SomeClassThatYouAddToInnerLists与任何类型的是,您将添加到内部ArrayLists。 (mBoard的类型)

如果您使用列表,那么它非常清楚列表中的所有内容。 A List<List<int>>很容易使用。这是一系列数字。