• Post Reply Bookmark Topic Watch Topic
  • New Topic

different for loops different results  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys this is related to multi dimensional arrays  or arrays of arrays in a way but at the same time isn't so I decided to make a new topic so my multi dimensional arrays topic didn't go off track

so here is the code



this code does exactly what I want it to do it swaps each number in a random place

2
6
3
5
7
1
8
0
4


the next part is what what confuses me when I put the println statement with array[i][j] in the for loop where I make the swap it prints out some of the numbers multiple times yet when I put the println statement in a separate nested for loop it seems to work fine I honestly can't see the difference between the two I mean shouldn't they both print the same results as the only difference is that  they are in separate for loops

here is the code and it's results



4
6
6
8
4
6
1
0
7


as you can see 4 is printed twice and 6 is printed three times.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
4 is printed twice and 6 is printed three times.

How many times did you execute the code?  Was the output always different?
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was expecting each number to get printed once like in the first example

2
6
3
5
7
1
8
0
4

in a random order but each number should be printed twice when I do it in a separate nested for loop I get the desired output but when I do the swap and try to print each number in the same for loop some numbers get printed twice
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried tracing the values in the array as the loops executed?

One easy way to see the contents of a 2 dim array is to use the Arrays class's deepToString() method:


some numbers get printed twice

It appears the code prints a number and then moves it and then prints it again.  Printing out the contents of array for each loop with the values of i and j will show you what is happening.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
true I think that's what's happening but how come in the first example the numbers don't get printed twice?
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  how come in the first example the numbers don't get printed twice?

The moving of the numbers is finished
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Norm =)
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't write a swap algorithm inside your method like that (lines 11‑13). You will end up writing the code repeatedly, which is a bad idea. Either:-
  • 1: Create a utility class with a swapTwoElementsInArray method, or
  • 2: Consider something like my IntSwapper class.
  • I think in this instance the utility class will work better. You can use an IntSwapper inside the swapping method. Overload that method for different kinds of array.
     
    Campbell Ritchie
    Marshal
    Posts: 56536
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You will have to do a lot of overloading of methods to accept an int[][].
     
    Fred Kleinschmidt
    Bartender
    Posts: 571
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    When you put the print statement inside the inner loop, you should print out im, j, a, and b in addition to array[i][j]. you might then see why you can get the same value printed more than once.

    Then you should print out the array after the nested loops end (as in your first example).
     
    D Nobels
    Greenhorn
    Posts: 15
    1
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Adam,

    Here is what I think... You can confirm it by printing some details about each move (the number moved and the indexes).

    With these loops, you have no guarantee that a number of your array won't be moved twice. Even though you parse through your loops with i and j, the two random indexes (a and b) might move the number to a spot in your array that still has to be covered by the i and j parsing. If that happens you will move the number again when i and j reach it.
    In that case, your print in the loop is just showing each move and not the final result of all the moves.
    So 6 was moved three times and 4 was moved two times.

    Does this make sense?

     
    Campbell Ritchie
    Marshal
    Posts: 56536
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Welcome to the Ranch
    Helpful first post
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!