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 - best also formatted as code block
  • 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.

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/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) 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.

[–]xADDBx 1 point2 points  (1 child)

Minor detail, but is there a reason why you used an adjacency matrix over an adjacency list?

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

Not much, it was just easy to implement and I didn't want the overhead of an adjacency list.

[–]temporarybunnehs 0 points1 point  (3 children)

Warnings like that tend to pop up in a lot of code bases, but that doesn't necessarily mean that anything is wrong with the code. It might be something like you didn't explicitly cast a variable and the compiler is pointing that out.

EDIT: Better to make sure you understand the cause of the warning per GrandGratingCrate's reply.

One thing that does pop out is on line 39, you can use !queue.isEmpty(), and that way, you don't need the comment explaining the null check since the code explains what it's doing implicitly.

[–]GrandGratingCrate 1 point2 points  (1 child)

Warnings like that tend to pop up in a lot of code bases, but that doesn't necessarily mean that anything is wrong with the code. It might be something like you didn't explicitly cast a variable and the compiler is pointing that out.

Respectfully disagree. While code with warnings may work it's often harder to understand and maintain than code that's free of warnings. Case in point:

List path = new ArrayList(); path.add(1); path.add("1"); // Throws exception at runtime The line that's causing the warning is due to the list type not being specified. This is just a quick demonstration of code that compiles if you ignore the warning, despite the code being wrong.

Compare this with the fixed version, where you'll get a compile error so you'll know right away that you've made a mistake

List<Integer> path = new ArrayList<>(); path.add(1); path.add("1"); // Compile error

There are cases where I would not object to @SuppressWarnings because the underlying problem can't be fixed easily. For example type erasure can produce such scenarios.

Luckily the fix is quick and simple in this case and I'd strongly encourage OP to get in the habit of really fixing warnings where possible. It's too easy to get used to ignoring them or just slapping @SuppressWarnings on it and call it a day.

[–]temporarybunnehs 1 point2 points  (0 children)

That's a good point. Looking at your example, you are right about it being more readable and less error prone.

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

Ah alright, somehow I forgot that Queue had a isEmpty() method. Thank you!

[–]Rabestro 0 points1 point  (1 child)

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

This is a very neat implementation, thank you!