Skip to content

basic fizzbuzz using java ArrayList#1

Open
claGiaSW wants to merge 1 commit intomainfrom
part1
Open

basic fizzbuzz using java ArrayList#1
claGiaSW wants to merge 1 commit intomainfrom
part1

Conversation

@claGiaSW
Copy link
Copy Markdown
Owner

@claGiaSW claGiaSW commented Nov 6, 2024

Basic code for a fizzbuzz program using Java.

Playing around with Java's ArrayLists methods and uses.

Copy link
Copy Markdown
Collaborator

@SChandarana SChandarana left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple small things to think about, but a great approach! :D

public class FizzBuzz {
public static void main(String[] args) throws Exception {

ArrayList<String> result = new ArrayList<String>();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice this is a pretty good approach! For performance and memory reasons though, you might want to use a "StringBuilder" https://www.geeksforgeeks.org/stringbuilder-class-in-java-with-examples/

Since you're never doing any special array functions (like sorting, splicing etc)

Comment on lines +16 to +18
if (result.size() == 0) {
result.add(String.valueOf(i));
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also a scenario in which I can introduce my favourite programming operator called the "ternary operator" https://www.w3schools.com/java/java_conditions_shorthand.asp

its perfect for when you want to do "if then else ". In this case it could be within the System.out.println you can check the length of the array/stringBuilder and return i if its 0, or the join if not

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants