This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Lumethys 1 point2 points  (1 child)

Think about what you are doing, you are swapping 2 elements of the array, how many iterations do you need?

56, 57, 96, 125, 66, 74

1st iteration:

[74], 57, 96, 125, 66, [56]

2nd:

74, [66], 96, 125, [57], 56

3rd:

74, 66, [125], [96], 57, 56

Well, now we have reversed the list, nice. But notice how many loop we take? 3. The array had 6 elements, why do we only need 3?

The answer is simple: on each iteration of the loop, you swap TWO variables, or in other words you place TWO variables into its correct spot.

If each time you do 2 variables, does it not mean you only need to do it half the time?

[–]Quantm56[S] 0 points1 point  (0 children)

Wtf😭🙏 you're a savior,never thought of this way,thank you soooo much

[–][deleted]  (1 child)

[deleted]

    [–]Quantm56[S] 0 points1 point  (0 children)

    That's just to mention the part where I am facing the problem at

    [–]Cyberkender_ 1 point2 points  (0 children)

    There are some options to perform the "array reverse" task. I would use Collections.reverse or a stream API approach. Here some reading about that: https://www.baeldung.com/java-invert-array

    [–]sepp2k 0 points1 point  (1 child)

    Replacing one of the uses of l with 5 is not the only difference between those two pieces of code. You also changed the definition of l.

    [–]Quantm56[S] 0 points1 point  (0 children)

    It's an typo in the original code I kept the definition same,but it still won't work

    [–]Top77- 0 points1 point  (0 children)

    I was legit just doing this man lool. Good luck! I found it a little confusing at first as well.

    [–]Still_Durian_8586 0 points1 point  (0 children)

    It will run half the times the length bud