|
Brain Candy posted:Unless you are using large lists (say 20000+), an ArrayList will be faster and allows constant time random access. If you only need to use the Deque interface, why not just use an ArrayDeque? Hell, if you only use List for iteration, just use Collection. The list I work on are often on the order of a couple million at a time. For the most part, either works just as well and you shouldn't really care which one you are using. If I'm needing random access, there are much better structures that I can both iterate through and give me random access to the data inside (Map). The place where it will matter is when you are performance tuning in which case access patterns and benchmarking must be considered. You do bring a good point about Collection though. In general, always try to use the most general data structure you can for declaring your variables. Why declare an ArrayList list = new ArrayList() when if all you need is the List interface you can declare List list = new ArrayList(). If you later decide you need a different List, then you can just change the instantiation.
|
# ? Nov 11, 2009 06:05 |
|
|
# ? May 19, 2024 23:47 |
|
Hmm, that is a little unclear. Second time's the charm : if you only use the List interface for .iterator() you could just store/pass/return things as Collections or Iterables instead. edit: ^^^ ha, apparently I wasn't completely unintelligible
|
# ? Nov 11, 2009 06:13 |
|
The problem I see with linked list is that for any non-trivially sized listcode:
You can tell that is a design problem by the fact that the Collections API has instanceof checks for a RandomAccess marker interface buried in its guts.
|
# ? Nov 11, 2009 06:31 |
|
Brain Candy posted:Hmm, that is a little unclear. Second time's the charm : if you only use the List interface for .iterator() you could just store/pass/return things as Collections or Iterables instead.
|
# ? Nov 11, 2009 06:44 |
|
Brain Candy posted:The problem I see with linked list is that for any non-trivially sized list Insertions can cost just as bad on an ArrayList. This is why you use Iterators though. LinkedList make a lot of sense if your insertions are on heads or tails. Like I said before, it really all depends. We are splitting hairs here. HFX fucked around with this message at 07:29 on Nov 11, 2009 |
# ? Nov 11, 2009 07:24 |
|
Linked list makes a lot of sense if your insertions are heads or tails, but hey it's a Deque. What I'm saying is that a linked list, despite its name, is crappy as a List.
|
# ? Nov 11, 2009 17:20 |
|
Since we're talking 'bout Lists, which one could be considered best in performance when passed to Collections.sort(list, comparator)? I know there isn't a one clear answer, but I know that some implementations most likely are worse than others for various technical reasons I haven't yet familiarized myself with. To be specific, the reason I'm asking is that I need to sort a list containing about several hundred objects. The related comparison math is rather heavy and overcomplicated and while I can cache parts of it manually, it can't be cached fully because the comparison object alters the the result itself significantly; it's not just "is x bigger than y?" comparison.
|
# ? Nov 15, 2009 12:18 |
|
You want a data structure with O(1) time for element retrieval and assignment (for arbitrary indexes), which would be an ArrayList.
|
# ? Nov 15, 2009 17:53 |
|
Parantumaton posted:Since we're talking 'bout Lists, which one could be considered best in performance when passed to Collections.sort(list, comparator)? I know there isn't a one clear answer, but I know that some implementations most likely are worse than others for various technical reasons I haven't yet familiarized myself with. quote:The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n log(n) performance. The specified list must be modifiable, but need not be resizable. This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element from the corresponding position in the array. This avoids the n2 log(n) performance that would result from attempting to sort a linked list in place. In other words, it doesn't matter as far as algorithic complexity goes, but you'll pretty much always want to prefer an array list unless you actually need constant time middle insertion/removal or splicing (I'm not sure if Java's LinkedList even supports this)
|
# ? Nov 15, 2009 21:46 |
|
Quick question, how do I change the header of an HTTP GET request in a Java program (using something like HttpURLConnection) to accept JSON data?
|
# ? Nov 16, 2009 01:25 |
|
Parantumaton posted:To be specific, the reason I'm asking is that I need to sort a list containing about several hundred objects. The related comparison math is rather heavy and overcomplicated and while I can cache parts of it manually, it can't be cached fully because the comparison object alters the the result itself significantly; it's not just "is x bigger than y?" comparison.
|
# ? Nov 16, 2009 02:00 |
|
fge posted:Quick question, how do I change the header of an HTTP GET request in a Java program (using something like HttpURLConnection) to accept JSON data? See the inherited (agnostic) addRequestProperty() to add a HTTP header. I've always used commons-httpclient though.
|
# ? Nov 16, 2009 04:06 |
|
covener posted:See the inherited (agnostic) addRequestProperty() to add a HTTP header. I've always used commons-httpclient though. Awesome addRequestProperty works perfectly, thanks!
|
# ? Nov 16, 2009 04:28 |
|
Fly posted:Do you mean the comparator, when you write "the comparison object"? Are you saying the act of compare(o1, o2) modified o1 or o2 in a way other than, say, lazy-loading contained objects from persistence? Yes, it's part of a really finicky and code-smellish set relevance comparison.
|
# ? Nov 16, 2009 14:47 |
|
Parantumaton posted:Yes, it's part of a really finicky and code-smellish set relevance comparison.
|
# ? Nov 17, 2009 00:36 |
|
Cannot javac/compile from command line in user folder. Folder where I'm writing stuff: C:\user\username\java Javac is installed in folder: C:\program files\java\jdk1.6.0_17\bin I'm in the command line, and have this: code:
code:
code:
|
# ? Nov 17, 2009 19:47 |
|
wintermuteCF posted:
If you change your classpath only to include the QTJava.zip file, does that help?
|
# ? Nov 17, 2009 20:13 |
|
Fly posted:Javac generally does not need the jdk jar files in it's classpath, and I don't think setting the classpath to the directory where the jar files are will help at all. Actually fixed it with some help from a friend at work. Just added ;. to the end of the classpath entry and it works now.
|
# ? Nov 17, 2009 20:25 |
|
Compiling from the command line is loving annoying. I suggest you use an IDE or learn to use Ant/Maven to do your compiling for you.
|
# ? Nov 17, 2009 21:41 |
|
MEAT TREAT posted:Compiling from the command line is loving annoying. I suggest you use an IDE or learn to use Ant/Maven to do your compiling for you.
|
# ? Nov 18, 2009 00:00 |
|
LOL no, but it only gets worse as your project grows in size and I feel it's valuable to learn it sooner rather than when you really need it and are pressed for time.
|
# ? Nov 18, 2009 01:14 |
|
Maven, however, doesn't improve your build quality at all, it's like sticking ice picks into your eyes. You're better off making reusable Ant scripts and using Ivy for dependency management.
|
# ? Nov 18, 2009 13:12 |
|
Parantumaton posted:Maven, however, doesn't improve your build quality at all, it's like sticking ice picks into your eyes. You're better off making reusable Ant scripts and using Ivy for dependency management. Everyone seems to be torn (well not torn on opposite ends) on this , myself included. My old job moved from custom Ant scripts to Maven. Never really noticed any major issues except that we had several POMs per project and if one was out of sync, build would fail. Over all I like Maven and I like that Maven is pretty simple to setup and use from the start, but some things still trip me up and online help / documentation doesn't seem to cover it. My main issue with Maven: it's either all or nothing with including dependencies. I can't just add what's needed. This is especially frustrating since I work with OSGI and it's easier to include dependent jars in the build jar instead of going through the hassle of Bundle-ing them or hacking them to pieces ( had to do this with Apache POI) but that's another thread. geeves fucked around with this message at 06:15 on Nov 19, 2009 |
# ? Nov 19, 2009 06:12 |
|
Actually, based on my last post I have a question: Is it bad form to hack apart and repackage Jar files, especially those from Maven repos? I work with OSGI, so it's a bit of a pain and here's why: We need Apache POI for dealing with Excel documents (we dont' need POI, but it's super easy to work with and we're only reading before passing data onto the JCR). But when loading into OSGI we need 2 jars: poi-3.x-FINAL and poi-ooxml-3.x-FINAL Basically to instantiate the code it's: Workbook wb = WorkbookFactory.create(InputStream in); Well, WorkbookFactory is in poi-ooxml and Workbook is in poi-3.x yet they're in the same location / package (org.apache.poi.ss.usermodel) OSGI will only load the classes of the package of the jar that's loaded first. So if I load poi-3.x first WorkbookFactory is not seen (even if it's in the MANIFEST) or if I load poi-ooxml first, Workbook (and other classes in poi-3.x) won't be seen or available. I get this as a fail-safe. To get POI to work, I had to extract the jars, move WorkbookFactory into poi instead of poi-ooxml (it was the only class in that package in poi-ooxml) then re-jar the classes, create bundles using Bnd (http://www.aqute.biz/Code/Bnd) and upload them to my OSGI manager. It works. I couldn't be happier. But, is this retarded or stupid? Is there a better way? Related to the above issue that, for me, Maven has been an all or nothing build with my dependencies (many of which are already loaded into the OSGI repo that I'm using). Hence why I had to find an alternative to include 3rd-party packages. On the final note: I really like OSGI - it's saved lots of time in restarting the webserver as well as giving an immediate rundown on what's working or what's not because of a dependcy missing. But some things are a bit of a pain that could be solved in other stages of development.
|
# ? Nov 19, 2009 06:45 |
|
Alright folks, I really don't know about your policy on helping with assignments, but I've recently had a personal crisis I won't go into now, and I missed a few of my classes. I have an assignment due tomorrow, but I'm unsure on what exactly is going on. I'm a bit of a programming noob and missing those classes kind of hurt things. Well this assignment is due tomorrow, and I'm having a hard time STARTING it. So I am requesting specific help with the first thing I have to do, once that happens, I think I can handle the rest. Anyway... here are my instructions. "The book defines the Node class within the Binarytree class. Move the Node class into its own public definition inside Node.java. Add/remove import statements as needed. 3. Add the following static attribute to the BinaryTree class and the Node class: private static final long serialVersionUID = 1L; 4. (10 pts) Write a static method named ex1() that returns a BinaryTree object representing the expression (2 + 4) * 8" Once I have an example for how to do number 4, I think I can handle the rest of the code, but I'm just having issues getting things working from the start. Anyway, the code I'm working with is here, the driver is at the bottom of the code. Can anyone give me some advice/help me out in any way? http://pastebin.com/m2edfc556
|
# ? Nov 19, 2009 07:15 |
|
Publius_Maximus posted:4. (10 pts) Write a static method named ex1() that returns a BinaryTree object representing the expression (2 + 4) * 8" Do you know what the binary tree for that expression should look like? If you know that, it should be trivial.
|
# ? Nov 19, 2009 07:46 |
|
I don't know much about trees at all, this is the first time I've had to code one, if I saw a sample of something like that, I'd be able to expand on that base for the rest of the code.
|
# ? Nov 19, 2009 09:07 |
|
I've never been taught about trees so take this with a grain of salt. A binary tree is made up of Nodes which are either null or contains a piece of data and references to two other binary trees. A Node which has both references null is known as a leaf. An example of a expression tree for a+(b*c) is: code:
(value of evaluating left child) + (value of evaluating right child) v.o.e left child = a v.o.e right child = ((v.o.e *'s left child) * (v.o.e *'s right child)) v.o.e right child = (b*c) Which leads to a+(b*c) To make a similar representation for (2 + 4) * 8 with the code you posted you will need to construct a class which can contain either an int or an operator. Edit: Or just use a char if you don't need to write code to evaluate it. RichardA fucked around with this message at 10:06 on Nov 19, 2009 |
# ? Nov 19, 2009 10:02 |
|
Sorry guys I should have specified, I appreciate the help so far, but what I'm looking for is what the code itself looks like. I kind of understand the concept, but I have no knowledge of the syntax to put it in.
|
# ? Nov 19, 2009 11:24 |
|
Publius_Maximus posted:Sorry guys I should have specified, I appreciate the help so far, but what I'm looking for is what the code itself looks like. I kind of understand the concept, but I have no knowledge of the syntax to put it in. code:
code:
|
# ? Nov 19, 2009 11:40 |
|
geeves posted:Is there a better way? Since I already expressed my hate towards Maven, I feel I now have to show you the alternative, which for dependency management is Apache Ivy (part of Ant project). The way Ivy handles dependencies is through configurations which there can be as many for every individual project as you like. Basically your very simplest Ivy dependency file could look like this: code:
The test->default part should be read as "my-product's test configuration depends on junit 3.8+'s default configuration". There's of course some default confs I didn't add there, namely default, runtime, compile and master each with their own predefined purpose such as that master is meant for acquiring the immediate result of the org/name resolving, ie. actual product without any of its dependencies. At this point I do want to point out that Ivy has both Ant tasks readily available and great Eclipse plugin called IvyDE for testing and making configurations and dependencies so it's no hassle at all to make even complex combinations; one important feature of the configurations is that they can extend each other, just add extends="anotherconf" attribute to the <conf> element. So, with what you're asking you have the following environments for your project: default - All the normal dependencies you may have within your project. OSGi - All the additional OSGi related dependencies plus the ones from default. In Ivy terms, this would be something like so: code:
Basically now you can make reusable Ant scripts which do stuff such as "get test dependencies and sources from src and test root folders, run all tests, if they pass, run coverage and other statistics and finally build JAR/WAR (don't forget automated MANIFEST.MF!) and deploy it to my own private Ivy repository". Mix all the above with a continous integration environment such as Hudson and you can take that even further; "After the My-Product has been succesfully built, automatically build both default and OSGi deployment packages, deploy those to Server D and Server O and run a set of integration tests on both of them". Best part of CI? You can configure it so that whenever you commit code to your source repository of choice, all of this is done automatically. Sure, making the Ant tasks takes some time but once they're done, they do exactly what you want and if you make them with a bit of a thought, you can reuse them so that you end up with a library of premade Ant targets which you can copy-and-paste, mix and match to create new build.xml:s for your project. Personally I love this way of dependency management and thus I just can't even look at Maven's equivalent anymore. PS. I have an eye infection so if there's any proof reading issues with this post, please buy me antibiotics.
|
# ? Nov 19, 2009 18:10 |
|
Right now I'm writing a servlet to test something at work. The input is coming from a device which is using xoring a string twice using two keys. The result is: .$8:2<! I've verified Java and the box are both giving me the same result when doing it byte by byte. My problem is when reading the http header, that the device must be reEncoding to another format as it is sending me: 8%11%0B%02%0B%1EK%11%16K+%17%08%17%00%1B%11. This immediately looks like some kind of encoding, however, I am not sure what the encoding actually is. Anyone recognize this by the off chance and know how I would decode it in java so I can run the xoring to reverse it?
|
# ? Nov 20, 2009 09:11 |
|
HFX posted:Right now I'm writing a servlet to test something at work. URL encoding. http://java.sun.com/j2se/1.5.0/docs/api/java/net/URLDecoder.html
|
# ? Nov 20, 2009 09:42 |
|
yatagan posted:URL encoding. http://java.sun.com/j2se/1.5.0/docs/api/java/net/URLDecoder.html Sadly tried that but it does not match the string generated by the xoring.
|
# ? Nov 20, 2009 16:40 |
|
HFX posted:Sadly tried that but it does not match the string generated by the xoring. Post your code, and the code of the generator.
|
# ? Nov 20, 2009 19:57 |
|
How do i generate a random long from 1-7 billion, when there is no range function like there is with integer?
|
# ? Nov 22, 2009 15:25 |
|
FateFree posted:How do i generate a random long from 1-7 billion, when there is no range function like there is with integer? If it doesn't need to be cryptographically secure then you can use the same method they use in Random.nextInt(int n). In the javadoc they go over it: All n possible int values are produced with (approximately) equal probability. The method nextInt(int n) is implemented by class Random as if by: code:
|
# ? Nov 22, 2009 16:29 |
|
Yes thats handy for ints, but I can't get a number over 2 billion that way. There is no corresponding long(long range) method in random, i imagine because the int version uses a long to catch overflows. So I guess something special has to be done for my case but I'm not sure what it is.
|
# ? Nov 22, 2009 16:45 |
|
If your desired range is 1...7 billion and you can get to 2 billion, why not just do that one to three times, add those three together and then add another billion?
|
# ? Nov 22, 2009 17:40 |
|
|
# ? May 19, 2024 23:47 |
|
There is probably a better way, but:code:
|
# ? Nov 22, 2009 17:54 |