• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Returning an array

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. I'm currently facing problems with this kind of thing: I need to do different kind of calculations and then return those results in a new array (I've one previous array but it doesn't affect to this?). How should I do this thing. Am I right if I say that I need first create a new array, then put those results to that array and then return it? Ok, if I'm right then there is only one problem, I couldn't find information how to return an array. Does it work then like this:

return myArray[];

This is what my logic says, but I can't test this return-thing yet so I hope that you'll inform me.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by V��r� Tunnus:

This is what my logic says, but I can't test this return-thing yet so I hope that you'll inform me.



Why can't you test it yet? You should test your hypotheses as soon as possible, even if it means writing a trivial class to do the testing. That's the best way to learn.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the way one can return array:

class RArray{
public static void main(String args[])
{
int array1[] = new int [5];
array1 = RArray.return_array();
for(int i=0; i<5 ; i++)
{
System.out.println(array1[i]);
}
}
static int[] return_array()
{
int array2[] = {1,2,3,4,5};
return array2;
}
}

 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

  • The preferred syntax for array variables is "String[] args", not "String args[]".
  • The allocation of "new int [5]" and its assignment to variable array1 is pointless, since array1 is assigned to in the next line.
  • The for loop assumes that the method is returning an array of length (at least?) 5. It's better to use the length atribute.

  • Fixing this yields:
     
    brevity is the soul of wit - shakepeare. Tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic