• Post Reply Bookmark Topic Watch Topic
  • New Topic

Fruit methods with arrays  RSS feed

 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • 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.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Yes, this makes more sense now. Thank you!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!