Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Help with Arrays!

 
Greenhorn
Posts: 15
  • 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
 
Ranch Hand
Posts: 360
  • 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
  • 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
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Bix Beiderbecke
Greenhorn
Posts: 15
  • 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
 
Ranch Hand
Posts: 83
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That Worked! Thanks!
 
lowercase baba
Posts: 12905
63
Chrome Java Linux
  • 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 ]
 
Squanch that. And squanch this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic