#why does the void method return filled array?

30 messages ยท Page 1 of 1 (latest)

rich dirge
#

if i try to do the same with rows it does not return the scanner input

shut vortexBOT
#

โŒ› This post has been reserved for your question.

Hey @rich dirge! Please use /close or the Close Post button 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.

shut swallow
#

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)

verbal turtle
shut swallow
# verbal turtle 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.

verbal turtle
#

You're just trusting that they will notice something is returned

shut swallow
#

Modern IDEs will give you a warning if you're ignoring the returned value of a function.

verbal turtle
#

In fact it's just weird when it doesn't have an obvious use of chaining calls

agile surge
#

Why not create a method :

public char[][] initCinemas(int row, int column) {
  char[][] cinemas = new char[row][column];
  //Do ur for here
  return cinemas;
}
shut swallow
#

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 kekw Sorry

verbal turtle
#

Personally I was giving the hint that the advice given is considered weird by others

agile surge
#

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. ๐Ÿ™‚

mortal plover
#

this.row will fix the problem

#

and no need in arguments at all

#

that whole code is mess

verbal turtle
#

May I answer the question, without recommanding weird things? Thanks

verbal turtle
# rich dirge if i try to do the same with rows it does not return the scanner input

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.
agile surge
#

Here is an error ๐Ÿ™‚
U nerver assign the row number from ur scanner ๐Ÿ™‚

verbal turtle
agile surge
#

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();