Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

removing rows/columns from 2d arrays

Status
Not open for further replies.

wallaceoc80

Programmer
Jul 7, 2004
182
GB
Hi,

I have an array of arrays decalred as follows:

Code:
double[][] egArray = new double[19][];
and populated as you would expect.

Later in the program there are situations when I want to remove a corresponding row/column e.g. egArray[2][2].

Is there any easy way of doing this without having to have a number of different loops?

Thanks,
Wallace
 
As far as I am aware there is no built in way to do this? If you want to get away from doing all this moving around of data then perhaps you could use the ArrayList Class?

Code:
ArrayList arraylist = new ArrayList();
....// Fill Array
.....
arraylist.RemoveAt(index);

if you use this dont forget to add
using System.Collections;
:)

Age is a consequence of experience
 
litton1 is right so you could cast it to an ArrayList then cast it back to an Array.
Code:
using System;
using System.Collections;
class POCRemoveFromArray
{
    static void Main(string[] args)
    {
        double[][] egArray = new double[3][];
        for(int i = 0; i < 3; ++i)
        {
            egArray[i] = new double[] {1,2,3,4,5};
            foreach (double dbl in egArray[i])
            {
                Console.Write(dbl.ToString());
            }
            Console.WriteLine();
        }
        Console.WriteLine("remove element from array");
        egArray[2] = RemoveDoubleArrayItem(egArray[2],2);
        for(int i = 0; i < 3; i++)
        {
            foreach (double dbl in egArray[i])
            {
                Console.Write(dbl.ToString());
            }
            Console.WriteLine();
        }
    }
    public static double[] RemoveDoubleArrayItem(double[] doubleArray, int removeIndex) 
    {
        ArrayList doubleArrayList = new ArrayList(doubleArray);
        doubleArrayList.RemoveAt(removeIndex);
        double[] returnDoubleArray = (double[])doubleArrayList.ToArray(typeof(double));
        return returnDoubleArray;
    }
}
Marty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top