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
epswing
Nov 4, 2003

Soiled Meat

Mulloy posted:

:words:

You really need to post your code rather than referring to your "first class" and your "second class."

Adbot
ADBOT LOVES YOU

epswing
Nov 4, 2003

Soiled Meat
Does he have a really good reason why he isn't using Swing?

epswing
Nov 4, 2003

Soiled Meat
I think you want the Javascript thread...

epswing
Nov 4, 2003

Soiled Meat
I'd probably start by writing insertion sort myself from scratch, using an int[] as input, just to understand what's going on. You can use the algorithm described in http://en.wikipedia.org/wiki/Insertion_sort nearly line-for-line.

code:
import java.util.Arrays;

public class Blah {
	public static void main(String[] args) {
		int[] array = { 2, 6, 5, 1, 3, 2, 5, 2, 3 };
		System.out.println(Arrays.toString(array)); // jumbled
		sort(array);
		System.out.println(Arrays.toString(array)); // should be sorted
	}
	public static void sort(int[] array) {
		// fill this in
	}
}

If you can write that, you'll see that the code you posted above just parts out the inner loop into another function, and it should then be easy to figure out where swaps and insertions occur.

epswing
Nov 4, 2003

Soiled Meat

mister_gosh posted:

Is there a design patterns megathread?

You tell me. http://forums.somethingawful.com/f/search

epswing
Nov 4, 2003

Soiled Meat

Mustach posted:

GregNorc posted:

Our professor just tells us to talk to the TA if we have questions, and our TA deducts points if we ask her questions about the lab, so I'm not really sure where to turn.

Turn to your professor with evidence that your TA is screwing you.

Seconding this. That's majorly hosed. You should be encouraged to ask as many questions as necessary to understand the lesson/exercise.

epswing
Nov 4, 2003

Soiled Meat
So just to clarify, your program is only given e as input, yes?

epswing
Nov 4, 2003

Soiled Meat
Maybe the values he needs to find must all be integers.

epswing
Nov 4, 2003

Soiled Meat
Assuming "lookup" means retrieving a value based on a key, does Map.Entry magically obtain a value without a lookup?

epswing fucked around with this message at 17:39 on Feb 4, 2010

epswing
Nov 4, 2003

Soiled Meat
That was actually an interview question I gave recently, just to weed out the liars. Turns out about half couldn't do it. And about half of the ones that did couldn't do my next question, which was "same thing, recursively."

epswing
Nov 4, 2003

Soiled Meat

fletcher posted:

JFreeChart + data URI scheme = :cool:

This is so you don't have to save the image to a web-accessible folder? You just send the contents as base64 and the browser renders it? Neato.

Guess the downside is you have to generate the image every time, but I suppose that's fine if it changes often enough.

epswing
Nov 4, 2003

Soiled Meat
The way you're thinking about it, you'd create them but wouldn't be able to use them (you wouldn't magically be able to utilize al1, al2, etc). You have to put them somewhere. If the number of lists you want to create is variable, use an ArrayList instead of an array (like Psychorider suggests):

code:
List<ArrayList<String>> listOfLists = new ArrayList<ArrayList<String>>();

for (int i = 0; i < num; i++) {
    listOfLists.add(new ArrayList<String>());
}

listOfLists.get(0).add("Hello"); // add "Hello" to the first ArrayList.

epswing
Nov 4, 2003

Soiled Meat
I remember getting a lot of mileage out of the Cajo Project (wikipedia link) when writing an internal client/server app. I suppose if your goal is to learn RMI, you should continue without the aid of such a framework, but as I recall Cajo made RMI much easier to deal with.

epswing
Nov 4, 2003

Soiled Meat
Regarding java/tomcat web development, is the workflow still the same? Make changes, deploy/reload, refresh browser, repeat...until OutOfMemoryError: PermGen space, in which case shutdown (or even kill -9, if necessary), startup, repeat.

I can hardly believe this issue, which affects everyone in the world who develops for the tomcat container, hasn't been solved yet.

Edit: or has it? I heard tomcat 6.0.24 fixes this (by actively reaching into other libs and nulling references, killing off threads, etc), c/d?

epswing fucked around with this message at 18:48 on Feb 19, 2010

epswing
Nov 4, 2003

Soiled Meat
To "iterate" through a collection means to visit every element in a collection. You can iterate through the ints in an ArrayList<Integer>:
code:
for (int i = 0; i < myList.size(); i++) {
    Integer current = myList.get(i);
    // do something with current
}
Note that there's an easier way to do the same thing:
code:
for (Integer current : myList) {
    // do something with current
}
All collections give you a way to visit every element. There's a method to traverse a collection apart from the two methods above, which is to use an Iterator (which is what method 2 above does behind the scenes for you, I believe):
code:
Iterator<Integer> i = myList.iterator();
while (i.hasNext()) {
    Integer current = i.next();
    // do something with current
}
So you can picture the Iterator i starting out pointing to the first element in an ArrayList. i.hasNext() will be true, and i.next() will return the first element and move to the second, and so on. When you run out of elements, i.hasNext() will be false.

An ArrayList is backed by an array. You've got a linked list, so it's a different data structure underneath, but should be the same to work with. Start by pointing at the first element in the linked list, and i.hasNext() should return true and i.next() should return the first element and try to move to the second, and so on.

epswing fucked around with this message at 03:01 on Mar 4, 2010

epswing
Nov 4, 2003

Soiled Meat
Just a style gripe, but this:

code:
    public boolean hasNext() {
    	boolean retval = true;
    	if (curr.getNext() == null || curr.getData() == null) {
    		retval = false;
    	}
        return retval;
    }
is the same as this:

code:
    public boolean hasNext() {
        return !(curr.getNext() == null || curr.getData() == null)
    }
Edit: vvv Agree with Fly

epswing fucked around with this message at 07:06 on Mar 9, 2010

epswing
Nov 4, 2003

Soiled Meat

yatagan posted:

Jumping on the style train, I prefer this way because I think it's easiest to parse reading someone else's code. Some people might lambast multiple points of return, but the reason that's undesirable is not applicable here.

code:
    public boolean hasNext() {
        if (curr.getNext() == null || curr.getData() == null) {
            return false;
        }
        return true;
    }

Sorry, I fail to see how this offers any readability advantages. You should be able to read simple boolean expressions, so "return A || B" should be very clear. You sound like you'd write something like...

code:
    public boolean isEmpty() {
        if (list.size() == 0)
            return true;
        } else {
            return false;
        }
    }
...which is just painful, and should be return list.size() == 0 of course.

If the expressions were getting particularly complex, you could do something like...

code:
    public boolean hasNext() {
        boolean nextIsNull = curr.getNext() == null,
                dataIsNull = curr.getData() == null;
        return (nextIsNull || dataIsNull);
    }
...but even that is pushing it.

epswing
Nov 4, 2003

Soiled Meat
The code you posted contained a boolean expression with ANDs/ORs/NOTs, so I'm not sure what you're arguing now. I wasn't griping about negations, I was squinting at multiple return statements.

epswing
Nov 4, 2003

Soiled Meat

osama bin diesel posted:

for what is worth I prefer yatagan's way of writing it but would of had a else so it lines up

:3:

Agreed, I can more easily stomach it without the fall-through return statement. And for what it's worth, I'd read the following two statements aloud exactly the same way.

1. if (A || B) { return true; } else { return false; }
2. return (A || B);

epswing
Nov 4, 2003

Soiled Meat
Exporting to jar shouldn't be too hard, eclipse can do that for you, you just specify the file with the main method you want to run. I think it's under File -> Export -> Export to JAR.

Someone asked about RMI recently, if you look back a few pages in this thread you might find some good links.

epswing
Nov 4, 2003

Soiled Meat
Stop using regex to parse xml.

http://forums.somethingawful.com/showthread.php?threadid=2780384&pagenumber=51&perpage=40#post368046496

epswing
Nov 4, 2003

Soiled Meat
code:
String myString = ...
String xpath = ...
Document document = new SAXBuilder().build(new ByteArrayInputStream(myString.getBytes()));
List<Element> elements = XPath.newInstance(xpath).selectNodes(document);
My word, that was a lot of work. I'm going to lie down for a minute. Such complex code makes me weak at the knees. My hands tremble just thinking about it. I only have the strength and sheer willpower to use such magic for huge projects.

It's a one-liner :ssh:

Edit: On a more serious note, I personally find that creating a Pattern, giving it to a Matcher, and then iterating over groups to be particularly annoying considering how easy this is to do in other languages. I'm not sure why people flock to this error-prone method when XML is involved, considering just parsing the XML and selecting exactly what you want takes as much or maybe even less work.

To each his own, I suppose.

epswing fucked around with this message at 21:48 on Apr 6, 2010

epswing
Nov 4, 2003

Soiled Meat

1337JiveTurkey posted:

I'd say that 90% of my stress at work is from people ignoring established solutions to complex problems in favor of their own hacked together solutions

Yes. I'd say this is closer to 60% of my stress at work. Unless I'm doing something that no one else in the world has ever done (hah), I'm a fan of following established solutions aka doing things The Right Way (tm) (r) (c). Software is too complicated, otherwise.

epswing
Nov 4, 2003

Soiled Meat
I'm saying that regexing XML is error-prone.

And I said using Pattern/Matcher is about the same amount of code as just parsing the XML, which you just demonstrated.

I'm not sure how you missed the point, which was "use the right tool for the job."

epswing fucked around with this message at 17:34 on Apr 7, 2010

epswing
Nov 4, 2003

Soiled Meat
Threading: elevator simulator!

epswing
Nov 4, 2003

Soiled Meat
It's a known fact that CNN speeds up Java (tm) (r) (c) programs.

epswing
Nov 4, 2003

Soiled Meat
Not really.

The second one lets you assign the StringBuilder based on arguments given to the constructor though, so maybe when you're constructing a Whatever you'd want to pass in an int to set the initial size of the StringBuilder.

