-
Oct 18th, 2021, 02:28 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] How to delete my Rows in 2-D Array efficiently?
I have an 2-D array, the structure is Data[Cells, Rows] as screenshot.
When I remove Row #1, Row #2 and followings will move up. I use for...next loop and it works fine.
But how to manipulate this Data[] array efficiently without looping?
Code:
for (int col = 0; col <= Data.GetUpperBound(0); col++)
{
for (int row = RemoveRowIndex; row <= Data.GetUpperBound(1) - DeleteRowCount; row++)
{
Data[col, row] = Data[col, row + DeleteRowCount]; //Move up
}
}
Here:
RemoveRowIndex = 1; //Row to be removed
DeleteRowCount = 1; //total Rows to be deleted
Last edited by DaveDavis; Oct 18th, 2021 at 02:41 AM.
-
Oct 18th, 2021, 03:05 AM
#2
Re: How to delete my Rows in 2-D Array efficiently?
There is no way to do it without looping. If you want to be able to actually remove rows then don't use a 2D array. Use a List<T> where T is an appropriate type, which might be a 1D array or something else.
-
Oct 18th, 2021, 04:07 AM
#3
Thread Starter
Fanatic Member
Re: How to delete my Rows in 2-D Array efficiently?
Originally Posted by jmcilhinney
There is no way to do it without looping. If you want to be able to actually remove rows then don't use a 2D array. Use a List<T> where T is an appropriate type, which might be a 1D array or something else.
I thought I can use Array.Copy to copy all bottom Rows at new Index, but haven't yet figured out.
But If delete the columns, then I am accomplished to use Array.Copy to do the job:
Code:
//for (int col = lFirstDeleteCol; col <= LastColumnIndex; col++) /
//{
// for (int row = 0; row <= LastRowIndex; row++)
// {
// Data[col, row] = OriginalData[col + lDeleteColCount, row];
// }
//}
//using Array.Copy:
Array.Copy(OriginalData, (lFirstDeleteCol+ lDeleteColCount) * OriginalData.GetLength(1), Data, lFirstDeleteCol * OriginalData.GetLength(1), (LastColumnIndex - lFirstDeleteCol + 1) * OriginalData.GetLength(1));
Last edited by DaveDavis; Oct 18th, 2021 at 04:15 AM.
-
Oct 20th, 2021, 04:53 AM
#4
Thread Starter
Fanatic Member
Re: How to delete my Rows in 2-D Array efficiently?
Originally Posted by DaveDavis
I thought I can use Array.Copy to copy all bottom Rows at new Index, but haven't yet figured out.
But If delete the columns, then I am accomplished to use Array.Copy to do the job:
Code:
//for (int col = lFirstDeleteCol; col <= LastColumnIndex; col++) /
//{
// for (int row = 0; row <= LastRowIndex; row++)
// {
// Data[col, row] = OriginalData[col + lDeleteColCount, row];
// }
//}
//using Array.Copy:
Array.Copy(OriginalData, (lFirstDeleteCol+ lDeleteColCount) * OriginalData.GetLength(1), Data, lFirstDeleteCol * OriginalData.GetLength(1), (LastColumnIndex - lFirstDeleteCol + 1) * OriginalData.GetLength(1));
OK. I figured out. but as jmcilhinney said, Array.Copy can't eliminate for...loop completely, but the loop can reduced to 1/RowCount times.
Last edited by DaveDavis; Oct 20th, 2021 at 05:34 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|