Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
1337JiveTurkey
Feb 17, 2005

Sorry if this is a bit late.

The way I look at DFS versus BFS is the type of data structure that you use to store intermediate nodes to be processed. DFS tends to get written as an explicitly recursive algorithm but you can remove the call stack and replace it with an explicit stack in code iterated over with a loop. BFS doesn't have a nice recursive way to write it out (probably something involving coroutines though) but can be written as an iterative loop with a queue. At this point it's worth mentioning a fun little data structure called the deque that can be a stack or a queue at the same time. A search written with a deque therefore can do either BFS or DFS with remarkably little modification.

code:
Deque<Node> nodes = new ArrayDeque<>();
nodes.addLast(root);
while (!nodes.isEmpty()) {
	Node node = depthFirst? nodes.removeLast() : nodes.removeFirst();
	if (found(node)) {
		return node;
	} else {
		for (Node child : node.getChildren()) {
			// Superstitious programmer hates adding to beginning of array-based data structures
			nodes.addLast(child);
		}
	}
}
return null;
These aren't the only options by any means. If you think about it the fastest way to search is to use some sort of system where you look at the best candidates by some measure first (doesn't need to be perfect) and then try the other nodes. If you're able to define some sort of ordering on your candidates, either directly on them with the Comparable interface or indirectly with the Comparator interface, then you can use a PriorityQueue to sort the nodes. The method names are slightly different but the same structure is there.

code:
PriorityQueue<Node> nodes = new PriorityQueue<>(someComparator);
nodes.add(root);
while (!nodes.isEmpty()) {
	Node node = nodes.poll();
	if (found(node)) {
		return node;
	} else {
		for (Node child : node.getChildren()) {
			nodes.add(child);
		}
	}
}
return null;
I think there's a short hop from this to Dijkstra and A* searches from here but I'm feeling too lazy to think about that at the moment.

Adbot
ADBOT LOVES YOU

1337JiveTurkey
Feb 17, 2005

I think that Effective Java by Joshua Bloch is probably one of the best books in terms of understanding specific Java issues. The same author also wrote Java Puzzlers which cover subtle gotchas. Otherwise for general "Is this good Java?" advice, idiomatic Java doesn't really value terseness and if that thousand line for statement is mostly Spring bullshit, hopefully some day it'll just blend into the background.

1337JiveTurkey
Feb 17, 2005

Also you can use import static Card.Suit.* to just import the card suits directly like you want.

1337JiveTurkey
Feb 17, 2005

F_Shit_Fitzgerald posted:

Is it better than Eclipse? I've been using that because it was the Java IDE my university recommended. I've heard good things about IntelliJ, though.

You can't control a Mars rover with it like Eclipse has been used for but professional developers tend to like IDEA more for its smart autocomplete, whole-project indexing and other features.

1337JiveTurkey
Feb 17, 2005

As far as Java GUIs go, Swing has been updated somewhat so that some of its classes use generics and single method interfaces. That's actually a nice feature that makes it a little bit more nice to use but is long after Java's heyday on the desktop. As far as using it goes, I'm not a big fan of GUI builders and suggest writing well-structured code to actually organize stuff rather than one big autogenerated block.

Something that makes a difference is that there's default models that can be used to back GUI objects but they are mainly useful for demos and other stuff where you don't have real data. Making your own implementations of those interfaces that directly reference your data are much, much easier than trying to cobble together some other classes that manipulate your real data to match the model object.

Adbot
ADBOT LOVES YOU

1337JiveTurkey
Feb 17, 2005

It's not exactly GUI but if you want another sort-of GUI framework that's Java-based look at GWT or Google Web Toolkit. Like all great Google projects it's technologically mindboggling in some ways (at least for the time) and no longer supported by the company. It's making a UI in Java with most everything working the same as on desktop, but then transpiled into optimized javascript and run in the browser. It's very good at making single-page apps but can also do multi-page webapps.

edit: I'm also a big GUI nostalgia fan.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply