• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

return array

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi this program is meant to return a new array with each element multiplied by the length.

e.g.
MultiplyByLength([2, 3, 1, 0]) ➞ [8, 12, 4, 0]
MultiplyByLength([4, 1, 1]) ➞ ([12, 3, 3])

I've done this



However the return type is void, but should be int[]
When I tried this for return type of int, I just don't know how to get it to work



What shall I do for this to compile^
 
Saloon Keeper
Posts: 9738
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To get it to compile you can do this
However this doesn't meet one of the requirements which is to return a new array.
 
Carey Brown
Saloon Keeper
Posts: 9738
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
P.S. Method names should must begin with a lower case letter.
 
a syed
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:To get it to compile you can do this
However this doesn't meet one of the requirements which is to return a new array.



Thanks for your response. This is the actual question 'Create a function to multiply all of the values in an array by the amount of values in the given array' so I guess new array is not required.
But how would I print out the values on the console, I've tried this:



But instead i get this I@7c75222b printed on my console
 
Carey Brown
Saloon Keeper
Posts: 9738
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out.println( Arrays.toString( arr ) );
 
a syed
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:System.out.println( Arrays.toString( arr ) );



How would I pass the parameters? I want to call the method MultiplyByLength, in the main method to test
 
Carey Brown
Saloon Keeper
Posts: 9738
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

a syed wrote:But instead i get this I@7c75222b printed on my console


This is what you get when you attempt to print out the reference of an object, and an array is managed as an object internally. To print out an object's value(s) you usually use the object's toString() method if it has one. Built in arrays don't have one. Instead the Arrays utility class provide a number of static methods to perform the task. I highly recommend looking through the JavaDoc for the Arrays class.
 
Carey Brown
Saloon Keeper
Posts: 9738
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

a syed wrote:


Here you try to do everything on one line which would work except for the missing "toString()" call. It might be clearer if you broke it into several lines:
1. create array and assign to variable
2. call method
3. print result
 
a syed
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

a syed wrote:


Here you try to do everything on one line which would work except for the missing "toString()" call. It might be clearer if you broke it into several lines:
1. create array and assign to variable
2. call method
3. print result



Got it thanks a lot!!
 
Carey Brown
Saloon Keeper
Posts: 9738
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post your end results so that others may learn from it. Congratulations.
 
a syed
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Marshal
Posts: 76862
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's the 2020s now. Use a Stream!Let's explain the lines, backwards.
Line 9 collects the elements of the preceding IntStream with its toArray() method. That directly creates an int[] containing all the elements of the original IntStream. That array is passed back to the keyword return.
Line 8 uses the map() method which creates a new IntStream handling the same number of elements as its predecessor. You will find that map() takes an IntUnaryOperator reference as its parameter. That is a Functional Interface, which means it has exactly one method you absolutely must implement: applyAsInt(). If you look in the Java™ Tutorials, you can find out how to declare an object of that interface simply by providing a λ expression. The left part before the -> is the input (method parameter) which I am calling i, and the output, what is returned, is on the right of the ->, i.e. i * original.length. Most Stream operations should avoid sied‑effects like the plague, but that isn't a side‑effect. I am not altering anything outside the stream, only taking information from the original array.
Line 7 starts the whole process by making the first IntStream with the Arrays#stream() method; that overloading takes an int[] and returns an IntStream handling every element in the array. The whole process being finished, the resultant array is passed back to return, as I said earlier.

[edit]Lots of spelling corrections.
 
I will open the floodgates of his own worst nightmare! All in a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic