4 Replies Latest reply: Oct 24, 2007 10:25 PM by 807600 RSS

    How can I find width of an array

    807600
      Hi everyone .I am new to java and trying to figure out how arrays work. I can find the length of an array but I can't find the width. How can I find it ?
      import java.util.*;
      public class practice {
      
      public static void main(String[] args){
      
      int  [][] l = {{1,1}, 
                      {1,1},
                {1,1},
                {1,1}};
       
      System.out.println(l.length); }
      }
      It prints out 4 which is true but I don't know how to get the length which is 2
        • 1. Re: How can I find width of an array
          807600
          arr[0].length
          • 2. Re: How can I find width of an array
            807600
            Here's something that should blow your mind. :)
            public class Test {
                
                public static void main(String args[]){
                     int[][] myArray = {{1,2,3},{1,2},{9,8,7,6,5,4,3,2,1}};
                     for(int i=0;i<myArray.length;i++){
                          for(int n=0;n<myArray.length;n++){
                           System.out.println(i+" "+n+" = "+myArray[i][n]);
                      }
                 }
            }

            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
            • 3. Re: How can I find width of an array
              807600
              thank you very much for the replies.
              • 4. Re: How can I find width of an array
                807600
                javajavajav wrote:
                thank you very much for the replies.
                Make sure you try out what cotton posted, don't just take the first reply.

                Most Java newbies have serious misconceptions about what a "2-D" array is.