code:
public class Whatever {
    private StringBuilder info = null;

    public Whatever(int capacity) {
        this.info = new StringBuilder(capacity);
    }
}
You wouldn't be able to do that in your first example.

epswing
Nov 4, 2003

Soiled Meat
We do a fair bit of batch processing at work, I was wondering the same thing. We're looking for a framework that will allow us to distribute 'jobs' to waiting nodes. Communication has to be robust and reliable (no duplicate work done, no jobs lost due to exceptions/crashes). We're actually itching to do it ourselves (currently reading about JMS), if we have the time, but if something exists we'd love to evaluate it.

Suggestions?



vvv Sweet, thanks.

epswing fucked around with this message at 05:14 on Apr 27, 2010

epswing
Nov 4, 2003

Soiled Meat
Teehee

code:
if (new ArrayList<String>() {{ add("one"); add("two"); add("three"); }}.contains(x))
Edited following Lysidas' excellent advice :)

code:
if (new HashSet<String>() {{ add("one"); add("two"); add("three"); }}.contains(x))

epswing fucked around with this message at 21:58 on May 4, 2010

epswing
Nov 4, 2003

Soiled Meat
If you didn't notice the "Teehee," this was a comedy 'contains' option.




vvv Edit: No problemo, and actually, thanks for the asymptotic analyzing, it's worse than I thought.

epswing fucked around with this message at 22:02 on May 4, 2010

epswing
Nov 4, 2003

Soiled Meat
If you've never done them, why do you think your opinion that they're a waste of money is worth anything?

I'm not intentionally trying to be a dick, but saying something is a waste of money with zero backup isn't helpful to anyone.

epswing
Nov 4, 2003

Soiled Meat
I have a textfile in my src folder, like jdbc.properties or whitelist.xml, and when I compile it ends up in my bin folder (or more specifically, my war/WEB-INF/classes folder since I'm in tomcat-land). How can I access whitelist.xml?

I thought I'd done this before in a previous life (job), but now I think that was packaging images for JButton icons into a jar, which isn't the same thing. I think I used some System.something call for that, but I can't find it now, and I'm not sure what to search for.

I can't reliably snatch whitelist.xml out of the filesystem, because when I start the tomcat server with tomcat/bin/startup.sh the current working directory is wherever I execute that command, obviously, so I can't get to it relatively, and trying to get to it absolutely is just dumb.

Edit: speling

vvv Edit 2: Gracias!

epswing fucked around with this message at 20:23 on May 5, 2010

epswing
Nov 4, 2003

Soiled Meat
nevermind...

epswing
Nov 4, 2003

Soiled Meat
You could download and take a look at how pircbot does things.

epswing fucked around with this message at 05:35 on May 17, 2010

epswing
Nov 4, 2003

Soiled Meat
This set of video tutorials is linked in the OP, check them out: http://sourceforge.net/projects/eclipsetutorial/files/





Edit: vvv No worries, let us know if they're any good.

epswing fucked around with this message at 20:41 on May 18, 2010

epswing
Nov 4, 2003

Soiled Meat

The OP posted:

Also Bubblegum Wishes recommends this series of videos teaching Java from the ground up using Eclipse.

epswing
Nov 4, 2003

Soiled Meat

yatagan posted:

Here's a better way, remember which was last filled in by the user.

I think I like yatagan's idea more, but another option is to have one "convert" button below each textfield.

vvv Edit: even better

epswing fucked around with this message at 14:38 on May 28, 2010

epswing
Nov 4, 2003

Soiled Meat
mister_gosh said he's got two war files, so two web apps. Isn't MEAT TREAT's suggestion exactly what tomcat/lib is for (ie multiple webapps having access to the same set of jars)?

epswing
Nov 4, 2003

Soiled Meat
Does tomcat override by filename or by what's inside the jar?

Adbot
ADBOT LOVES YOU

epswing
Nov 4, 2003

Soiled Meat
I'm writing about 100,000 lines to an HttpServletResponse:

code:
List<String> lines = ... // 100,000 elements, about 60 chars each, so about 6mb
String newline = System.getProperty("line.separator");

response.setContentType("text/plain; charset=utf-8");
PrintWriter out = response.getWriter();

long start = System.currentTimeMillis();

for (String line : lines) {
  out.write(line);
  out.write(newline);
}

logger.info("time: " + (System.currentTimeMillis() - start) + " ms"); // 10455 ms

out.flush();
out.close();
This takes about 10 seconds, running both the server (tomcat) and client (firefox) locally. Would you expect it to take that long? I thought perhaps 100k calls to out.write was a bit much, so using StringBuilder I concatenated all the elements in the list and did one big out.write(buffer.toString()), and that also took about 10 seconds.

I'm going to start playing with maybe setting the content length in advance, or trying different content types, but would you expect a response of this size to take 10 seconds? I don't think I'm bandwidth-limited, because we're only talking about 6mb / 10s = 600k/s here, and I'd expect to be able to hit a faster speed than that when everything is local.

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