Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Finding the length of a two dimensional array

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Finding the length of a two dimensional array

    What if I were to use ".length" on a 2d array. What value would it return? Would it return the length of the row, or the length of the column? And for whatever value it would return, how would I make it return the other?

    Hope that wasn't a confusing sentance!


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Finding the length of a two dimensional array

    Ok, well a 2d array is an array of arrays.

    So, when you do a .length call on it, it will return the number of arrays inside of it. Just like when you do a .length call on a 1d array of doubles, it will return the number of doubles.

    If you wanted to return the length of the arrays in the array, it is a little different. The reason it is different is, in theory, it is possible for each array in the array to have a different length. If we are assuming that all of the arrays in the array have the same length, then you would want to say: array[0].length. The reason we say that is because we get the first array with the call array[0] and we get the length of that array by applying the .length call on the first array.

    Tell me if that is confusing.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Finding the length of a two dimensional array

    Quote Originally Posted by aussiemcgr View Post
    Ok, well a 2d array is an array of arrays.

    So, when you do a .length call on it, it will return the number of arrays inside of it. Just like when you do a .length call on a 1d array of doubles, it will return the number of doubles.

    If you wanted to return the length of the arrays in the array, it is a little different. The reason it is different is, in theory, it is possible for each array in the array to have a different length. If we are assuming that all of the arrays in the array have the same length, then you would want to say: array[0].length. The reason we say that is because we get the first array with the call array[0] and we get the length of that array by applying the .length call on the first array.

    Tell me if that is confusing.
    nope thats fine, thanks for the explaination

Similar Threads

  1. finding specified NUMBERS in an array
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 16th, 2010, 10:35 PM
  2. How to write 2 dimensional array of float numbers to binary file?
    By Ghuynh in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 17th, 2010, 04:26 PM
  3. Finding the highest number in an array?
    By halfwaygone in forum Algorithms & Recursion
    Replies: 1
    Last Post: April 17th, 2010, 03:56 PM
  4. 2 dimensional array alternative ???
    By zeeshanmirza in forum Java SE APIs
    Replies: 1
    Last Post: February 23rd, 2010, 06:18 PM
  5. Multi Dimensional Array help NEEDED URGENT
    By bonjovi4u in forum Loops & Control Statements
    Replies: 5
    Last Post: February 13th, 2010, 12:44 AM