Getting the array length of a 2D array in Java









up vote
92
down vote

favorite
48












I need to get the length of a 2D array for both the row and column. I’ve successfully done this, using the following code:



public class MyClass 

public static void main(String args)

int test;
test = new int[5][10];

int row = test.length;
int col = test[0].length;

System.out.println(row);
System.out.println(col);




This prints out 5, 10 as expected.



Now take a look at this line:



 int col = test[0].length;


Notice that I actually have to reference a particular row, in order to get the column length. To me, this seems incredibly ugly. Additionally, if the array was defined as:



test = new int[0][10];


Then the code would fail when trying to get the length. Is there a different (more intelligent) way to do this?










share|improve this question



























    up vote
    92
    down vote

    favorite
    48












    I need to get the length of a 2D array for both the row and column. I’ve successfully done this, using the following code:



    public class MyClass 

    public static void main(String args)

    int test;
    test = new int[5][10];

    int row = test.length;
    int col = test[0].length;

    System.out.println(row);
    System.out.println(col);




    This prints out 5, 10 as expected.



    Now take a look at this line:



     int col = test[0].length;


    Notice that I actually have to reference a particular row, in order to get the column length. To me, this seems incredibly ugly. Additionally, if the array was defined as:



    test = new int[0][10];


    Then the code would fail when trying to get the length. Is there a different (more intelligent) way to do this?










    share|improve this question

























      up vote
      92
      down vote

      favorite
      48









      up vote
      92
      down vote

      favorite
      48






      48





      I need to get the length of a 2D array for both the row and column. I’ve successfully done this, using the following code:



      public class MyClass 

      public static void main(String args)

      int test;
      test = new int[5][10];

      int row = test.length;
      int col = test[0].length;

      System.out.println(row);
      System.out.println(col);




      This prints out 5, 10 as expected.



      Now take a look at this line:



       int col = test[0].length;


      Notice that I actually have to reference a particular row, in order to get the column length. To me, this seems incredibly ugly. Additionally, if the array was defined as:



      test = new int[0][10];


      Then the code would fail when trying to get the length. Is there a different (more intelligent) way to do this?










      share|improve this question















      I need to get the length of a 2D array for both the row and column. I’ve successfully done this, using the following code:



      public class MyClass 

      public static void main(String args)

      int test;
      test = new int[5][10];

      int row = test.length;
      int col = test[0].length;

      System.out.println(row);
      System.out.println(col);




      This prints out 5, 10 as expected.



      Now take a look at this line:



       int col = test[0].length;


      Notice that I actually have to reference a particular row, in order to get the column length. To me, this seems incredibly ugly. Additionally, if the array was defined as:



      test = new int[0][10];


      Then the code would fail when trying to get the length. Is there a different (more intelligent) way to do this?







      java arrays multidimensional-array






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 22 '10 at 19:31









      robert_x44

      8,03312235




      8,03312235










      asked Oct 22 '10 at 19:15









      user432209

      14.3k95071




      14.3k95071






















          8 Answers
          8






          active

          oldest

          votes

















          up vote
          126
          down vote



          accepted










          Consider



          public static void main(String args) 

          int foo = new int
          new int 1, 2, 3 ,
          new int 1, 2, 3, 4,
          ;

          System.out.println(foo.length); //2
          System.out.println(foo[0].length); //3
          System.out.println(foo[1].length); //4



          Column lengths differ per row. If you're backing some data by a fixed size 2D array, then provide getters to the fixed values in a wrapper class.






          share|improve this answer




















          • Unrelated question about your answer. What is the technique/method called where you put "...;" after the object definition. As a new developer I keep seeing this more and more.
            – user432209
            Oct 22 '10 at 19:27










          • Well, I understand that much :). I just thought there might be a specific name for the technique.
            – user432209
            Oct 22 '10 at 19:53










          • Not sure what the name of it is - object initialization? inline initialization? one of our Java Gurus will know
            – NG.
            Oct 22 '10 at 19:59






          • 5




            Per JLS 10.6 and 15.10, the curly braces part is simply an Array Initializer, while the whole thing starting with new is an Array Creation Expression.
            – ILMTitan
            Oct 22 '10 at 20:30

















          up vote
          11
          down vote













          A 2D array is not a rectangular grid. Or maybe better, there is no such thing as a 2D array in Java.



          import java.util.Arrays;

          public class Main
          public static void main(String args)

          int test;
          test = new int[5];//'2D array'
          for (int i=0;i<test.length;i++)
          test[i] = new int[i];

          System.out.println(Arrays.deepToString(test));

          Object test2;
          test2 = new Object[5];//array of objects
          for (int i=0;i<test2.length;i++)
          test2[i] = new int[i];//array is a object too

          System.out.println(Arrays.deepToString(test2));




          Outputs



          [, [0], [0, 0], [0, 0, 0], [0, 0, 0, 0]]
          [, [0], [0, 0], [0, 0, 0], [0, 0, 0, 0]]


          The arrays test and test2 are (more or less) the same.






          share|improve this answer



























            up vote
            4
            down vote













            Java allows you to create "ragged arrays" where each "row" has different lengths. If you know you have a square array, you can use your code modified to protect against an empty array like this:



            if (row > 0) col = test[0].length;





            share|improve this answer



























              up vote
              3
              down vote













              There's not a cleaner way at the language level because not all multidimensional arrays are rectangular. Sometimes jagged (differing column lengths) arrays are necessary.



              You could easy create your own class to abstract the functionality you need.



              If you aren't limited to arrays, then perhaps some of the various collection classes would work as well, like a Multimap.






              share|improve this answer



























                up vote
                2
                down vote













                If you have this array:



                String example = "Please!", "Thanks", "Hello!", "Hey", "Hi!",
                "Why?", "Where?", "When?", "Who?", "Yes!";


                You can do this:



                example.length;


                = 2



                example[0].length;


                = 2



                example[1].length;


                = 2



                example[0][1].length;


                = 3



                example[1][0].length;


                = 4






                share|improve this answer




















                • I saw a similar answer before but I think it's easier to understand with examples!
                  – Andy
                  Oct 4 '17 at 15:41


















                up vote
                1
                down vote













                Try this following program for 2d array in java:



                public class ArrayTwo2 
                public static void main(String args) throws IOException,NumberFormatException
                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                int a;
                int sum=0;
                a=new int[3][2];
                System.out.println("Enter array with 5 elements");
                for(int i=0;i<a.length;i++)

                for(int j=0;j<a[0].length;j++)

                a[i][j]=Integer.parseInt(br.readLine());


                for(int i=0;i<a.length;i++)

                for(int j=0;j<a[0].length;j++)

                System.out.print(a[i][j]+" ");
                sum=sum+a[i][j];

                System.out.println();
                //System.out.println("Array Sum: "+sum);
                sum=0;








                share|improve this answer





























                  up vote
                  0
                  down vote













                  import java.util.Arrays;

                  public class Main

                  public static void main(String args)


                  double test = 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000;

                  int removeRow = 0, 1, 3, 4, ;

                  double newTest = new double[test.length - removeRow.length][test[0].length];

                  for (int i = 0, j = 0, k = 0; i < test.length; i++)
                  if (j < removeRow.length)
                  if (i == removeRow[j][0])
                  j++;
                  continue;


                  newTest[k][0] = test[i][0];
                  k++;


                  System.out.println(Arrays.deepToString(newTest));







                  share|improve this answer






















                  • Will print [[300.0], [600.0], [700.0], [800.0], [900.0], [1000.0]] convert to ints to lose decimal point. This solution assumes your input array will always be of size ([x][1])
                    – RobynVG
                    Nov 11 at 16:57

















                  up vote
                  -1
                  down vote













                  public class Array_2D 
                  int arr;
                  public Array_2D()
                  Random r=new Random(10);
                  arr = new int[5][10];
                  for(int i=0;i<5;i++)

                  for(int j=0;j<10;j++)

                  arr[i][j]=(int)r.nextInt(10);



                  public void display()

                  for(int i=0;i<5;i++)


                  for(int j=0;j<10;j++)

                  System.out.print(arr[i][j]+" ");

                  System.out.println("");


                  public static void main(String args)
                  Array_2D s=new Array_2D();
                  s.display();







                  share|improve this answer
















                  • 1




                    this is not related to the question
                    – alfonso.kim
                    Apr 20 '17 at 13:12










                  Your Answer






                  StackExchange.ifUsing("editor", function ()
                  StackExchange.using("externalEditor", function ()
                  StackExchange.using("snippets", function ()
                  StackExchange.snippets.init();
                  );
                  );
                  , "code-snippets");

                  StackExchange.ready(function()
                  var channelOptions =
                  tags: "".split(" "),
                  id: "1"
                  ;
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function()
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled)
                  StackExchange.using("snippets", function()
                  createEditor();
                  );

                  else
                  createEditor();

                  );

                  function createEditor()
                  StackExchange.prepareEditor(
                  heartbeatType: 'answer',
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader:
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  ,
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  );



                  );













                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function ()
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4000169%2fgetting-the-array-length-of-a-2d-array-in-java%23new-answer', 'question_page');

                  );

                  Post as a guest















                  Required, but never shown

























                  8 Answers
                  8






                  active

                  oldest

                  votes








                  8 Answers
                  8






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  126
                  down vote



                  accepted










                  Consider



                  public static void main(String args) 

                  int foo = new int
                  new int 1, 2, 3 ,
                  new int 1, 2, 3, 4,
                  ;

                  System.out.println(foo.length); //2
                  System.out.println(foo[0].length); //3
                  System.out.println(foo[1].length); //4



                  Column lengths differ per row. If you're backing some data by a fixed size 2D array, then provide getters to the fixed values in a wrapper class.






                  share|improve this answer




















                  • Unrelated question about your answer. What is the technique/method called where you put "...;" after the object definition. As a new developer I keep seeing this more and more.
                    – user432209
                    Oct 22 '10 at 19:27










                  • Well, I understand that much :). I just thought there might be a specific name for the technique.
                    – user432209
                    Oct 22 '10 at 19:53










                  • Not sure what the name of it is - object initialization? inline initialization? one of our Java Gurus will know
                    – NG.
                    Oct 22 '10 at 19:59






                  • 5




                    Per JLS 10.6 and 15.10, the curly braces part is simply an Array Initializer, while the whole thing starting with new is an Array Creation Expression.
                    – ILMTitan
                    Oct 22 '10 at 20:30














                  up vote
                  126
                  down vote



                  accepted










                  Consider



                  public static void main(String args) 

                  int foo = new int
                  new int 1, 2, 3 ,
                  new int 1, 2, 3, 4,
                  ;

                  System.out.println(foo.length); //2
                  System.out.println(foo[0].length); //3
                  System.out.println(foo[1].length); //4



                  Column lengths differ per row. If you're backing some data by a fixed size 2D array, then provide getters to the fixed values in a wrapper class.






                  share|improve this answer




















                  • Unrelated question about your answer. What is the technique/method called where you put "...;" after the object definition. As a new developer I keep seeing this more and more.
                    – user432209
                    Oct 22 '10 at 19:27










                  • Well, I understand that much :). I just thought there might be a specific name for the technique.
                    – user432209
                    Oct 22 '10 at 19:53










                  • Not sure what the name of it is - object initialization? inline initialization? one of our Java Gurus will know
                    – NG.
                    Oct 22 '10 at 19:59






                  • 5




                    Per JLS 10.6 and 15.10, the curly braces part is simply an Array Initializer, while the whole thing starting with new is an Array Creation Expression.
                    – ILMTitan
                    Oct 22 '10 at 20:30












                  up vote
                  126
                  down vote



                  accepted







                  up vote
                  126
                  down vote



                  accepted






                  Consider



                  public static void main(String args) 

                  int foo = new int
                  new int 1, 2, 3 ,
                  new int 1, 2, 3, 4,
                  ;

                  System.out.println(foo.length); //2
                  System.out.println(foo[0].length); //3
                  System.out.println(foo[1].length); //4



                  Column lengths differ per row. If you're backing some data by a fixed size 2D array, then provide getters to the fixed values in a wrapper class.






                  share|improve this answer












                  Consider



                  public static void main(String args) 

                  int foo = new int
                  new int 1, 2, 3 ,
                  new int 1, 2, 3, 4,
                  ;

                  System.out.println(foo.length); //2
                  System.out.println(foo[0].length); //3
                  System.out.println(foo[1].length); //4



                  Column lengths differ per row. If you're backing some data by a fixed size 2D array, then provide getters to the fixed values in a wrapper class.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Oct 22 '10 at 19:21









                  NG.

                  19.2k44559




                  19.2k44559











                  • Unrelated question about your answer. What is the technique/method called where you put "...;" after the object definition. As a new developer I keep seeing this more and more.
                    – user432209
                    Oct 22 '10 at 19:27










                  • Well, I understand that much :). I just thought there might be a specific name for the technique.
                    – user432209
                    Oct 22 '10 at 19:53










                  • Not sure what the name of it is - object initialization? inline initialization? one of our Java Gurus will know
                    – NG.
                    Oct 22 '10 at 19:59






                  • 5




                    Per JLS 10.6 and 15.10, the curly braces part is simply an Array Initializer, while the whole thing starting with new is an Array Creation Expression.
                    – ILMTitan
                    Oct 22 '10 at 20:30
















                  • Unrelated question about your answer. What is the technique/method called where you put "...;" after the object definition. As a new developer I keep seeing this more and more.
                    – user432209
                    Oct 22 '10 at 19:27










                  • Well, I understand that much :). I just thought there might be a specific name for the technique.
                    – user432209
                    Oct 22 '10 at 19:53










                  • Not sure what the name of it is - object initialization? inline initialization? one of our Java Gurus will know
                    – NG.
                    Oct 22 '10 at 19:59






                  • 5




                    Per JLS 10.6 and 15.10, the curly braces part is simply an Array Initializer, while the whole thing starting with new is an Array Creation Expression.
                    – ILMTitan
                    Oct 22 '10 at 20:30















                  Unrelated question about your answer. What is the technique/method called where you put "...;" after the object definition. As a new developer I keep seeing this more and more.
                  – user432209
                  Oct 22 '10 at 19:27




                  Unrelated question about your answer. What is the technique/method called where you put "...;" after the object definition. As a new developer I keep seeing this more and more.
                  – user432209
                  Oct 22 '10 at 19:27












                  Well, I understand that much :). I just thought there might be a specific name for the technique.
                  – user432209
                  Oct 22 '10 at 19:53




                  Well, I understand that much :). I just thought there might be a specific name for the technique.
                  – user432209
                  Oct 22 '10 at 19:53












                  Not sure what the name of it is - object initialization? inline initialization? one of our Java Gurus will know
                  – NG.
                  Oct 22 '10 at 19:59




                  Not sure what the name of it is - object initialization? inline initialization? one of our Java Gurus will know
                  – NG.
                  Oct 22 '10 at 19:59




                  5




                  5




                  Per JLS 10.6 and 15.10, the curly braces part is simply an Array Initializer, while the whole thing starting with new is an Array Creation Expression.
                  – ILMTitan
                  Oct 22 '10 at 20:30




                  Per JLS 10.6 and 15.10, the curly braces part is simply an Array Initializer, while the whole thing starting with new is an Array Creation Expression.
                  – ILMTitan
                  Oct 22 '10 at 20:30












                  up vote
                  11
                  down vote













                  A 2D array is not a rectangular grid. Or maybe better, there is no such thing as a 2D array in Java.



                  import java.util.Arrays;

                  public class Main
                  public static void main(String args)

                  int test;
                  test = new int[5];//'2D array'
                  for (int i=0;i<test.length;i++)
                  test[i] = new int[i];

                  System.out.println(Arrays.deepToString(test));

                  Object test2;
                  test2 = new Object[5];//array of objects
                  for (int i=0;i<test2.length;i++)
                  test2[i] = new int[i];//array is a object too

                  System.out.println(Arrays.deepToString(test2));




                  Outputs



                  [, [0], [0, 0], [0, 0, 0], [0, 0, 0, 0]]
                  [, [0], [0, 0], [0, 0, 0], [0, 0, 0, 0]]


                  The arrays test and test2 are (more or less) the same.






                  share|improve this answer
























                    up vote
                    11
                    down vote













                    A 2D array is not a rectangular grid. Or maybe better, there is no such thing as a 2D array in Java.



                    import java.util.Arrays;

                    public class Main
                    public static void main(String args)

                    int test;
                    test = new int[5];//'2D array'
                    for (int i=0;i<test.length;i++)
                    test[i] = new int[i];

                    System.out.println(Arrays.deepToString(test));

                    Object test2;
                    test2 = new Object[5];//array of objects
                    for (int i=0;i<test2.length;i++)
                    test2[i] = new int[i];//array is a object too

                    System.out.println(Arrays.deepToString(test2));




                    Outputs



                    [, [0], [0, 0], [0, 0, 0], [0, 0, 0, 0]]
                    [, [0], [0, 0], [0, 0, 0], [0, 0, 0, 0]]


                    The arrays test and test2 are (more or less) the same.






                    share|improve this answer






















                      up vote
                      11
                      down vote










                      up vote
                      11
                      down vote









                      A 2D array is not a rectangular grid. Or maybe better, there is no such thing as a 2D array in Java.



                      import java.util.Arrays;

                      public class Main
                      public static void main(String args)

                      int test;
                      test = new int[5];//'2D array'
                      for (int i=0;i<test.length;i++)
                      test[i] = new int[i];

                      System.out.println(Arrays.deepToString(test));

                      Object test2;
                      test2 = new Object[5];//array of objects
                      for (int i=0;i<test2.length;i++)
                      test2[i] = new int[i];//array is a object too

                      System.out.println(Arrays.deepToString(test2));




                      Outputs



                      [, [0], [0, 0], [0, 0, 0], [0, 0, 0, 0]]
                      [, [0], [0, 0], [0, 0, 0], [0, 0, 0, 0]]


                      The arrays test and test2 are (more or less) the same.






                      share|improve this answer












                      A 2D array is not a rectangular grid. Or maybe better, there is no such thing as a 2D array in Java.



                      import java.util.Arrays;

                      public class Main
                      public static void main(String args)

                      int test;
                      test = new int[5];//'2D array'
                      for (int i=0;i<test.length;i++)
                      test[i] = new int[i];

                      System.out.println(Arrays.deepToString(test));

                      Object test2;
                      test2 = new Object[5];//array of objects
                      for (int i=0;i<test2.length;i++)
                      test2[i] = new int[i];//array is a object too

                      System.out.println(Arrays.deepToString(test2));




                      Outputs



                      [, [0], [0, 0], [0, 0, 0], [0, 0, 0, 0]]
                      [, [0], [0, 0], [0, 0, 0], [0, 0, 0, 0]]


                      The arrays test and test2 are (more or less) the same.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Oct 22 '10 at 19:35









                      Ishtar

                      10.3k11730




                      10.3k11730




















                          up vote
                          4
                          down vote













                          Java allows you to create "ragged arrays" where each "row" has different lengths. If you know you have a square array, you can use your code modified to protect against an empty array like this:



                          if (row > 0) col = test[0].length;





                          share|improve this answer
























                            up vote
                            4
                            down vote













                            Java allows you to create "ragged arrays" where each "row" has different lengths. If you know you have a square array, you can use your code modified to protect against an empty array like this:



                            if (row > 0) col = test[0].length;





                            share|improve this answer






















                              up vote
                              4
                              down vote










                              up vote
                              4
                              down vote









                              Java allows you to create "ragged arrays" where each "row" has different lengths. If you know you have a square array, you can use your code modified to protect against an empty array like this:



                              if (row > 0) col = test[0].length;





                              share|improve this answer












                              Java allows you to create "ragged arrays" where each "row" has different lengths. If you know you have a square array, you can use your code modified to protect against an empty array like this:



                              if (row > 0) col = test[0].length;






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Oct 22 '10 at 19:21









                              robert_x44

                              8,03312235




                              8,03312235




















                                  up vote
                                  3
                                  down vote













                                  There's not a cleaner way at the language level because not all multidimensional arrays are rectangular. Sometimes jagged (differing column lengths) arrays are necessary.



                                  You could easy create your own class to abstract the functionality you need.



                                  If you aren't limited to arrays, then perhaps some of the various collection classes would work as well, like a Multimap.






                                  share|improve this answer
























                                    up vote
                                    3
                                    down vote













                                    There's not a cleaner way at the language level because not all multidimensional arrays are rectangular. Sometimes jagged (differing column lengths) arrays are necessary.



                                    You could easy create your own class to abstract the functionality you need.



                                    If you aren't limited to arrays, then perhaps some of the various collection classes would work as well, like a Multimap.






                                    share|improve this answer






















                                      up vote
                                      3
                                      down vote










                                      up vote
                                      3
                                      down vote









                                      There's not a cleaner way at the language level because not all multidimensional arrays are rectangular. Sometimes jagged (differing column lengths) arrays are necessary.



                                      You could easy create your own class to abstract the functionality you need.



                                      If you aren't limited to arrays, then perhaps some of the various collection classes would work as well, like a Multimap.






                                      share|improve this answer












                                      There's not a cleaner way at the language level because not all multidimensional arrays are rectangular. Sometimes jagged (differing column lengths) arrays are necessary.



                                      You could easy create your own class to abstract the functionality you need.



                                      If you aren't limited to arrays, then perhaps some of the various collection classes would work as well, like a Multimap.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Oct 22 '10 at 19:23









                                      kaliatech

                                      13.5k45370




                                      13.5k45370




















                                          up vote
                                          2
                                          down vote













                                          If you have this array:



                                          String example = "Please!", "Thanks", "Hello!", "Hey", "Hi!",
                                          "Why?", "Where?", "When?", "Who?", "Yes!";


                                          You can do this:



                                          example.length;


                                          = 2



                                          example[0].length;


                                          = 2



                                          example[1].length;


                                          = 2



                                          example[0][1].length;


                                          = 3



                                          example[1][0].length;


                                          = 4






                                          share|improve this answer




















                                          • I saw a similar answer before but I think it's easier to understand with examples!
                                            – Andy
                                            Oct 4 '17 at 15:41















                                          up vote
                                          2
                                          down vote













                                          If you have this array:



                                          String example = "Please!", "Thanks", "Hello!", "Hey", "Hi!",
                                          "Why?", "Where?", "When?", "Who?", "Yes!";


                                          You can do this:



                                          example.length;


                                          = 2



                                          example[0].length;


                                          = 2



                                          example[1].length;


                                          = 2



                                          example[0][1].length;


                                          = 3



                                          example[1][0].length;


                                          = 4






                                          share|improve this answer




















                                          • I saw a similar answer before but I think it's easier to understand with examples!
                                            – Andy
                                            Oct 4 '17 at 15:41













                                          up vote
                                          2
                                          down vote










                                          up vote
                                          2
                                          down vote









                                          If you have this array:



                                          String example = "Please!", "Thanks", "Hello!", "Hey", "Hi!",
                                          "Why?", "Where?", "When?", "Who?", "Yes!";


                                          You can do this:



                                          example.length;


                                          = 2



                                          example[0].length;


                                          = 2



                                          example[1].length;


                                          = 2



                                          example[0][1].length;


                                          = 3



                                          example[1][0].length;


                                          = 4






                                          share|improve this answer












                                          If you have this array:



                                          String example = "Please!", "Thanks", "Hello!", "Hey", "Hi!",
                                          "Why?", "Where?", "When?", "Who?", "Yes!";


                                          You can do this:



                                          example.length;


                                          = 2



                                          example[0].length;


                                          = 2



                                          example[1].length;


                                          = 2



                                          example[0][1].length;


                                          = 3



                                          example[1][0].length;


                                          = 4







                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Oct 3 '17 at 15:15









                                          Andy

                                          156111




                                          156111











                                          • I saw a similar answer before but I think it's easier to understand with examples!
                                            – Andy
                                            Oct 4 '17 at 15:41

















                                          • I saw a similar answer before but I think it's easier to understand with examples!
                                            – Andy
                                            Oct 4 '17 at 15:41
















                                          I saw a similar answer before but I think it's easier to understand with examples!
                                          – Andy
                                          Oct 4 '17 at 15:41





                                          I saw a similar answer before but I think it's easier to understand with examples!
                                          – Andy
                                          Oct 4 '17 at 15:41











                                          up vote
                                          1
                                          down vote













                                          Try this following program for 2d array in java:



                                          public class ArrayTwo2 
                                          public static void main(String args) throws IOException,NumberFormatException
                                          BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                                          int a;
                                          int sum=0;
                                          a=new int[3][2];
                                          System.out.println("Enter array with 5 elements");
                                          for(int i=0;i<a.length;i++)

                                          for(int j=0;j<a[0].length;j++)

                                          a[i][j]=Integer.parseInt(br.readLine());


                                          for(int i=0;i<a.length;i++)

                                          for(int j=0;j<a[0].length;j++)

                                          System.out.print(a[i][j]+" ");
                                          sum=sum+a[i][j];

                                          System.out.println();
                                          //System.out.println("Array Sum: "+sum);
                                          sum=0;








                                          share|improve this answer


























                                            up vote
                                            1
                                            down vote













                                            Try this following program for 2d array in java:



                                            public class ArrayTwo2 
                                            public static void main(String args) throws IOException,NumberFormatException
                                            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                                            int a;
                                            int sum=0;
                                            a=new int[3][2];
                                            System.out.println("Enter array with 5 elements");
                                            for(int i=0;i<a.length;i++)

                                            for(int j=0;j<a[0].length;j++)

                                            a[i][j]=Integer.parseInt(br.readLine());


                                            for(int i=0;i<a.length;i++)

                                            for(int j=0;j<a[0].length;j++)

                                            System.out.print(a[i][j]+" ");
                                            sum=sum+a[i][j];

                                            System.out.println();
                                            //System.out.println("Array Sum: "+sum);
                                            sum=0;








                                            share|improve this answer
























                                              up vote
                                              1
                                              down vote










                                              up vote
                                              1
                                              down vote









                                              Try this following program for 2d array in java:



                                              public class ArrayTwo2 
                                              public static void main(String args) throws IOException,NumberFormatException
                                              BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                                              int a;
                                              int sum=0;
                                              a=new int[3][2];
                                              System.out.println("Enter array with 5 elements");
                                              for(int i=0;i<a.length;i++)

                                              for(int j=0;j<a[0].length;j++)

                                              a[i][j]=Integer.parseInt(br.readLine());


                                              for(int i=0;i<a.length;i++)

                                              for(int j=0;j<a[0].length;j++)

                                              System.out.print(a[i][j]+" ");
                                              sum=sum+a[i][j];

                                              System.out.println();
                                              //System.out.println("Array Sum: "+sum);
                                              sum=0;








                                              share|improve this answer














                                              Try this following program for 2d array in java:



                                              public class ArrayTwo2 
                                              public static void main(String args) throws IOException,NumberFormatException
                                              BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                                              int a;
                                              int sum=0;
                                              a=new int[3][2];
                                              System.out.println("Enter array with 5 elements");
                                              for(int i=0;i<a.length;i++)

                                              for(int j=0;j<a[0].length;j++)

                                              a[i][j]=Integer.parseInt(br.readLine());


                                              for(int i=0;i<a.length;i++)

                                              for(int j=0;j<a[0].length;j++)

                                              System.out.print(a[i][j]+" ");
                                              sum=sum+a[i][j];

                                              System.out.println();
                                              //System.out.println("Array Sum: "+sum);
                                              sum=0;









                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Jan 11 '17 at 6:51









                                              abpatil

                                              8881417




                                              8881417










                                              answered Jan 11 '17 at 5:20









                                              Pampanagouda Karmanchi

                                              111




                                              111




















                                                  up vote
                                                  0
                                                  down vote













                                                  import java.util.Arrays;

                                                  public class Main

                                                  public static void main(String args)


                                                  double test = 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000;

                                                  int removeRow = 0, 1, 3, 4, ;

                                                  double newTest = new double[test.length - removeRow.length][test[0].length];

                                                  for (int i = 0, j = 0, k = 0; i < test.length; i++)
                                                  if (j < removeRow.length)
                                                  if (i == removeRow[j][0])
                                                  j++;
                                                  continue;


                                                  newTest[k][0] = test[i][0];
                                                  k++;


                                                  System.out.println(Arrays.deepToString(newTest));







                                                  share|improve this answer






















                                                  • Will print [[300.0], [600.0], [700.0], [800.0], [900.0], [1000.0]] convert to ints to lose decimal point. This solution assumes your input array will always be of size ([x][1])
                                                    – RobynVG
                                                    Nov 11 at 16:57














                                                  up vote
                                                  0
                                                  down vote













                                                  import java.util.Arrays;

                                                  public class Main

                                                  public static void main(String args)


                                                  double test = 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000;

                                                  int removeRow = 0, 1, 3, 4, ;

                                                  double newTest = new double[test.length - removeRow.length][test[0].length];

                                                  for (int i = 0, j = 0, k = 0; i < test.length; i++)
                                                  if (j < removeRow.length)
                                                  if (i == removeRow[j][0])
                                                  j++;
                                                  continue;


                                                  newTest[k][0] = test[i][0];
                                                  k++;


                                                  System.out.println(Arrays.deepToString(newTest));







                                                  share|improve this answer






















                                                  • Will print [[300.0], [600.0], [700.0], [800.0], [900.0], [1000.0]] convert to ints to lose decimal point. This solution assumes your input array will always be of size ([x][1])
                                                    – RobynVG
                                                    Nov 11 at 16:57












                                                  up vote
                                                  0
                                                  down vote










                                                  up vote
                                                  0
                                                  down vote









                                                  import java.util.Arrays;

                                                  public class Main

                                                  public static void main(String args)


                                                  double test = 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000;

                                                  int removeRow = 0, 1, 3, 4, ;

                                                  double newTest = new double[test.length - removeRow.length][test[0].length];

                                                  for (int i = 0, j = 0, k = 0; i < test.length; i++)
                                                  if (j < removeRow.length)
                                                  if (i == removeRow[j][0])
                                                  j++;
                                                  continue;


                                                  newTest[k][0] = test[i][0];
                                                  k++;


                                                  System.out.println(Arrays.deepToString(newTest));







                                                  share|improve this answer














                                                  import java.util.Arrays;

                                                  public class Main

                                                  public static void main(String args)


                                                  double test = 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000;

                                                  int removeRow = 0, 1, 3, 4, ;

                                                  double newTest = new double[test.length - removeRow.length][test[0].length];

                                                  for (int i = 0, j = 0, k = 0; i < test.length; i++)
                                                  if (j < removeRow.length)
                                                  if (i == removeRow[j][0])
                                                  j++;
                                                  continue;


                                                  newTest[k][0] = test[i][0];
                                                  k++;


                                                  System.out.println(Arrays.deepToString(newTest));








                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited Nov 11 at 17:17









                                                  Axel P

                                                  99621124




                                                  99621124










                                                  answered Nov 11 at 16:54









                                                  RobynVG

                                                  1




                                                  1











                                                  • Will print [[300.0], [600.0], [700.0], [800.0], [900.0], [1000.0]] convert to ints to lose decimal point. This solution assumes your input array will always be of size ([x][1])
                                                    – RobynVG
                                                    Nov 11 at 16:57
















                                                  • Will print [[300.0], [600.0], [700.0], [800.0], [900.0], [1000.0]] convert to ints to lose decimal point. This solution assumes your input array will always be of size ([x][1])
                                                    – RobynVG
                                                    Nov 11 at 16:57















                                                  Will print [[300.0], [600.0], [700.0], [800.0], [900.0], [1000.0]] convert to ints to lose decimal point. This solution assumes your input array will always be of size ([x][1])
                                                  – RobynVG
                                                  Nov 11 at 16:57




                                                  Will print [[300.0], [600.0], [700.0], [800.0], [900.0], [1000.0]] convert to ints to lose decimal point. This solution assumes your input array will always be of size ([x][1])
                                                  – RobynVG
                                                  Nov 11 at 16:57










                                                  up vote
                                                  -1
                                                  down vote













                                                  public class Array_2D 
                                                  int arr;
                                                  public Array_2D()
                                                  Random r=new Random(10);
                                                  arr = new int[5][10];
                                                  for(int i=0;i<5;i++)

                                                  for(int j=0;j<10;j++)

                                                  arr[i][j]=(int)r.nextInt(10);



                                                  public void display()

                                                  for(int i=0;i<5;i++)


                                                  for(int j=0;j<10;j++)

                                                  System.out.print(arr[i][j]+" ");

                                                  System.out.println("");


                                                  public static void main(String args)
                                                  Array_2D s=new Array_2D();
                                                  s.display();







                                                  share|improve this answer
















                                                  • 1




                                                    this is not related to the question
                                                    – alfonso.kim
                                                    Apr 20 '17 at 13:12














                                                  up vote
                                                  -1
                                                  down vote













                                                  public class Array_2D 
                                                  int arr;
                                                  public Array_2D()
                                                  Random r=new Random(10);
                                                  arr = new int[5][10];
                                                  for(int i=0;i<5;i++)

                                                  for(int j=0;j<10;j++)

                                                  arr[i][j]=(int)r.nextInt(10);



                                                  public void display()

                                                  for(int i=0;i<5;i++)


                                                  for(int j=0;j<10;j++)

                                                  System.out.print(arr[i][j]+" ");

                                                  System.out.println("");


                                                  public static void main(String args)
                                                  Array_2D s=new Array_2D();
                                                  s.display();







                                                  share|improve this answer
















                                                  • 1




                                                    this is not related to the question
                                                    – alfonso.kim
                                                    Apr 20 '17 at 13:12












                                                  up vote
                                                  -1
                                                  down vote










                                                  up vote
                                                  -1
                                                  down vote









                                                  public class Array_2D 
                                                  int arr;
                                                  public Array_2D()
                                                  Random r=new Random(10);
                                                  arr = new int[5][10];
                                                  for(int i=0;i<5;i++)

                                                  for(int j=0;j<10;j++)

                                                  arr[i][j]=(int)r.nextInt(10);



                                                  public void display()

                                                  for(int i=0;i<5;i++)


                                                  for(int j=0;j<10;j++)

                                                  System.out.print(arr[i][j]+" ");

                                                  System.out.println("");


                                                  public static void main(String args)
                                                  Array_2D s=new Array_2D();
                                                  s.display();







                                                  share|improve this answer












                                                  public class Array_2D 
                                                  int arr;
                                                  public Array_2D()
                                                  Random r=new Random(10);
                                                  arr = new int[5][10];
                                                  for(int i=0;i<5;i++)

                                                  for(int j=0;j<10;j++)

                                                  arr[i][j]=(int)r.nextInt(10);



                                                  public void display()

                                                  for(int i=0;i<5;i++)


                                                  for(int j=0;j<10;j++)

                                                  System.out.print(arr[i][j]+" ");

                                                  System.out.println("");


                                                  public static void main(String args)
                                                  Array_2D s=new Array_2D();
                                                  s.display();








                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Apr 20 '17 at 13:00









                                                  peter

                                                  1




                                                  1







                                                  • 1




                                                    this is not related to the question
                                                    – alfonso.kim
                                                    Apr 20 '17 at 13:12












                                                  • 1




                                                    this is not related to the question
                                                    – alfonso.kim
                                                    Apr 20 '17 at 13:12







                                                  1




                                                  1




                                                  this is not related to the question
                                                  – alfonso.kim
                                                  Apr 20 '17 at 13:12




                                                  this is not related to the question
                                                  – alfonso.kim
                                                  Apr 20 '17 at 13:12

















                                                  draft saved

                                                  draft discarded
















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid


                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.

                                                  To learn more, see our tips on writing great answers.





                                                  Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                  Please pay close attention to the following guidance:


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid


                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.

                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function ()
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4000169%2fgetting-the-array-length-of-a-2d-array-in-java%23new-answer', 'question_page');

                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  Top Tejano songwriter Luis Silva dead of heart attack at 64

                                                  ReactJS Fetched API data displays live - need Data displayed static

                                                  政党