LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-07-2004, 09:56 AM   #1
pycoucou
Member
 
Registered: Apr 2004
Location: Edinburgh
Posts: 78

Rep: Reputation: 15
Java arrays and dimensions


I am confused... We can get the length of an array thanks to myArray.length. I discovered that per chance. Unfortunately, if I want to use a 2D array, I can't use this trick... Except if someone can explain me I can see if other properties of array exist...

Thanks very much.

Pierre-Yves
 
Old 04-07-2004, 11:37 AM   #2
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
Multidimensional arrays are actually arrays of arrays, so you can do

myArray.length for the "width" of the array

myArray[0].length
myArray[1].length

and so on for the individual depth of each array in the array, which don't have to be the same size if you don't want them to be
 
Old 04-07-2004, 11:47 AM   #3
pycoucou
Member
 
Registered: Apr 2004
Location: Edinburgh
Posts: 78

Original Poster
Rep: Reputation: 15
Thanks... I am un-confused ;-)

int myArrray = {{1,2,3},{4,5,6}};
myArray.length returns 2
but myArray[0].length would return 3...

I should have written it down first !

Cheers.
Pierre-Yves
 
Old 04-07-2004, 11:57 AM   #4
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
That's correct:

Code:
int myArray = { { FIRST ELEMENT HAS INDEX 0 }, { SECOND ELEMENT HAS INDEX 1}}
so myArray[0].length returns 3, because you have three elements in the index [0]. I guess...
 
Old 04-07-2004, 12:06 PM   #5
pycoucou
Member
 
Registered: Apr 2004
Location: Edinburgh
Posts: 78

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by Megaman X
so myArray[0].length returns 3, because you have three elements in the index [0]. I guess...
Cf my example... :
 
Old 04-07-2004, 01:59 PM   #6
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
I'm so dumb... you've already understood it when I posted :S Sorry
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
need video dimensions shanenin Programming 6 11-21-2005 06:54 PM
java + arrays dilberim82 Programming 3 02-12-2005 02:33 PM
java arrays and objects titanium_geek Programming 13 03-07-2004 01:21 PM
terminal dimensions and lilo nautilus_1987 Linux - General 1 10-22-2003 01:20 PM
exciting java arrays problem! Bert Programming 3 11-20-2001 05:11 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration