Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Array

Array

Scheduled Pinned Locked Moved C#
data-structuresquestion
12 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D David1987

    1 is an array of arrays of arrays of ints, initialized to a length of 3 (that is, the outer array) 2 is an two-dimensional array of arrays of arrays of ints, initialized to a size of 3 by 2 (the outer, 2d array) They are already initialized, so maybe you meant to ask how to initialize the sub-arrays? For 1, do

    = new int[length][];
    For 2, do secondarray[x,y] = new int[length][];

    M Offline
    M Offline
    messages
    wrote on last edited by
    #3

    oh Thanks for your answer it was very helpful,I want to know how can I fill them? for example int[][] test= new int[3][]; test[0] = new int[5] { 1, 3, 5, 7, 9 }; .... but I dont know about these arrays can you help me? Thanks again

    D OriginalGriffO 2 Replies Last reply
    0
    • M messages

      oh Thanks for your answer it was very helpful,I want to know how can I fill them? for example int[][] test= new int[3][]; test[0] = new int[5] { 1, 3, 5, 7, 9 }; .... but I dont know about these arrays can you help me? Thanks again

      D Offline
      D Offline
      David1987
      wrote on last edited by
      #4

      Yes it's also in my message

      1 Reply Last reply
      0
      • M messages

        oh Thanks for your answer it was very helpful,I want to know how can I fill them? for example int[][] test= new int[3][]; test[0] = new int[5] { 1, 3, 5, 7, 9 }; .... but I dont know about these arrays can you help me? Thanks again

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #5

        What David means is: Every time you create an array, you have to initialize it:

                int\[\] test = new int\[3\];
        

        Declares a variable that refers to an array of integers, calls it test, and assigns an array of three integers to it. You can now use test[0], test[1], and test[2] perfectly happily.

                int\[\]\[\] test = new int\[3\]\[\];
        

        Declares a variable that refers to an array of arrays of integers, calls it test, and assigns an array of three integer arrays to it. It does not assign the arrays of integers. To do that, you either need a static declaration, or a loop:

                int\[\]\[\] test = new int\[3\]\[\] { new int\[\] { 1, 2, 3 }, 
                                              new int\[\] { 4, 5, 6 }, 
                                              new int\[\] { 7, 8, 9, 10 } };
        

        Or:

                int\[\]\[\] test = new int\[3\]\[\];
                for (int i = 0; i < test.Length; i++)
                    {
                    test\[i\] = new int\[3\];
                    }
        

        You need to repeat the process for each layer of array of arrays you add! Beware: arrays of arrays start to take significant amounts of space, very, very quickly... These are all "jagged" arrays: the inner arrays are not necessarily the same size. You can also declare rectangular arrays:

                int\[,\] test = new int\[3,4\];
        

        This declares a rectangular array of 12 elements in total, and assigns them all. You can happily use test[0,0], test[0,1]... etc. You can combine them as you did in your example:

                int\[,\]\[\] test = new int\[3,4\]\[\];
        

        Declares a rectangular array of 12 jagged arrays. You have to initialize each of these to a new array of ints before you can use them, either statically, or in a loop as before. Note: Normally, I would recommend using a foreach loop rather than a for loop, but that is not possible when filling in jagged arrays, as you cannot change the loop variable:

                int\[\]\[\] test = new int\[3\]\[\];
                foreach (int\[\] inner in test)
                    {
                    inner = new int\[3\];
                    }
        

        Will give you a compilation error.

        Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        P M 2 Replies Last reply
        0
        • OriginalGriffO OriginalGriff

          What David means is: Every time you create an array, you have to initialize it:

                  int\[\] test = new int\[3\];
          

          Declares a variable that refers to an array of integers, calls it test, and assigns an array of three integers to it. You can now use test[0], test[1], and test[2] perfectly happily.

                  int\[\]\[\] test = new int\[3\]\[\];
          

          Declares a variable that refers to an array of arrays of integers, calls it test, and assigns an array of three integer arrays to it. It does not assign the arrays of integers. To do that, you either need a static declaration, or a loop:

                  int\[\]\[\] test = new int\[3\]\[\] { new int\[\] { 1, 2, 3 }, 
                                                new int\[\] { 4, 5, 6 }, 
                                                new int\[\] { 7, 8, 9, 10 } };
          

          Or:

                  int\[\]\[\] test = new int\[3\]\[\];
                  for (int i = 0; i < test.Length; i++)
                      {
                      test\[i\] = new int\[3\];
                      }
          

          You need to repeat the process for each layer of array of arrays you add! Beware: arrays of arrays start to take significant amounts of space, very, very quickly... These are all "jagged" arrays: the inner arrays are not necessarily the same size. You can also declare rectangular arrays:

                  int\[,\] test = new int\[3,4\];
          

          This declares a rectangular array of 12 elements in total, and assigns them all. You can happily use test[0,0], test[0,1]... etc. You can combine them as you did in your example:

                  int\[,\]\[\] test = new int\[3,4\]\[\];
          

          Declares a rectangular array of 12 jagged arrays. You have to initialize each of these to a new array of ints before you can use them, either statically, or in a loop as before. Note: Normally, I would recommend using a foreach loop rather than a for loop, but that is not possible when filling in jagged arrays, as you cannot change the loop variable:

                  int\[\]\[\] test = new int\[3\]\[\];
                  foreach (int\[\] inner in test)
                      {
                      inner = new int\[3\];
                      }
          

          Will give you a compilation error.

          Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

          P Offline
          P Offline
          Prasanta_Prince
          wrote on last edited by
          #6

          Good one.

          1 Reply Last reply
          0
          • D David1987

            1 is an array of arrays of arrays of ints, initialized to a length of 3 (that is, the outer array) 2 is an two-dimensional array of arrays of arrays of ints, initialized to a size of 3 by 2 (the outer, 2d array) They are already initialized, so maybe you meant to ask how to initialize the sub-arrays? For 1, do

            = new int[length][];
            For 2, do secondarray[x,y] = new int[length][];

            M Offline
            M Offline
            messages
            wrote on last edited by
            #7

            Thanks for answer. :-D

            1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              What David means is: Every time you create an array, you have to initialize it:

                      int\[\] test = new int\[3\];
              

              Declares a variable that refers to an array of integers, calls it test, and assigns an array of three integers to it. You can now use test[0], test[1], and test[2] perfectly happily.

                      int\[\]\[\] test = new int\[3\]\[\];
              

              Declares a variable that refers to an array of arrays of integers, calls it test, and assigns an array of three integer arrays to it. It does not assign the arrays of integers. To do that, you either need a static declaration, or a loop:

                      int\[\]\[\] test = new int\[3\]\[\] { new int\[\] { 1, 2, 3 }, 
                                                    new int\[\] { 4, 5, 6 }, 
                                                    new int\[\] { 7, 8, 9, 10 } };
              

              Or:

                      int\[\]\[\] test = new int\[3\]\[\];
                      for (int i = 0; i < test.Length; i++)
                          {
                          test\[i\] = new int\[3\];
                          }
              

              You need to repeat the process for each layer of array of arrays you add! Beware: arrays of arrays start to take significant amounts of space, very, very quickly... These are all "jagged" arrays: the inner arrays are not necessarily the same size. You can also declare rectangular arrays:

                      int\[,\] test = new int\[3,4\];
              

              This declares a rectangular array of 12 elements in total, and assigns them all. You can happily use test[0,0], test[0,1]... etc. You can combine them as you did in your example:

                      int\[,\]\[\] test = new int\[3,4\]\[\];
              

              Declares a rectangular array of 12 jagged arrays. You have to initialize each of these to a new array of ints before you can use them, either statically, or in a loop as before. Note: Normally, I would recommend using a foreach loop rather than a for loop, but that is not possible when filling in jagged arrays, as you cannot change the loop variable:

                      int\[\]\[\] test = new int\[3\]\[\];
                      foreach (int\[\] inner in test)
                          {
                          inner = new int\[3\];
                          }
              

              Will give you a compilation error.

              Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

              M Offline
              M Offline
              messages
              wrote on last edited by
              #8

              your answer was very helpful for me thank you. :-D but may I ask a question? what is it? and how can I fill it? double[ , , ,] m_array4 = new double[,,,]; Thanks

              D OriginalGriffO 2 Replies Last reply
              0
              • M messages

                your answer was very helpful for me thank you. :-D but may I ask a question? what is it? and how can I fill it? double[ , , ,] m_array4 = new double[,,,]; Thanks

                D Offline
                D Offline
                David1987
                wrote on last edited by
                #9

                A 4-dimensional array of doubles - and the first time I've seen one. Like this: m_array4[x,y,z,w] = 1.0;

                1 Reply Last reply
                0
                • M messages

                  your answer was very helpful for me thank you. :-D but may I ask a question? what is it? and how can I fill it? double[ , , ,] m_array4 = new double[,,,]; Thanks

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #10

                  Not like that! :laugh: If you look back at my previous answer, that is a rectangular array (doubles or ints, bools or TextBoxes, it doesn't matter what the type is) so you have to tell the compiler how big it is!

                  double[ , , ,] m_array4 = new double[,,,];

                  Will give a compilation error, because new double[,,,] does not specify all (or indeed any) of the dimensions. Without them, the space cannot be allocated, and it will complain.

                  double[ , , ,] m_array4 = new double[3,4,5,6];

                  Will do it, but - remember I said they get big quickly? - it allocates space for 360 doubles...

                  Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  1 Reply Last reply
                  0
                  • M messages

                    Hi everyone could you tell me what are these? and how we can initialize them? (1) int[][][] array = new int[3][][]; (2) int[,][][] secondarray= new int[3,2][][]; Thanks

                    A Offline
                    A Offline
                    Abhinav S
                    wrote on last edited by
                    #11

                    These are multidimensional arrays. For more information, see here[^].

                    The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.

                    1 Reply Last reply
                    0
                    • M messages

                      Hi everyone could you tell me what are these? and how we can initialize them? (1) int[][][] array = new int[3][][]; (2) int[,][][] secondarray= new int[3,2][][]; Thanks

                      A Offline
                      A Offline
                      ambarishtv
                      wrote on last edited by
                      #12

                      A jagged array is an array whose elements are arrays http://msdn.microsoft.com/en-us/library/2s05feca.aspx[^]

                      -ambarish-

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups