#why does the void method return filled array?
30 messages ยท Page 1 of 1 (latest)
โ This post has been reserved for your question.
Hey @rich dirge! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
So it isn't returning a filled Array, it's instead modifying the array you've supplied.
void functions can mutate objects, which can be an absolute nightmare to deal with, so I would recommend using public static char[][] loadCinema(char[][] cinema)
Err, weird. Does it return the parameter it is given?
Yeah that's the idea. You can still ignore the response (since the underlying object is still mutated) but it's nicer to work with from my experience. Since now your functions can be used in a stream().map() with no changes. The idea is to turn your mutation into a transparent operation, instead of trusting people to read the docs of a void function.
You're just trusting that they will notice something is returned
Modern IDEs will give you a warning if you're ignoring the returned value of a function.
In fact it's just weird when it doesn't have an obvious use of chaining calls
Why not create a method :
public char[][] initCinemas(int row, int column) {
char[][] cinemas = new char[row][column];
//Do ur for here
return cinemas;
}
You can chain the calls in a streaming context,
.map(UtilClass::functionOne)
.map(UtilClass::functionTwo)
Which you can't do with void functions. It's a more functional approach, instead of OoP. Your functions should return the result, instead of embedded that state in an object.
Also yeah we totally are ignoring the question
Sorry
Personally I was giving the hint that the advice given is considered weird by others
There is some problem with ur "row" in "cinemaSizeRow(int row)"
int is primitive ๐
So when u assign ur Scanner.nextInt() in the row, this will not set this value on the "row field" in the main method. ๐
If u want to do that, u need to use Integer class. ๐
Absolutely not
this.row will fix the problem
and no need in arguments at all
that whole code is mess
May I answer the question, without recommanding weird things? Thanks
So the question is why doesn't it work when you try to read the row in a method.
That's because the method that fills the array, changes the content of the parameter given in array.
While the method supposed to "fill" the row, tries to reassign the parameter itself.
It's different, and the second one doesn't work.
You may reassign the content of a parameter.
Like myParam.stuff = newValue;
But not myParam = newValue;
That is because Java cumulates these two things :
- Java is pass-by-value, so values are copied and changing the copy has no effect on the caller
- all objects are always accessed by reference, so passing an object is pass-by-value-of-a-reference. Changing the reference is without effect on the caller, but changing the content of the object the reference points to, does.
Here is an error ๐
U nerver assign the row number from ur scanner ๐
(Honestly I don't understand what you're trying to explain here. The result from scanner.nextInt() is indeed assigned to a variable. It's just that assigning to that variable is essentially useless)
Cause if u look at the main method, u will see :
int rows = 0;
cinemaSizeRows(rows);
In the method, he is trying to assign the Scanner result to the variable. (May be he tought that will assign scanner value to the var rows in the main method.)
When he instanciate the char[][], he use "rows", but "rows" is = 0.
The method for cinemaSeats is good, but not for "rows". ๐
With rows = 0, the method loadCinema is pretty useless cause cinemas.length = 0
So he never go in the for loop. ๐
And never add the "S" for each seat. ๐
public static int cinemaSizeRows() {
System.out.println("Enter the number of rows:");
retun scanner.nextInt();
}
And :
int rows = cinemaSizeRows();