Ad
  • Custom User Avatar

    In order to prevent bugs in more complicated situations like this, you should strive not to learn imperative coding style at all. Almost all languages do that kind of work for you out of the box - in Java it's streams. When you start coding like this, you most certainly will produce bugs.

    In this particular case you already produced a bug: what happens if I call this function with an int array of size 0?

  • Default User Avatar

    I usally do code like this as i learn the language. But i hope to figure out other ways as i learn more of java.
    But what do you mean by "dreaded bugs"? It just looks like a simple "brute force" solution to me.
    (btw your missing a "do" before your "it" in your finall sentence)

  • Custom User Avatar

    This is neither "clever" nor a "best practice". I always wonder who rates these solutions.
    All this is, is straight forward error prone and to be avoided imperative code. Used in production this will eventually lead to all of our dreaded bugs.

    DO NOT IT THIS WAY. Never.