File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with Arrays!

 
Bix Beiderbecke
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 360
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if I understand your problem. Is this what you mean?

 
Bix Beiderbecke
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Bix Beiderbecke
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That Worked! Thanks!
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic