• Post Reply Bookmark Topic Watch Topic
  • New Topic

Taking an array of integers  RSS feed

 
pedro venis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question asks: In Q1D write a method called getLength() that takes an array of integers and returns its length.

No clue how to take an array of integers. Current code:

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pedro venis wrote:No clue how to take an array of integers.


Try googling for java method parameters.
 
Khuzema Dharwala
Ranch Hand
Posts: 61
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pedro, you can use the following modified version of your class.

[Moderator action: full solution removed]

Every array object has a public length instance variable which is used to store the count of array elements I have used that variable in the above code.

Thanks.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khuzema Dharwala wrote:Hi Pedro, you can use the following modified version of your class...

Khuzema, DontBeACodeMill (←click).

I've removed the code you posted because we don't like people to post ready-made solutions - especially in the 'Beginning Java' forum.

I'm sure you had the best of intentions, but it's far better to guide someone to an answer, and let them work it out for themselves.

Thanks.

Winston
 
Khuzema Dharwala
Ranch Hand
Posts: 61
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Winston I understood.
 
pedro venis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Jeff The threads/questions were similar because I'm working on set tasks. But the first one was for parameters which I figured out how to utilize, but this one is for an array of integers where I don't know where it comes from or how to utilize

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pedro venis wrote:@Jeff The threads/questions were similar because I'm working on set tasks. But the first one was for parameters which I figured out how to utilize, but this one is for an array of integers where I don't know where it comes from or how to utilize



Okay, so you understand method parameters? You understand that the format for declaring them is (type1 name1, type2 name2, ....) and the format for passing them when you call a method is (expression_of_type1, expression_of_type2, ..), yes?

But you don't understand arrays? Then the same advice applies, except with a different topic: Try googling for java arrays. Once you understand arrays, then you can declare them as method parameters and pass them just like any other type.
 
pedro venis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
pedro venis wrote:@Jeff The threads/questions were similar because I'm working on set tasks. But the first one was for parameters which I figured out how to utilize, but this one is for an array of integers where I don't know where it comes from or how to utilize



Okay, so you understand method parameters? You understand that the format for declaring them is (type1 name1, type2 name2, ....) and the format for passing them when you call a method is (expression_of_type1, expression_of_type2, ..), yes?

But you don't understand arrays? Then the same advice applies, except with a different topic: Try googling for java arrays. Once you understand arrays, then you can declare them as method parameters and pass them just like any other type.


I figured it out... The explanation was enough. Let me know if my "small" questions will continue to get me redirected to google
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pedro venis wrote:
I figured it out... The explanation was enough.


Good. Glad you got it!

Let me know if my "small" questions will continue to get me redirected to google


Yes, they definitely will. We're happy to help people here, but we expect them to do some research first. For broad, general questions like "What is X?" or "How do I do X in Java?" a Google search--or a search on this site, or a scan of the Table of Contents and Index of your textbook or tutorial--is a good place to start. After that, if something is still not clear, tell us what you read and ask a more detailed question about whatever in particular it is that you find confusing.
 
enombe thierry
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello as regard to your question if i understand you well you mean you have a problem in how to call an array to a method so as to return the lenght? ue i think you should just create an array thuat takes an array as parameter and in the method's body you include a counter that loops thru the array and returnthe value of the counter
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
enombe thierry wrote:Hello as regard to your question if i understand you well you mean you have a problem in how to call an array to a method so as to return the lenght? ue i think you should just create an array thuat takes an array as parameter and in the method's body you include a counter that loops thru the array and returnthe value of the counter


No, that's pointless. Arrays have a length field. This was already pointed out several posts ago.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!