none
2 Dimensional ArrayList

    Question

  •  

    Is it possible to create a 2-D ArrayList in visual C++?

     

    Right now i have a 2-D array that I want to dynamically update when i run my program.  The problem is the Insert and Remove methods that can be used on an ArrayList can't be used on an Array.  I also can't use the ArrayList in for  loop to access its elements 1 by one and insert into the array.  Please see my code below, any suggestions would be welcome...

     

     

    //Create 2d array

    array< double, 2 > ^m_2DArray = gcnew array < double, 2 > ( 20, 2 );

     

    //Create arraylist

    ArrayList^ m_ArrayList = gcnew ArrayList;

     

    //fill arraylist with zero's (will be updated with other values later)

    for (int y = 0; y<=19; y++)

    {

    m_ArrayList->Insert( y, 0 );

    }

     

    //Populate 2D array

    double m_2DSum = 0;

    for ( int i = 0; i <= m_2DArray->GetUpperBound( 0 ); i++ )

    {

    for ( int j = 0; j <= m_2DArray->GetUpperBound( 1 ); j++ )

    { if( j== 0)

     

     

    {

    m_2DArray[ i, j ]= m_ArrayList [ i ] ; //THIS IS WHERE MY PROBLEM IS

    m_2DSum += m_2DArray[ i, j ];

    Console::WriteLine("Element at row: " +i+ " column: " +j+ " is " +m_2DArray[i,j]);

    }

     

    else

    {

    m_2DArray[ i, j ] =i;

    m_2DSum += m_2DArray[ i, j ];

    Console::WriteLine("Element at row: " +i+ " column: " +j+ " is " +m_2DArray[i,j]);

    }

     

     

    }

    }

    Monday, April 21, 2008 8:44 PM

Answers

  • You could flatten out the array into the ArrayList.  You'd need 20*2 = 40 elements in the list and index them with [GetUpperBound(0) * i + j].  Or make an ArrayList of ArrayLists.
    Tuesday, April 22, 2008 12:38 AM
    Moderator