Clear Array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ron

    #1

    Clear Array

    Hi,
    Could i clear the a single dimension array and a jagged
    array in the following way.

    int[] mySingleArr = new int[10];

    mySingleArr = System.Array.Cl ear();

    int[][] myJaggedArr = new int[3][];
    myJaggedArr = new int[0][3];
    myJaggedArr = new int[1][3];
    myJaggedArr = new int[2][3];

    myJaggedArr = System.Array.Cl ear();

    Is this possiable without iterating through the arrays
    and setting the elements to zero?

    Thanks Ron

  • Chris LaJoie

    #2
    Re: Clear Array

    I don't know what exactly you're getting at, but why don't you just use
    multi-dimensional arrays like this:

    int[,] a2Darray = new int[4, 10];
    a2Darray[0,0] = 1;
    a2Darray[0,1] = 2;
    ....[etc]...

    What you're doing with your "jagged" array is creating an array inside of an
    array.. that's old C style. C# has new built-in features for
    multi-dimensional arrays.

    Chris LaJoie


    "ron" <goodr@no_spam_ for_me_mddm.pan asonic.com> wrote in message
    news:037801c363 59$9d123080$a10 1280a@phx.gbl.. .[color=blue]
    > Hi,
    > Could i clear the a single dimension array and a jagged
    > array in the following way.
    >
    > int[] mySingleArr = new int[10];
    >
    > mySingleArr = System.Array.Cl ear();
    >
    > int[][] myJaggedArr = new int[3][];
    > myJaggedArr = new int[0][3];
    > myJaggedArr = new int[1][3];
    > myJaggedArr = new int[2][3];
    >
    > myJaggedArr = System.Array.Cl ear();
    >
    > Is this possiable without iterating through the arrays
    > and setting the elements to zero?
    >
    > Thanks Ron
    >[/color]


    Comment

    • Jon Skeet

      #3
      Re: Clear Array

      ron <goodr@no_spam_ for_me_mddm.pan asonic.com> wrote:[color=blue]
      > Could i clear the a single dimension array and a jagged
      > array in the following way.
      >
      > int[] mySingleArr = new int[10];
      >
      > mySingleArr = System.Array.Cl ear();[/color]

      No, because Array.Clear takes arguments and returns void. You mean:

      Array.Clear (mySingleArr, 0, mySingleArr.Len gth);
      [color=blue]
      > int[][] myJaggedArr = new int[3][];
      > myJaggedArr = new int[0][3];
      > myJaggedArr = new int[1][3];
      > myJaggedArr = new int[2][3];
      >
      > myJaggedArr = System.Array.Cl ear();
      >
      > Is this possiable without iterating through the arrays
      > and setting the elements to zero?[/color]

      In this case you're not actually wanting to clear myJaggedArr itself,
      which would set it back to being an array of 3 null references, but
      each member of those three arrays - so you need to do precisely that:

      foreach (int[] subArray in myJaggedArr)
      {
      Array.Clear (subArray, 0, subArray.Length );
      }

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      Working...