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. 2d array in c#

2d array in c#

Scheduled Pinned Locked Moved C#
csharpjavadata-structureshelpquestion
21 Posts 5 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.
  • A Offline
    A Offline
    Aljaz111
    wrote on last edited by
    #1

    Ok in java it looks like this: private static int maxState = 10; private int [][] avtomat = new int[maxState][256]; Now i would like to declare same array in c#. When i tryed private int [][] avtomat = new int[maxState][]; and fill array with values like this avtomat[0][0]=-1 I get error "An unhandled exception of type 'System.NullReferenceException' occurred in.. Additional information: Object reference not set to an instance of an object." What am i doing wrong? Thx

    P L D 3 Replies Last reply
    0
    • A Aljaz111

      Ok in java it looks like this: private static int maxState = 10; private int [][] avtomat = new int[maxState][256]; Now i would like to declare same array in c#. When i tryed private int [][] avtomat = new int[maxState][]; and fill array with values like this avtomat[0][0]=-1 I get error "An unhandled exception of type 'System.NullReferenceException' occurred in.. Additional information: Object reference not set to an instance of an object." What am i doing wrong? Thx

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      private int [][] avtomat = new int[maxState][/* This is your problem. You haven't set it to anything */];

      It's a null parameter.

      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

      My blog | My articles | MoXAML PowerToys | Onyx

      A L 2 Replies Last reply
      0
      • P Pete OHanlon

        private int [][] avtomat = new int[maxState][/* This is your problem. You haven't set it to anything */];

        It's a null parameter.

        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

        My blog | My articles | MoXAML PowerToys | Onyx

        A Offline
        A Offline
        Aljaz111
        wrote on last edited by
        #3

        But if i add value to second []..i get this error CS0178: Invalid rank specifier: expected ',' or ']'.

        D P 2 Replies Last reply
        0
        • A Aljaz111

          But if i add value to second []..i get this error CS0178: Invalid rank specifier: expected ',' or ']'.

          D Offline
          D Offline
          Dan Mos
          wrote on last edited by
          #4

          use something like

          int blah[,] = new int[80,20]

          [Edit] C# has two kind of multidimensional array: jagged and rectangular

          int[,] numbers = new int[,] { {1, 2}, {3, 4}, {5, 6} };//rectangular
          int[][] numbers = new int[2][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };//jagged or array of array
          //as you can see the size of the second array in the jagged one can differ
          //more on the topic => msdn lots of examples

          [/Edit]

          modified on Friday, February 26, 2010 10:18 AM

          L A 2 Replies Last reply
          0
          • A Aljaz111

            Ok in java it looks like this: private static int maxState = 10; private int [][] avtomat = new int[maxState][256]; Now i would like to declare same array in c#. When i tryed private int [][] avtomat = new int[maxState][]; and fill array with values like this avtomat[0][0]=-1 I get error "An unhandled exception of type 'System.NullReferenceException' occurred in.. Additional information: Object reference not set to an instance of an object." What am i doing wrong? Thx

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            It's because you now have an array of int[], all of which are null. You have to create the sub-array's as well.

            1 Reply Last reply
            0
            • D Dan Mos

              use something like

              int blah[,] = new int[80,20]

              [Edit] C# has two kind of multidimensional array: jagged and rectangular

              int[,] numbers = new int[,] { {1, 2}, {3, 4}, {5, 6} };//rectangular
              int[][] numbers = new int[2][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };//jagged or array of array
              //as you can see the size of the second array in the jagged one can differ
              //more on the topic => msdn lots of examples

              [/Edit]

              modified on Friday, February 26, 2010 10:18 AM

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              :thumbsup:

              1 Reply Last reply
              0
              • A Aljaz111

                But if i add value to second []..i get this error CS0178: Invalid rank specifier: expected ',' or ']'.

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                Thanks for 1 voting me for giving you the answer. I really appreciate it. :rolleyes:

                "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                My blog | My articles | MoXAML PowerToys | Onyx

                1 Reply Last reply
                0
                • P Pete OHanlon

                  private int [][] avtomat = new int[maxState][/* This is your problem. You haven't set it to anything */];

                  It's a null parameter.

                  "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                  As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                  My blog | My articles | MoXAML PowerToys | Onyx

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Hey Pete, I hope you don't mean

                  private int [][] avtomat = new int[maxState][30];

                  Because that is not C#! Actually it's unclear what you really mean here, but if you interpret it as above you get the compiler error the OP quoted

                  D P 2 Replies Last reply
                  0
                  • L Lost User

                    Hey Pete, I hope you don't mean

                    private int [][] avtomat = new int[maxState][30];

                    Because that is not C#! Actually it's unclear what you really mean here, but if you interpret it as above you get the compiler error the OP quoted

                    D Offline
                    D Offline
                    Dan Mos
                    wrote on last edited by
                    #9

                    Totally OffTopic but what does OP stands for? I mean I know it has something to do with the person that posted the question/message but what's OP from? Object => hope and know not ;)

                    L 1 Reply Last reply
                    0
                    • D Dan Mos

                      use something like

                      int blah[,] = new int[80,20]

                      [Edit] C# has two kind of multidimensional array: jagged and rectangular

                      int[,] numbers = new int[,] { {1, 2}, {3, 4}, {5, 6} };//rectangular
                      int[][] numbers = new int[2][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };//jagged or array of array
                      //as you can see the size of the second array in the jagged one can differ
                      //more on the topic => msdn lots of examples

                      [/Edit]

                      modified on Friday, February 26, 2010 10:18 AM

                      A Offline
                      A Offline
                      Aljaz111
                      wrote on last edited by
                      #10

                      Now when i did like this int[,] array=new int[10,256] it gives me error CS0022: Wrong number of indices inside []; expected '2' when i am adding values->array[10][2]=-1 for ex. I tryed few examples from msdn still can't get it to work!!

                      L 1 Reply Last reply
                      0
                      • A Aljaz111

                        Now when i did like this int[,] array=new int[10,256] it gives me error CS0022: Wrong number of indices inside []; expected '2' when i am adding values->array[10][2]=-1 for ex. I tryed few examples from msdn still can't get it to work!!

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        Try array[10**,**2]=-1

                        A 1 Reply Last reply
                        0
                        • D Dan Mos

                          Totally OffTopic but what does OP stands for? I mean I know it has something to do with the person that posted the question/message but what's OP from? Object => hope and know not ;)

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          Original Poster[^]

                          D 1 Reply Last reply
                          0
                          • L Lost User

                            Try array[10**,**2]=-1

                            A Offline
                            A Offline
                            Aljaz111
                            wrote on last edited by
                            #13

                            yeah this works..thanks a lot;D I need something else please.. how to write this from java in c#?

                                      try {
                            		datoteka.mark(1); ?
                            		int tmp = datoteka.read(); 
                            		datoteka.reset();  ?
                            		return tmp;
                            
                            L 1 Reply Last reply
                            0
                            • A Aljaz111

                              yeah this works..thanks a lot;D I need something else please.. how to write this from java in c#?

                                        try {
                              		datoteka.mark(1); ?
                              		int tmp = datoteka.read(); 
                              		datoteka.reset();  ?
                              		return tmp;
                              
                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #14

                              Maybe this?

                              try
                              {
                              datoteka.mark(1);
                              int tmp = datoteka.read();
                              datoteka.reset();
                              return tmp;
                              }
                              catch { return -1; } //I'm just guessing here, you didn't provide enough code

                              1 Reply Last reply
                              0
                              • L Lost User

                                Hey Pete, I hope you don't mean

                                private int [][] avtomat = new int[maxState][30];

                                Because that is not C#! Actually it's unclear what you really mean here, but if you interpret it as above you get the compiler error the OP quoted

                                P Offline
                                P Offline
                                Pete OHanlon
                                wrote on last edited by
                                #15

                                That's not what I meant. I was pointing out where the null reference was occuring. If you tried the example you quote here, you get an InvalidFieldSpecifier problem, not a null reference exception.

                                "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                                As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                                My blog | My articles | MoXAML PowerToys | Onyx

                                L 1 Reply Last reply
                                0
                                • P Pete OHanlon

                                  That's not what I meant. I was pointing out where the null reference was occuring. If you tried the example you quote here, you get an InvalidFieldSpecifier problem, not a null reference exception.

                                  "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                                  As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                                  My blog | My articles | MoXAML PowerToys | Onyx

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #16

                                  Pete O'Hanlon wrote:

                                  That's not what I meant. I was pointing out where the null reference was occuring.

                                  Ok, then IMO it's an odd way of pointing that out..

                                  Pete O'Hanlon wrote:

                                  If you tried the example you quote here, you get an InvalidFieldSpecifier problem, not a null reference exception.

                                  You'd get Invalid rank specifier: expected ',' or ']' as the OP also posted, which is exactly my point, if that had been what you meant, you would have given him invalid code. And from the OP's reaction, that is how what he understood your post to mean..

                                  P 1 Reply Last reply
                                  0
                                  • L Lost User

                                    Pete O'Hanlon wrote:

                                    That's not what I meant. I was pointing out where the null reference was occuring.

                                    Ok, then IMO it's an odd way of pointing that out..

                                    Pete O'Hanlon wrote:

                                    If you tried the example you quote here, you get an InvalidFieldSpecifier problem, not a null reference exception.

                                    You'd get Invalid rank specifier: expected ',' or ']' as the OP also posted, which is exactly my point, if that had been what you meant, you would have given him invalid code. And from the OP's reaction, that is how what he understood your post to mean..

                                    P Offline
                                    P Offline
                                    Pete OHanlon
                                    wrote on last edited by
                                    #17

                                    I didn't give him code - that was the whole point. He asked why he was getting a null reference exception; I told him. What he did next was up to him - without knowing what he was trying to achieve with it, I did not feel it was my place to suggest what he should do to resolve this and nowhere in my original answer did I suggest that he try to put a parameter to put into there. I will not be held responsible for somebody reading more into an answer than was there.

                                    "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                                    As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                                    My blog | My articles | MoXAML PowerToys | Onyx

                                    L 1 Reply Last reply
                                    0
                                    • P Pete OHanlon

                                      I didn't give him code - that was the whole point. He asked why he was getting a null reference exception; I told him. What he did next was up to him - without knowing what he was trying to achieve with it, I did not feel it was my place to suggest what he should do to resolve this and nowhere in my original answer did I suggest that he try to put a parameter to put into there. I will not be held responsible for somebody reading more into an answer than was there.

                                      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                                      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                                      My blog | My articles | MoXAML PowerToys | Onyx

                                      L Offline
                                      L Offline
                                      Lost User
                                      wrote on last edited by
                                      #18

                                      I don't think I agree Pete, your post looked a lot like "code with something you have to fill in" - it still looks that way to me, and going by his reaction it also looked that way to the OP. But of course I'm not holding you responsible in any way (this is teh interwebs), I just tried (and failed, clearly) to point out how it would be confusing - you really don't need to get all defensive over it :) So, my apologies.

                                      P 1 Reply Last reply
                                      0
                                      • L Lost User

                                        I don't think I agree Pete, your post looked a lot like "code with something you have to fill in" - it still looks that way to me, and going by his reaction it also looked that way to the OP. But of course I'm not holding you responsible in any way (this is teh interwebs), I just tried (and failed, clearly) to point out how it would be confusing - you really don't need to get all defensive over it :) So, my apologies.

                                        P Offline
                                        P Offline
                                        Pete OHanlon
                                        wrote on last edited by
                                        #19

                                        I can see your point about why it looked that way, but it was never my intention to make it a "fill this bit in and it magically works". This is what I get when answering a post when juggling an installation onto 360 machines at the same time. Perhaps I could have been clearer, but I'm not getting defensive on this - I was just trying to clarify that I knew this wasn't valid C#, and my answer wasn't intended to suggest it was. Next time I'll try not to be as "clever" with where I put the pointer to the problem.

                                        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                                        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                                        My blog | My articles | MoXAML PowerToys | Onyx

                                        1 Reply Last reply
                                        0
                                        • A Aljaz111

                                          Ok in java it looks like this: private static int maxState = 10; private int [][] avtomat = new int[maxState][256]; Now i would like to declare same array in c#. When i tryed private int [][] avtomat = new int[maxState][]; and fill array with values like this avtomat[0][0]=-1 I get error "An unhandled exception of type 'System.NullReferenceException' occurred in.. Additional information: Object reference not set to an instance of an object." What am i doing wrong? Thx

                                          D Offline
                                          D Offline
                                          Dave Doknjas
                                          wrote on last edited by
                                          #20

                                          The Java rectangular array in your example is a special case in Java since Java only has jagged arrays (arrays of arrays), not multidimensional arrays, but allows a special rectangular jagged array initialization. The C# equivalent to this is: [code]//ORIGINAL LINE: private int[][] avtomat = new int[maxState][256]; //JAVA TO VB & C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: private int[][] avtomat = RectangularArrays.ReturnRectangularIntArray(maxState, 256); //---------------------------------------------------------------------------------------- // Copyright © 2008 - 2010 Tangible Software Solutions Inc. // This class can be used by anyone provided that the copyright notice remains intact. // // This class provides the logic to simulate Java rectangular arrays, which are jagged // arrays with inner arrays of the same length. //---------------------------------------------------------------------------------------- internal static class RectangularArrays { internal static int[][] ReturnRectangularIntArray(int Size1, int Size2) { int[][] Array = new int[Size1][]; for (int Array1 = 0; Array1 < Size1; Array1++) { Array[Array1] = new int[Size2]; } return Array; } }[/code] David Anton Convert between VB, C#, C++, & Java www.tangiblesoftwaresolutions.com

                                          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