• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Arrays / MultiDimensional Arrays - example.length

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK I'm a bit confused with arrays at the moment, so say I have a for loop that's along the lines of :



and the "example" part is referring to an array



Does the .length return the value of Row's by default? And how does one get the amount of columns instead?

I was trying to work out how a for loop was working in some code that I'd pinched and got confused.

Here's the full code if any context is wanted :






the section I was looking at specifically was :



I didn't (don't) really understand how the outer for loop runs twice, where as the inner for loop runs six times (3 times on each of the outer for)

cheers

 
Marshal
Posts: 80447
451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which is Row?
Let's have a look at an array of arrays. There ain't no such thing as a multidimensional array in Java, but arrays of arrays are better.Since the 5 arrays in question have different lengths, it should be obvious to you which is being printed. Note one of the arrays is empty with length 0.
For the Arrays class, import java.util.Arrays
 
author & internet detective
Posts: 42109
934
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes example.length returns the size of the first array. example[0].length returns the size of the inner array. (Make sure your array isn't empty before calling this.)

As for the nested loop, the outer loop runs twice. The inner loop runs three times for each run of the outer loop. 2x3 = 6 so the inner loop runs 6 times.
 
Campbell Ritchie
Marshal
Posts: 80447
451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should not use parallel arrays. It is very easy to get the elements of the two out of step. You should have a Synth object with name and price fields, and create a Synth[] array.
 
Mike Philips
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:Yes example.length returns the size of the first array. example[0].length returns the size of the inner array. (Make sure your array isn't empty before calling this.)



I'm trying to picture this in my head...



so in this example there are two arrays - which I have been referring to as Row's (because I've been visualising a table)

each of these arrays / rows is three sections / elements / columns in length? (I'm not sure what the nomenclature is for this...)

I'm not really following you what you mean by the inner array, as far as I can picture they are parallel - what would an inner array be?



As for the nested loop, the outer loop runs twice. The inner loop runs three times for each run of the outer loop. 2x3 = 6 so the inner loop runs 6 times.



How does that happen? Does the outer loop run, then the nested for loop - and outer loop doesn't re trigger until the nested loop has reached the specified value of < 3 ?


Thank you, just trying to bend my head round these thing's a bit...
 
Mike Philips
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You should not use parallel arrays. It is very easy to get the elements of the two out of step. You should have a Synth object with name and price fields, and create a Synth[] array.



I just got to getters and setters last night ;)

So In this case it would be better to create a SynthProperties class at the top of the page - and then include some data for it :



like this?
 
Campbell Ritchie
Marshal
Posts: 80447
451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Philips wrote: . . . so in this example there are two arrays - . . .
each of these arrays / rows is three sections / elements / columns in length? (I'm not sure what the nomenclature is for this...)

Yes. Two arrays with three elements each.

I'm not really following you what you mean by the inner array, as far as I can picture they are parallel - what would an inner array be?

That would mean one of the two arrays inside the larger array.

As for the nested loop, the outer loop runs twice. The inner loop runs three times for each run of the outer loop. 2x3 = 6 so the inner loop runs 6 times.


How does that happen? Does the outer loop run, then the nested for loop - and outer loop doesn't re trigger until the nested loop has reached the specified value of < 3 ? . . . Yes, but don't write 3. Always use length. That allows for arrays any length.You can use nested for‑each loops if you want the arrays to be “read only”.
 
Campbell Ritchie
Marshal
Posts: 80447
451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Philips wrote:. . . it would be better to create a SynthProperties class . . . include some data for it :

like this?

It is just about always better to create a class. Yes, but the fields should be private, initialised via a constructor, and access can be made via getters and maybe also setters. Also a toString method.
 
Mike Philips
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Mike Philips wrote:. . . it would be better to create a SynthProperties class . . . include some data for it :

like this?

It is just about always better to create a class. Yes, but the fields should be private, initialised via a constructor, and access can be made via getters and maybe also setters. Also a toString method.




OK I've had a little go at this and here's what I've made :



So it returns the name / price of the synth, but it doesn't allow the user to enter in that information... I have to enter it all in the console.

I'm not familiar with (anything...) the constructor or the tostring method.


Cheers though.

 
Campbell Ritchie
Marshal
Posts: 80447
451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not come across constructors? Good grief!
Give all your fileds private access.
A constructor has exactly the same name as the class and you must miss out the return type. You set up all the fields into a consistent state in the constructor, or you establish the class' invariants in the constructor. In your case that the synth has a name and a price.If the parameters have the same names as the fields, that is called shadowing, and it must be resolved by using this‑dot
In some cases it is necessary to take “defensive copies” of constructor parameters (also for other methods). It is never necessary for immutable reference types, e.g. String, nor for primitives, e.g. int.
You can perform validation. For example you can throw an Exception if offered a negative price.

You need a toString() method. It can do exactly the same as the get synth info method. Then you can write
 
Mike Philips
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ace cheers Campbell - I get to them soon apparently!



 
Campbell Ritchie
Marshal
Posts: 80447
451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic