- Joined
- Aug 2, 2021
>no zero-index arrayYes, unless for some reason to you hate yourself and want to reinvent the wheel. This is a good opportunity to see if you can come up with a fast solution to the problem though. Always worth seeing if you can improve you initial solution.
You're learning don't feel too bad. If you're making progress and gaining new knowledge you're on the right track. It would be worth reading about software development patterns when you get a chance, because there's more than a few.
Understanding that we're working with some very basic code here, this might be of interest to you, you can loop over your users map a lot easier using `entrySet`. Don't treat this as good code, it's just something that immediately stood out to me.
Code:import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.lang.Integer; class User { private String name; public User(String name){ this.name = name; } public String getName(){ return this.name; } } class UserDemo { private static Map<Integer, User> users = new HashMap<>(); public static void main(String[] args) { users.put(1, new User("Mike")); users.put(2, new User("Bob")); users.put(3, new User("Billy")); boolean found = false; for (Entry<Integer, User> entry : users.entrySet()) { if (entry.getValue().getName().equals("Bob")) { found = true; break; } } System.out.println(found); } }
Grab your torches, programmchads