• Post Reply Bookmark Topic Watch Topic
  • New Topic

Elements in array  RSS feed

 
Shal Lango
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assume  you are given  an int  variable  named  nElements and a 2-dimensional array  that has been created and assigned  to a2d. Write one or more statements  that assign  to nElements the total number of elements  that could be stored  in the entire 2-dimensional array.

I know I have to multiply the arrays but I'm not sure how

 
Carey Brown
Saloon Keeper
Posts: 3327
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2D arrays in Java are really arrays of arrays where each row might be a different length. To get the number of rows you can use a2d.length and to get the length of each row you can use a2d[row].length.
 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question is confusing and not clear.

Copy/Paste it exactly as it appears. If it is originally written in different language, then try to rephrase all that.
Knowing that Java has an array of arrays concept, there can be jagged arrays, which wouldn't be that straight forward to calculate - but that's too advance here.
 
Shal Lango
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Question is confusing and not clear.

Copy/Paste it exactly as it appears. If it is originally written in different language, then try to rephrase all that.
Knowing that Java has an array of arrays concept, there can be jagged arrays, which wouldn't be that straight forward to calculate - but that's too advance here.



That's exactly what the question says. There's no other info :/
Its not supposed to have jagged arrays.
 
Carey Brown
Saloon Keeper
Posts: 3327
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, if all the rows are the same length (ie not jagged) then you should be able to multiply the length in rows by the number of elements in one of your rows. See my first post for a hint at the syntax.

BTW, if you did write it to handle the jagged case then it would automatically work for the non-jagged case. It's not too difficult to do but involves a loop.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:It's not too difficult to do but involves a loop.

@OP: It can literally be just 3 lines of code, the first one being to initialize nElements to 0.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There must be a way to do that with a Stream.
It is easy enough to get a Stream from an array with the Arrays#stream() method, which will take any sort of array except boolean[] byte[] float[] short[] and char[]. So if you have a Foo[][] that method will produce a Stream<Foo[]>, which means that every object handled by the Stream is a Foo[] instance. Now you can change that instance to an int by finding its length field; you can write a λ matching an array element (let's call it a) to its length field a.length, as in line 3. That can create an IntStream with its mapToInt() method. An IntStream is a kind of Stream which handles sequential ints, and you can get the total simply with its sum() method. And Bob's your uncle.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And Bob's your uncle.

Isn't he everyone's Uncle?
 
Carey Brown
Saloon Keeper
Posts: 3327
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:

Challenge: Can you come up with a stream approach for a 3D array? Would it involve flatMap?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Challenge: Can you come up with a stream approach for a 3D array? Would it involve flatMap?

Is that a trick question? There are no 3D arrays in Java. There are only arrays of arrays of arrays... 
 
Carey Brown
Saloon Keeper
Posts: 3327
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Carey Brown wrote:Challenge: Can you come up with a stream approach for a 3D array? Would it involve flatMap?

Is that a trick question? There are no 3D arrays in Java. There are only arrays of arrays of arrays... 
Yeah, but you knew what I meant.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No need for a flatmap. but this works:

But probably simpler is this scheme:

 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:. . . Isn't he everyone's Uncle?
Not that Bob
 
Shal Lango
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it!



Thank you all
 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wait wait wait wait wait.

What has happened with braces around for loops bodies?
And what has happened with indentation?

Fix them before submit assignment, so you wouldn't lose marks.
 
Shal Lango
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Wait wait wait wait wait.

What has happened with braces around for loops bodies?
And what has happened with indentation?

Fix them before submit assignment, so you wouldn't lose marks.


oh my bad it didn't paste properly. yeah it worked
 
Carey Brown
Saloon Keeper
Posts: 3327
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shal Lango wrote:Got it!
Thank you all

Excellent work! You could also eliminate line 3 (your 'j' loop) if you increase nElements by something other than one. That would get it down to 3 lines of code as mentioned in Junilu's earlier post.
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If anyone's interested, I made a version that counts the number of elements in a stream, collection or array of any dimension. It's not pretty, and it doesn't support iterables in general, nor primitive arrays, but I thought it was a nice challenge.
 
Shal Lango
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow that's too complicated for me haha but really impressive! Good work
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!