aspose file tools*
The moose likes Java in General and the fly likes Passing Array Elements to Method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Passing Array Elements to Method" Watch "Passing Array Elements to Method" New topic
Author

Passing Array Elements to Method

Alex Kirk
Ranch Hand

Joined: Aug 13, 2001
Posts: 44
I'm trying to pass out each element of an array, as I loop through it, to a method that will create a hash of that element. I'm very confused, however, about how to pass array elements.
I've found in reading a ton of web documentation that arrays are really just references to a location in memory. Thus, it makes sense that, when passing an array element to a method, I'd need to pass a reference to the array (i.e. the array name?) and a reference to the array position. I'm not sure how to do that, though.
FYI, the code I'm working with is as follows:
File path = new File("/home/alex");
String files[] = path.list();
for (int x=0;x<files.length;x++)>
{
String id = makeID(files, x);
...}
public String makeID(String[] names, int y)
{
int code = names[y].hashCode();
if (code <= 0)
{
code = java.lang.Math.abs(code);
code *= 2;
}
code = java.lang.Math.abs(code);
String id = java.lang.Integer.toString(code);
return id;
}
Thanks,
Alex Kirk
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Why not do something like this instead:

And change makeID to take in just a String (not a String[]), and change the code in the method to work on just one String. The for loop above is how you would normally iterate through an array. Of course, by the end of the loop, you are only going to have id equal to the last hashcode from the files array, so you might want to make id an array. Here is how you could set it up:

That would create an array of id's that matches each element in the files array.
Hope this helps,
Jason

[This message has been edited by jason adam (edited August 13, 2001).]
Alex Kirk
Ranch Hand

Joined: Aug 13, 2001
Posts: 44
That's actually a good way to go about it. Funny how you get locked into a way of thinking once you start, huh?
For future reference, though, I would like to know how to pass an array element to a method. What you saw today was just the first piece of a large project I'm embarking on, and I have a feeling it'll come up elsewhere. If you know where I might look for that, I'd appreciate it -- not to say that I'm unhappy with today's solution, though. :-)
Thanks,
Alex
Cherry Sta. Romana
Greenhorn

Joined: Aug 08, 2001
Posts: 18

There is no need to pass the whole array and the index. You just have to pass each individual array element (which happens to be Strings) in your makeID method
Just do it like this:
...
String files[] = path.list();
for (int x=0;x {
String id = makeID(files[x]);// files[x] is a ref. to a String
...}

public String makeID(String name)
{
int code = name.hashCode();
....
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Ah, well, if you need to pass the whole array, then you do like you did. If you declare String[] names, and you want to pass the whole array, just do someMethod( String[] whatever ).
Perfect example, look at your main() method. It always accepts a String[] array, for any extra command line arguements that you might pass. Once you pass it to the method, you can do whatever you want.
Jason
 
jQuery in Action, 2nd edition
 
subject: Passing Array Elements to Method