Results 1 to 4 of 4

Thread: [RESOLVED] How to delete my Rows in 2-D Array efficiently?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    597

    Resolved [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

    Name:  Table Structure.PNG
Views: 226
Size:  3.4 KB
    Last edited by DaveDavis; Oct 18th, 2021 at 02:41 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    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.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    597

    Re: How to delete my Rows in 2-D Array efficiently?

    Quote Originally Posted by jmcilhinney View Post
    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));
    Name:  Table Structure_Del Cols.PNG
Views: 228
Size:  1.3 KB
    Last edited by DaveDavis; Oct 18th, 2021 at 04:15 AM.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    597

    Re: How to delete my Rows in 2-D Array efficiently?

    Quote Originally Posted by DaveDavis View Post
    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));
    Name:  Table Structure_Del Cols.PNG
Views: 228
Size:  1.3 KB
    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
  •  



Click Here to Expand Forum to Full Width