File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes passing values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "passing values" Watch "passing values" New topic

passing values

Grant Allen

Joined: Jun 04, 2006
Posts: 8
how can i pass a value from a method into an array, the value = length of array;
marc weber

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Grant Allen:
how can i pass a value from a method into an array, the value = length of array;

Assuming that the method returns a type consistent with what the array holds, you can assign a method's return value to an array like this...

myArray[x] = myMethod();

I'm not sure I understand what you mean by "the value = length of array," but if the array is declared to hold int values, you could certainly do this...

myArray[x] = myArray.length;

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

I don't understand either If you could explain a bit more.

[My Blog]
All roads lead to JavaRanch
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46337
You can have an array:-You can pass the array as an argument to a method.
You can get the method to return an array.
You can pass a number and get a method to produce an array that size.
You can get the length attribute of any array, which is a common-or-garden int number, and pass it as an argument. You name it, you can do it.

You use [] when you are creating the array, or referring to a aprticular member.

If you are referring to a whole array, you miss out the [].

[ June 05, 2006: Message edited by: Campbell Ritchie ]
I agree. Here's the link:
subject: passing values
It's not a secret anymore!