aspose file tools*
The moose likes Beginning Java and the fly likes Help with Arrays! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with Arrays!" Watch "Help with Arrays!" New topic
Author

Help with Arrays!

Bix Beiderbecke
Greenhorn

Joined: Jun 28, 2004
Posts: 15
Ok,
Say I have the following: public static int[] makeArray(int n)
and when I call this method from main I want an array to print out from 1 to n.

Makes since?

I really am new to this stuff so a lot of detail would be appreciated, not just the answer

thanks
Pavel Kubal
Ranch Hand

Joined: Mar 13, 2004
Posts: 356
I'm not sure if I understand your problem. Is this what you mean?

Bix Beiderbecke
Greenhorn

Joined: Jun 28, 2004
Posts: 15
no not exactly
say i have this:
public class whatever
{
public static int[] makeArray(int n)
{

}
public static void main(String[] args)
{
System.out.println(makeArray(6));

what I want to print out on the console is an array that has 1 through 6. Or if i change it to say 30 i want it to print an array 1 through 30.

thanks
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
Bix Beiderbecke
Greenhorn

Joined: Jun 28, 2004
Posts: 15
Not exactly: the exact problem I'm trying to figure out is this:
Write a static method called makeArray that takes an integer parameter n and that returns an array of integers containing 1 through n with all even numbers at the front of the list in forwards order and all of the odd numbers at the back of the list in backwards order. For example, a call to makeArray(6) should return [2, 4, 6, 5, 3, 1] while the call to makeArray(13) should return [2, 4, 6, 8, 10, 12, 13, 11, 9, 7, 5, 3, 1]. If n is less than or equal to 0, your static method should return an array of size 0.

public static int[] makeArray(int n)

but once I figure out how to print the array without doing the even odd thing, I can figure the rest out on my own.

thanks
Ben Buchli
Ranch Hand

Joined: Mar 26, 2004
Posts: 83
This is what you need. Be careful, though: The makeArray() returns the reference to the intArray, so that might cause problems sometimes.

Also, the ordering I let you do...

[ June 28, 2004: Message edited by: Ben Buchli ]
Bix Beiderbecke
Greenhorn

Joined: Jun 28, 2004
Posts: 15
That Worked! Thanks!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

i don't see anyplace where it was explicitly said, but... arrays are indexed from 0. what that means is that if you create an array of size 6, your elements are 0-5.

in other words, this:
public static int[] makeArray(int n)
and when I call this method from main I want an array to print out from 1 to n.


isn't correct. you can make the array like that, but the only elements you can print out (because the only elements you have, are 0 through n-1. if you try and display makeArray(n), you'll get an array out of bounds exception.
[ June 29, 2004: Message edited by: fred rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with Arrays!