• 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

Fruit methods with arrays

 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For context: I have to describe abstractly what these methods do, and for each variable identify the role it plays.


But my question lies in the first method. What does this part of the code do?

Specifically, what is the a.length for? I read it in notes, but not quite understand it. I am hoping someone can simplify it for me.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do your notes say and what don't you understand? a.length simply refers to the length of the array, i.e., how many elements it can hold.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have seen that same code in a different form before. This does exactly the same thing:
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:What do your notes say and what don't you understand. length simply refers to the length of the array, i.e., how many elements it can hold.



My notes say that too, but why is the a in front of it? Does the a hold a value?
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's standard object dereferencing, same way you'd use a  . between an object reference and a method your calling on that object. Each array will have its own length, so you have to put the name of the array first, then a dot, then the name length, which is an attribute that all Java arrays have. Unfortunately, the designers of Java decided to make length a public attribute/field instead of a method like everything else on an object so not having the () at the end of length tends to throw people off when they see it.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lexi Turgeon wrote:Does the a hold a value?


Look at your method signature again. a is declared as an int[], i.e., a is an array that contains int values.  If you're still confused by this notation, maybe you should go back over your past notes and exercises and really understand that material.

It's the same thing as the args parameter in the main() method signature:

You're familiar with this kind of code, right? Well, the parameter args here is declared as a String[], i.e., args is an array that holds String values.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, this makes more sense now. Thank you!
 
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic