Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to return an array from a method  RSS feed

 
James Sea
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having trouble with my Java project on arrays, I was wondering if anyone could help me understand how to return an array from a method
 
Carey Brown
Bartender
Posts: 2982
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Paul Clapham
Sheriff
Posts: 22472
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch!

Don't hesitate to post back if you have other questions, too.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there are about three things you need to do:

1) declare your method as returning an array (and what type of array that is). In Carey's example, that's this part:

public String[] myMethod()

That line is saying (among other things) that the method will return a String array.

2) create the array you want to return. It doesn't matter HOW you create it, or where it comes from, but you have to have it (more precisely, a reference to it. Carey did that here:

String[] ary = new String[4];

"ary" is a reference that points to an array of Strings with four elements.

3) finally, you have to return that array reference. That's as easy as:

return ary;



So give it a try. If you still have issues, post your code and we can review it with you.
 
Trung Hieu Hoang
Greenhorn
Posts: 12
AngularJS Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My experience is to think of array variable as reference to an object of type "array"
 
James Sea
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys so much ! Im loving this site already ! If I happen to run into another problem ill post the code so it can be reviewed and I can be guided in the right direction
 
James Sea
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having trouble again here is my code for adding a Student object to the collection. If the collection is full, calls the private method increaseSize() to increase the size to (2 * the original size ) + 1.



does this look correct
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would recommend that you pick up the Head First Java book and go through the first couple of chapters!
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code looks wrong in a couple of ways, see below:



Maybe you meant to declare the method like this:

  public void addToCollection(Student student)

but then where does "s" come from?
 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
…but OP is not adding anything to a Collection. He is populating an array in that method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!