• 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Java problem: 2d-Arrays

 
Bartender
Posts: 5021
186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The difference between

and

is that the first method returns a single int, while the second one returns an array of ints.

If you have a 2D array (in java that is an array of arrays, so where each element is an array itself) you can iterate through the rows as Norm showed you (and that is indeed called an enhanced for-loop)

or using a traditional loop:

My idea to solve your assignment is to have a method say

Now, create a 2D array int[][], name it solution, that has my2DArray.length rows. Then, for each row of my2DArray, invoke the method

and the array that this method returns will be the corresponding row of the solution. So the method 'changeLastElementOfAllRows' would look something like:

This way, we do not change our 2D input array, but return a completely new 2D array.

Think about it.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the clarification sir.

Campbell Ritchie wrote:

Adebiyi Itunuayo wrote:. . . Why is it like that, I mean written with the square brackets together with a method. . . .

Because . . . .

 
Adebiyi Itunuayo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for this sir. I appreciate your help. I'll post my solution soon.

Piet Souris wrote:The difference between . . .

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic