• Post Reply Bookmark Topic Watch Topic
  • New Topic

Understanding this Java code  RSS feed

 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This method is used to remove the value toRemove from the array. The remaining elements should just be shifted toward the beginning of the array. Since the array will now have one fewer element, the position of the previous last element should just be filled with 0. If there is more than one occurrence of toRemove in the array, only the first occurrence should be removed.






It's hard to pick out one line that confuses me. Could someone please explain what each line of code is doing?

Thank you
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That method has a logical flaw, filling the last element with 0, because
  • 1: 0 might be a valid element.
  • 2: The size of the array doesn't change. Actually array sizes never change.
  • What is going to happen if the last element was specifically added as 0?
    It also has some problems in that it does three things, which might well be factored out to private methods:-
  • 1: It seeks the first instance of a particular number.
  • 2: It moves the remainder of the array to obliterate that value.
  • 3: It adds a 0 to avoid duplicating the last element.
  • Now you have seen the drawbacks of that method, which could quite easily be sorted out by hiding the array inside an instance of something like this, I think you can probably work out what the method does.

    Challenge: Change the return type of the method to int[]; then you can return an array minus the sought number, without adding a 0 at its end.
     
    Norman Radder
    Ranch Hand
    Posts: 146
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Another issue:  The loop continues after the match is found. 
    A couple of solutions:
    Change the for loops ending condition to consider the value of done
    Use break to exit the loop when a match is found.
     
    Campbell Ritchie
    Marshal
    Posts: 56546
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Justin Robbins wrote:. . . It's hard to pick out one line that confuses me. . . .
    You mean it is hard to pick which one line confuses you the most? The loop in lines 7‑11 is badly written. Do tell us where you found that code.
     
    Junilu Lacar
    Sheriff
    Posts: 11493
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:The loop in lines 7‑11 is badly written.

    That's not a loop; the poorly indented code tricked you into thinking it is. Lines 8-11 are part of an if-statement.

    Here's the code indented properly. The code OP posted was missing a closing brace. I added it:

     
    Junilu Lacar
    Sheriff
    Posts: 11493
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This is a very poor implementation and you should just ignore it. It would be a better exercise for you to try to implement the functionality yourself and see if you can come up with something better and more coherent.
     
    Norman Radder
    Ranch Hand
    Posts: 146
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What if the missing } was on line 10?
     
    Junilu Lacar
    Sheriff
    Posts: 11493
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Norman Radder wrote:What if the missing } was on line 10?

    You mean like this?

    I guess that would be better. I haven't tested it but that looks like it would work.
     
    Campbell Ritchie
    Marshal
    Posts: 56546
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Norman Radder wrote:What if the missing } was on line 10?
    I think that is where it is supposed to be. Oh, I see Junilu has already answered that.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!