• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

2D Array size

 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
I've got a 2D array which I'm initializing manually. How do I find the number of columns in each row?
Here's my array:
int times[][]={ {5,22,12,30,19,17,22,36,10,22,15,30,8},
{9,45,18,46,7,17,14,50,23,18,25,15,7},
{7,18,7,30,15,25,12,30,10,25,12,20,9},
{11,20,8,19,15,16,14,14,19,18,15},
{22,24,23,17,10,6,19,8,14,9,9,12,8,20,6},
{25,20,22,10,21,12,24,9,30},
{10,45,20,42,7,65,29},
{4,22,14,24,16,19,22,12,20 }};
<h1>(How messy!)</h1>
Anyways, how do I use a 2 level nested for loop to go thru the elements? Is there a 2D equivalent of array.length?
Thanks,
sasikanth
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sasikanth Malladi,
You were in Auburn right?. Nice to meet you here.

for (int i=0;i<times.length;i++){>
System.out.println(times[i].length);
}

------------------

***********************************************
Sun Certified Programmer for Java 2 Platform
***********************************************
 
natarajan meghanathan
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry!! something wrong when i tried to post it!! statements appeared to get mixed eventhough i posted it right

------------------
*********************************************
Sun Certified Programmer for Java 2 Platform
*********************************************

[This message has been edited by Marilyn deQueiroz (edited February 11, 2001).]
 
natarajan meghanathan
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hope this works.. I am typing it again!!
for (int i=0;
i<times.length;i++){>
System.out.println(times[i].length);
}
 
natarajan meghanathan
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no use.. Moderators!! why is this sudden problem?. I am trying to type a simple for loop with i<times.length; it get messed up at that point?..>
 
Sasikanth Malladi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Natarajan!
Yeah, nice meeting you here! Thanks for your reply but my array is an uneven array. Will this still work?
Sasikanth
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by natarajan meghanathan:
no use.. Moderators!! why is this sudden problem?. I am trying to type a simple for loop with i < times.length; it get messed up at that point?..


This forum thinks that the < followed immediately by times.length with no space between is an html code and therefore does not display as you would expect it to. If you put a space after your < , it will display properly. Notice your post above with extra spaces included.
 
natarajan meghanathan
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Meriylon!!.
Yes Sasikanth. The loop will occur for arrays of any type and length.
for (int i=0; i < times.length; i++)
System.out.println(times[i].length);
Did you try this?


------------------

***********************************************
Sun Certified Programmer for Java 2 Platform
***********************************************
reply
    Bookmark Topic Watch Topic
  • New Topic