Getting access to an ArrayList within an ArrayList

Go To StackoverFlow.com

0

I'm sure the first response will be 'wtf, don't use arraylists!', but I'm really just trying to get this working in any way that I can.

It's basically the 'match looker' for a match 3 game. I'm having trouble getting access to the match data within the match list. See below..

    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;
}   

Good news is, I know that it's finding the matches correctly. The number of matches it's finding using the debug log correlates with what's happening on the screen. Yay! But, I need to get access to that data so I can compare it to the board (mBoard[col,row]) and remove those objects from the game board.

The 'foreach' loop in findandremovematches gives an error about casting. Any help with this?

thanks!

2012-04-04 19:07
by Salx
It would help to have the exact error message - mobiGeek 2012-04-04 19:12
You were wrong about the first response : - Gert Arnold 2012-04-04 19:14
wtf, don't use arraylists - Justin Kirk 2012-04-04 19:15
Possible duplicate of C# Array List of ArrayListJim Fell 2016-05-12 20:31


2

I hope i understand your question correctly

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
 }
}

However, as suggested, use List<ArrayList> or List<List<int[,]>> if I guessed it right.

2012-04-04 19:22
by ata


2

If you're using ArrayLists because you think they're easier, don't. Use List. If you're using ArrayLists because you have to and there is no way for you to use List (which I doubt) then you'll have something like this:

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

Replace SomeClassThatYouAddToInnerLists with whatever the type is that you're adding to the inner ArrayLists. (The type of mBoard.)

If you use Lists then it's very clear what the type is of everything inside of the list. A List<List<int>> is easy to work with. It's a list of lists of numbers.

2012-04-04 19:22
by Servy
Ads