• Post Reply Bookmark Topic Watch Topic
  • New Topic

2D Array UniqueRow Method  RSS feed

 
Recaip Sanli
Ranch Hand
Posts: 69
2
  • Mark post as helpful
  • send pies
  • 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: 69
2
  • Mark post as helpful
  • send pies
  • 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: 69
2
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Marshal
Posts: 55785
164
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!