• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

2D Array UniqueRow Method

 
Ranch Hand
Posts: 75
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method below goes through each row of the 2D array and checks if all elements of that array appear twice. If it does not, then it prints that row index. The problem with this code is that, it only checks first index of the 2D array, that's it. It should move to second index and start comparing that but it never does that. Why?



Output
 
Recaip Sanli
Ranch Hand
Posts: 75
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note1: I want to use While loop instead of for to cut the cycle if duplicate found.

Note2: This is an assignment so I cannot use other methods, I have to code in simplest way.
 
Recaip Sanli
Ranch Hand
Posts: 75
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Major correction, to my own code and others. Very important concept when it comes to printing 2D arrays. I rather using while when there is a possibility I may not go through each row. My error was that I was not setting inner comparison integer's value to 0 so second turn it was already out of boundary and it was never getting checked again.

correct code below
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no such thing as a 2D array in Java®, only an array of arrays.
Please explain what you mean about each element appearing twice. None of your arrays contains all elements appearing twice. Particularly if you talk about appearing twice and you have a method called uniqueRows. What does the uniqueRows method do? The name is not specific enough. Does it count unique rows, or identify them, or print them?

If that is assigned work, you shou‍ld correct its formatting and style in order to get good marks.
Your arrays have unhelpful names; why should an array be called c?
Make sure to leave empty lines between successive methods; line 17 is difficult to read because of that mistake.
It is not obvious what the variable cmpr means from its name.
Don't declare multiple variables on the same lin (line 19) and always put spaces around binary operators including =.
The fact that you are reinitialising your local variables in line 41 suggests you would have been better with a for loop. You can include the flag as part of the continuation condition:-
for (int i = 0; i < myArray.length && needsSearching; i++) ...
That will keep the numbers' scope within the bounds of the loops, so you don't need to reinitialise them in line 41. You might be able to get the flag in the same scope, but I have never tried the following:-
for (int i = 0, boolean needsSearching = true; i < myArray.length && needsSearching; i++) ...
I do not know whether such code will even compile.

Now, you will need to work out what the algorithm is to find the duplicates you are looking for. You can only do that on a sheet of paper, not on screen.

Edit: correct spelling mistake in underlined word.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic