• Post Reply Bookmark Topic Watch Topic
  • New Topic

Nested For-Loops  RSS feed

 
Kat Rollo
Ranch Hand
Posts: 62
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Nested for-loops always throw me in a loop. >_<

I found a snippet that uses 2 for-loops to check if there is a duplicate element in the array:

Let us say we have:
String[] input = new String[] {"one","two","three","one"}

I am aware it is discouraged for this kind of task,
but for the sake of understanding nested for-loops (which I have been avoiding, sorry ), I am now trying to study it.

I am a bit lost on how the 2 loops "compare each element to all other elements of the array"?
I know the loops iterate 4 times, but I cannot seem to trace what is going on.

Enlightenment is much appreciated. Thanks in advance.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12564
49
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
forget about java, programming, computers, and all that...and just start thinking about what YOU would do, if all you had were paper, pencils, and an eraser. Or you had a big row of containers, each holding <something>, and your task is to see if any of the bins have a duplicate. How would you approach the problem? Even better, think about how you would explain to a 10 year old child how THEY can determine if there are any duplicates.

You might say something like:

take the thing out of the first bucket. Then, go down the rest of buckets (you obviously don't have to compare the first item to itself so start at the second bucket) and compare it with each - as soon as you find a duplicate, you can quit.

Assuming you didn't find a match, take the second item out of its bucket. compare it with the remaining items - it has already been compared to the first item, and you don't have to compare it to itself, so start with the third item, and go down the list. again, if you find a match, you can quit.

Assuming you didn't find a match, take the third item out of its bucket. compare it with the remaining items - it has already been compared to the first and second items, and you don't have to compare it to itself, so start with the fourth item, and go down the list. again, if you find a match, you can quit.
...
Eventually, you will take out the second to last item. Compare it with the last item.

When all that is left is the last item, it has already been compared with every other item, so there's nothing to do - you're done. If you get this far and have not found a match, then there are no matches.


Now see if you can relate the above to the loops you found in the code.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kat Rollo wrote:I know the loops iterate 4 times, but I cannot seem to trace what is going on.

No. The outer loop iterates 4 times. The inner one iterates 4 times for each iteration of the outer loop - ie, 16.

Furthermore, your inner loop contains a basic mistake. What does:
do?

And what did your requirements say (from your docs)?
"comparing each elements to all other elements of array"

I'll leave you to try and work out what's wrong. In this case it's not particularly important, since it will still work, but code should always reflect what you've been asked to do.

If you get really stuck, come back.

HIH

Winston
 
Pallavi Sadit
Ranch Hand
Posts: 50
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kat,
A simple suggestion from my side - print the values input[I] and input[j] as first statement inside the inner loop to see what values are being compared in each run. You will be able to make out how your program is behaving currently and then you can go ahead and change it to the way you think it should behave.
 
Kat Rollo
Ranch Hand
Posts: 62
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To tell you guys honestly, I understood the HashSet method way more easily and quickly than the nested for-loops:

But that means avoiding it again. >_<

@fred:
Thank you very much for this analogy. In itself, I understood it rather than reading the code.

@Winston:
No. The outer loop iterates 4 times. The inner one iterates 4 times for each iteration of the outer loop - ie, 16.

Ah, thanks! So it is 4 x 4 (inner x outer). To avoid confusion, I think I should trace from the inner loops first.

Regarding the if-statement, sorry I can't seem to find the mistake, since it works as expected.
I tried changing the condition to...

...and it stopped working correctly. Might you explain what you wanted to show?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kat Rollo wrote:Might you explain what you wanted to show?

Sure:

The keyword here is "other":
"...comparing each elements to all other elements of array..."does a comparison for every element in the array, including itself, and only then checks whether the indexes are unequal.

On the other hand:doesn't, since the comparison is then done only if i != j which, among other things, will make it faster.

Like I say, in this case it's not particularly important, since the effect is the same; but it could be in others. It's important that your code does exactly what it's been asked to.

HIH

Winston
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A good debugging technique is to use system.out.println to see what your code is actually doing. This is what I do if I have a bug and I think it is in a for loop and it isn't behaving properly or if I want a better understanding of what is actually going on with my code.


Here is an example. Below is a binary search algorithm that I developed. When I was first writing this I had trouble with my loop and if statements. I placed a bunch of system.out.println to know exactly what was happening and after a few trial and errors I had a perfectly working binary search algorithm.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!