Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2008

Tip: Looking for answers? Try searching our database.

Simple array question

Thread view: 
Hakan - 14 May 2008 15:06 GMT
How do I find the number of rows in a multi dimensional array? The
number of columns can quite easily be found with A[0].length, but Java
unlike Matlab does not allow an expression like A[][0].length . I
suppose that there is a way around it.

Regards.

Signature

Newsoffice.de - Die Onlinesoftware zum Lesen und Schreiben im Usenet
Die Signatur läßt sich nach Belieben anpassen ;-)

Jussi Piitulainen - 14 May 2008 15:13 GMT
> How do I find the number of rows in a multi dimensional array? The
> number of columns can quite easily be found with A[0].length, but
> Java unlike Matlab does not allow an expression like A[][0].length

There are A.length rows, if I understand you correctly.
Hakan - 15 May 2008 13:34 GMT
>> How do I find the number of rows in a multi dimensional array? The
>> number of columns can quite easily be found with A[0].length, but
>> Java unlike Matlab does not allow an expression like A[][0].length

> There are A.length rows, if I understand you correctly.

Yup, that works. In response to the rest of the discussion, the multi
dimensional array was ensured to have columns of equal length prior to
the call. Thank you for your help.

Signature

Newsoffice.de - Die Onlinesoftware zum Lesen und Schreiben im Usenet
Die Signatur läßt sich nach Belieben anpassen ;-)

Lew - 15 May 2008 13:58 GMT
>>> How do I find the number of rows in a multi dimensional array? The
>>> number of columns can quite easily be found with A[0].length, but
[quoted text clipped - 5 lines]
> dimensional array was ensured to have columns of equal length prior to
> the call. Thank you for your help.

The reason it works is that A (which, as Roedy pointed out, is misnamed by
starting with an upper-case letter) does not have "rows" at all.

If you think correctly of what a Java array is, then the answer becomes
obvious.  Arrays are single-dimensional only - your /a/ is an array of arrays.
 Of course a.length represents how many of those it has.  Since you
*interpret* an element of /a/ as a "row", then it immediately follows that
a.length represents the number of rows.

Signature

Lew

Patrick - 14 May 2008 15:23 GMT
Le 14/05/2008 16:06, Hakan a écrit :

> How do I find the number of rows in a multi dimensional array? The
> number of columns can quite easily be found with A[0].length

Beware, there are no real multi-dimensional arrays in Java like in Ada.
A[0].length may be different from A[1].length.

Signature

Patrick
http://iang.info

Stefan Ram - 14 May 2008 16:10 GMT
>Beware, there are no real multi-dimensional arrays in Java like in Ada.

 I also think so, but still the JLS mentions the term
 »multidimensional array« as if there was such a thing:

     »A clone of a multidimensional array is shallow«

http://java.sun.com/docs/books/jls/third_edition/html/arrays.html

 The example program following this statement then contains:

int ia[][] = { { 1 , 2}, null };

                             ~~

 There are different ways to implement a two-dimensional array.
 An array of arrays is only one possibility. Another would be a
 one-dimensional array with an index calculated from the two
 indices of the two-dimensional array. So the means to obtain
 the two dimensions depend on the way the multidimensional
 array is implemented.
Lew - 15 May 2008 05:51 GMT
Patrick writes:
>> Beware, there are no real multi-dimensional arrays in Java like in Ada.

>   I also think so, but still the JLS mentions the term
>   »multidimensional array« as if there was such a thing:
[quoted text clipped - 15 lines]
>   the two dimensions depend on the way the multidimensional
>   array is implemented.

The JLS uses the term "multidimensional array" to mean "array of arrays", Java
/ C.* style.  They do not mean it in the sense used here, to mean a "real"
multi-D array.  There are lots of places in the JLS where they are a little
less formal in the interest of exposition.

Signature

Lew

Roedy Green - 14 May 2008 16:18 GMT
> How do I find the number of rows in a multi dimensional array? The
>number of columns can quite easily be found with A[0].length, but Java
>unlike Matlab does not allow an expression like A[][0].length . I
>suppose that there is a way around it.

It is a meaningless question.  Every row can have a different length.
Java "matrices" might be triangular.

So try a[0][0].length

Note the lower case a.  A is a class.
See http://mindprod.com/jgloss/codingconventions.html

see http://mindprod.com/jgloss/array.html
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.