• Post Reply Bookmark Topic Watch Topic
  • New Topic

need help returning an array, got an error  RSS feed

 
Steven Hofmann
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

So I've got this assignment to create an array of 1000 doubles, and use a method to put random numbers between 1 and 500 in there. I basically created an array called "Array1" and I believe I passed it alright to the method "initialize", but I'm having trouble with the return statement. I followed other examples that say to just put "return arrayname;" but I'm getting an error (Netbeans IDE) that underlines the return statement and says, "required: double, found: double[]". What am I doing wrong?


Thanks,

Steve









 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steven Hofmann wrote:What am I doing wrong?


Well. if you are trying to code the method to return an array, then you are doing nothing wrong.... However, if you look closely, you declared the method to return a double, not a double array.

Henry
 
Steven Hofmann
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Steven Hofmann wrote:What am I doing wrong?


Well. if you are trying to code the method to return an array, then you are doing nothing wrong.... However, if you look closely, you declared the method to return a double, not a double array.

Henry


Oh man, thanks. I knew it needed a second set of eyes! I also fixed a couple other errors and I think I'm on my way. Thanks Henry!
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

In that method, you are receiving a parameter which you are changing. That is sometimes called an output parameter. It is a kind of design I don't like myself, but you don't need to return the array at all. You can pass it as the argument, and the method doesn't have to return it, so you can delete the return statement and change the return type to void.
 
Steven Hofmann
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the welcome! I wish I could've skipped it, but it was a requirement to return the array for the assignment.
 
Alex Hurtt
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steven Hofmann wrote:Thanks for the welcome! I wish I could've skipped it, but it was a requirement to return the array for the assignment.


Did the assignment also require that you pass in the array that you are to populate to the method? You could just create the new array and populate it totally within the method and then return the whole array so sort of like:

double[] myDoubles = myInitMethod();

And myInitMethod() just creates the array within and returns it. I'm kind of in agreement with the prior poster. Arrays in Java are Objects and as such are passed by reference so you CAN modify an object passed to a function without needing to return that object to make the effects visible outside the mutating method.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Hurtt wrote: . . . passed by reference . . .
There is no such thing as pass by reference in Java™.
 
Steven Hofmann
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well part of the criteria was to show that we could pass an array to a method and bring it back, so that's why I had to do it that way. Next week we are doing a vending machine program that uses a bunch of methods for the menu, so I'm worried about that one lol...

Thanks again guys

Steve
 
Alex Hurtt
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Alex Hurtt wrote: . . . passed by reference . . .
There is no such thing as pass by reference in Java™.


I know but this is the beginner forum. From outward appearances, the way it behaves if you pass an object reference, you have passed by reference. I realize under the covers Java treats the object reference itself as if it were a primitive and passes a copy of the reference which makes it in actuality 'by value' even though you are passing a reference to an object. But I don't think the creators of Java actually meant for people to have to think about it that deeply. The end result is that when you pass a reference to an object, no new object has been created. Only a new reference. You now just have 2 identical references to the same object (i.e. 2 pointers to the same exact memory address on the heap if you will). Operations performed on one are visible to the other. So while it may not "technically" be 'by reference,' from the programmer point of view it behaves exactly as if it were by reference.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should not distinguish between what you tell beginners and what you tell experienced people. The beginners require a simple explanation, that is all.
Pass-by-reference, which is supported in C++ and some other languages, and can be mimicked in C by passing a pointer, means you can change the actual pointer or its contents. Pass-by-reference works like this:What you are doing is replacing the actual object with the "rr" in the FooBar class, so it prints "Alex". You can try for a thousand years in Java and never get "Alex" printed. What I have shown is what would happen in pass-by-reference. Try it. It will never work. It will always print "Campbell".

You can however get it to print "Alex" like thisThat is pass-by-value. The foo method is given the location of the reference (its value), and is able to manipulate the state of the object whose reference it is. Assuming that code will compile, and there are public getName and setName methods, that second bit of code will print "Alex".

Pass-by-reference: you can put a new object into the reference. Not supported in Java.
Pass-by-value: you cannot put a difference object into the reference, but you can alter its state or manipulate it, assuming it has "visible" members. Always supported in Java.

You can try the same with primitives and you find the same thing. Pass-by-value only.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I earlier wrote: . . . What you are doing is replacing the actual object with the "rr" in the FooBar class, so it prints "Alex".
Actually, that is confusing. I meant, that is what would happen if you could use pass-by-reference.
You can try for a thousand years in Java and never get "Alex" printed. What I have shown is what would happen in pass-by-reference. Try it. It will never work. It will always print "Campbell". . . .
That bit is correct. You cannot get "Alex" out of that code, unless you change the JVM to support pass-by-reference.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!