|
I think you want the Javascript thread...
|
# ? Dec 1, 2009 18:46 |
|
|
# ? May 25, 2024 14:53 |
|
Quick question: Say I had an ArrayList of 5 objects, and I wanted to loop through this ArrayList 5 times, starting from a different object each time. Is this possible? So normally when I loop through once, it starts on the first object and works its way down to the fifth, I want to make it so that it then starts on the second and ends on the first, then starts on the third ends on the second etc. Hope I explained this OK.
|
# ? Dec 4, 2009 04:55 |
|
Mellomeh posted:Quick question: Since it sounds you are new to things, I broke this out into more lines then I probably would have written. code:
|
# ? Dec 4, 2009 05:21 |
|
HFX posted:Since it sounds you are new to things, I broke this out into more lines then I probably would have written. Thank you so much, I am fairly new to Java, so this is perfect.
|
# ? Dec 4, 2009 05:31 |
|
I'm having a bit of a brain fart at the moment and can't figure out the right way to convert:code:
|
# ? Dec 4, 2009 06:07 |
|
dis astranagant posted:I'm having a bit of a brain fart at the moment and can't figure out the right way to convert: Personally, I would overload equals and use a set to keep the conflicts from happening in the first place. Remember kids, the foreach for loop is just syntactic sugar for getting an iterator and using a while loop. HFX fucked around with this message at 08:09 on Dec 4, 2009 |
# ? Dec 4, 2009 07:58 |
|
dis astranagant posted:I'm having a bit of a brain fart at the moment and can't figure out the right way to convert: I do not know for sure if what you're doing there is correct or not since I don't have the bigger picture, but when I usually want to remove elements from a list while traversing through it i do something like: code:
|
# ? Dec 4, 2009 15:53 |
What is the best way to copy part of a byte array into another byte array? Do I have to loop through it and copy it 1 by 1?
|
|
# ? Dec 4, 2009 21:29 |
|
java.util.Arrays.copyOf() java.util.Arrays.copyOfRange() java.lang.System.arrayCopy() The first two create the copy, the third takes the destination as a parameter
|
# ? Dec 4, 2009 21:38 |
Mustach posted:java.util.Arrays.copyOf() Sweet! Worked great. Thank you!
|
|
# ? Dec 4, 2009 23:49 |
|
I have a memory leak in a large Java program. I got an hprof dump and used an Eclipse analyzer to try to make sense of it. I am finding a JDBC ResultSet object is dangling around. I don't know if that's the single problem, but it shouldn't be there, and I want to figure out how to sever it. My program is making a few huge queries to a database using a singleton to facilitate all communication to the database. From these queries I get ResultSet objects to gobble up everything into the data structure my program needs. The singleton is a warning sign right there. All ResultSets I instantiate are local to the methods in which they are called. Before leaving the method, I will close the ResultSet, and try to close it when an Exception occurs. That's the same for the database connection. Generally each method looks something like: code:
All I can think of doing now is after closing the results and connection that I set the reference to null. Is there anything out I should watch out for?
|
# ? Dec 6, 2009 22:42 |
|
Use finally to close resources.code:
code:
Fly fucked around with this message at 07:01 on Dec 7, 2009 |
# ? Dec 7, 2009 06:56 |
|
Fly posted:You can write a safeClose(ResultSet rs) that catches the exception if you have to repeat this pattern a lot. zootm fucked around with this message at 11:11 on Dec 7, 2009 |
# ? Dec 7, 2009 09:27 |
|
I ended up writing a helper that not only closed the connections but set the pointers to null. That nuked the stray ResultSet that was laying around. I ended up having some danglers of my own which were probably the more fundamental problem. I figured I'd share. If you have a list of jobs to do of some kind, it's probably better for the resources you have to dequeue them and dismiss the old references rather than using, say, a for loop with an index. The completed jobs hang around afterwards because they're still in the data structure. In my case, the jobs started as about 4kB of memory and ballooned to about 120MB after they were finished. By removing the references the GC was able to see them as unused anymore and reclaim them.
|
# ? Dec 7, 2009 20:23 |
|
Rocko Bonaparte posted:I ended up writing a helper that not only closed the connections but set the pointers to null. That nuked the stray ResultSet that was laying around. I ended up having some danglers of my own which were probably the more fundamental problem. I figured I'd share. WeakReference is your friend.
|
# ? Dec 7, 2009 20:37 |
|
Two possibly naive questions from a Java neophyte: 1. I've gotten interested in dynamic languages running on the JVM (for development speed, quick scripts, data analysis etc. as well as for learning another language). Coming from Python, Jython is the obvious choice but it seems to be a 2nd class citizen - just a bit messy, some standard libraries not complete, a bit slow, doesn't seem to have a huge development effort behind it. JRuby? Also a bit slow, and I'm a bit concerned about Ruby oddities. So my eye has been drawn to Groovy and Scala. Scala in particular looks cool, and promises to be fast for a dynamic language. Any opinions or things I should look out for? What are the distinguishing points or USPs between Groovy and Scala? 2. What numerics, stats and graphing libraries would people recommend within Java? In Python, there's just a few obvious leaders that everyone uses (Numpy and matplotlib), but I haven't been able to sort the best options from the wide variety available for Java.
|
# ? Dec 7, 2009 20:43 |
|
TRex EaterofCars posted:WeakReference is your friend.
|
# ? Dec 7, 2009 20:44 |
|
Fly posted:That could be a bad idea though. If the jobs have not been completed, but they are only weakly reachable, the garbage collector could get rid of them before they are executed. In the case of a work queue, I suggest a producer/consumer model for most situations. Yeah I thought about that once I posted. Oops. If they're non-critical you could use a SoftReference instead, I suppose.
|
# ? Dec 7, 2009 20:50 |
|
Okay, I have no idea what's going on but I'm all of a sudden having real difficulty with packages and classpaths under Windows 7. I've just tested the following: code:
code:
Edit: It seems to be a problem with the way I was compiling. if I specify the classpath as .. when I compile within the mypack folder it compiles fine. I must have the classpath set differently on my other pc, I guess. Flamadiddle fucked around with this message at 02:06 on Dec 8, 2009 |
# ? Dec 8, 2009 01:36 |
|
Flamadiddle posted:Okay, I have no idea what's going on but I'm all of a sudden having real difficulty with packages and classpaths under Windows 7.
|
# ? Dec 9, 2009 19:31 |
|
Yeah, I figured that much out, thanks. Can't believe it took me so long.
|
# ? Dec 10, 2009 00:38 |
|
ynef posted:Nope, that's what is supposed to happen: the class path should point to a location where the subdirectories are the same as the package names of the classes that you want to use. Speaking of which, is there any way to circumvent this requirement? In my recent software design project (a simple card game), one of the bonus features we implemented was to have AI classes loaded dynamically from .class files. We did this, but it was a pain in the rear end to have to have them in a properly named subdirectory. I suppose the proper way to do this is insist they be properly packaged in a separate JAR?
|
# ? Dec 10, 2009 00:59 |
|
Use a custom ClassLoader subclass; just feed it a bucket of bytes and (if appropriate) the fully-qualified class you expected to find, and it'll give you a class back.
|
# ? Dec 10, 2009 02:38 |
|
I need to count the number of comparisons and swaps(aka exchanges) that occur in this code when sorting an int array:code:
|
# ? Dec 10, 2009 05:11 |
|
StickFigs posted:I need to count the number of comparisons and swaps(aka exchanges) that occur in this code when sorting an int array: Does insertion sort work by swapping? Homework dude posted:and numComparison++('s)? Look for compareTo.
|
# ? Dec 10, 2009 05:41 |
|
Brain Candy posted:Does insertion sort work by swapping? Well it's just one array so somethings getting swapped.
|
# ? Dec 10, 2009 06:29 |
|
reordered!=swapped Imagine four balls on the edge of a cliff... insertion sort is the same way.
|
# ? Dec 10, 2009 06:42 |
|
Whatever I don't care what its called I just need to count them.
|
# ? Dec 10, 2009 06:45 |
|
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:
|
# ? Dec 10, 2009 06:52 |
|
According to this I should get 26 "Exchanges" and 32 "Comparisons" with this array: int [] arr1 = {4,8,3,7,6,1,2,5,0,9} With my code: code:
|
# ? Dec 10, 2009 07:07 |
|
PT6A posted:Speaking of which, is there any way to circumvent this requirement? In my recent software design project (a simple card game), one of the bonus features we implemented was to have AI classes loaded dynamically from .class files. We did this, but it was a pain in the rear end to have to have them in a properly named subdirectory. I suppose the proper way to do this is insist they be properly packaged in a separate JAR?
|
# ? Dec 10, 2009 09:28 |
|
Any recommended json libraries or such?
|
# ? Dec 10, 2009 12:26 |
|
tef posted:Any recommended json libraries or such?
|
# ? Dec 10, 2009 12:51 |
|
tef posted:Any recommended json libraries or such? I've been looking at this as well and currently have been comparing json.org/java vs json-lib.sourceforge.net (based on json.org but probably a bit more developed, according to them) and Jackson http://jackson.codehaus.org/ There is also one in Sling, but it's a bit limited and geared toward working with a JCR.
|
# ? Dec 10, 2009 16:42 |
|
tef posted:Any recommended json libraries or such? I use the one included with DWR but not sure how it compares to anything else, just wanted to make a GeoJSON library for a GIS project and was already using DWR for some AJAX poo poo. It is more geared for building JSON then parsing it though. (there is a bug in the DWR 3.0 RC1 distribution with JSON, if you go that route build a nightly)
|
# ? Dec 10, 2009 16:56 |
|
I have a question about performance and loops. Right now it works, but I'm looking for efficiency. I guess this is more general programming than Java, but here it goes. I am getting a list of tags, looping through them and then looping through the list of pages each of them return. I'm working with a Java Content Repository. So I have a loop within a loop. I've never thought this was ideal. I'm still a somewhat Java beginner when it comes to certain concepts, but I hate nested IF statements, especially when checking for NULL. Should I leave that to the catch / exception handling? If something is null I don't want it to kill the whole loop, just ignore it (and log it, of course). Should the second loop be another method or does that not matter? Is it better to have a nested try/catch/ finally? Should these be in a separate method? Is it personal preference? Or just something I'm stuck with? Besides working, I like to go for readability and I know I'm not perfect in that area, but I do try my best. code:
|
# ? Dec 15, 2009 05:59 |
|
geeves posted:I have a question about performance and loops. Right now it works, but I'm looking for efficiency. I'm having a bit of trouble figuring out the bigger picture of what you are doing (which could help me recommend something better for you). So I'll talk you a bit about what I do get. geeves posted:I have a question about performance and loops. Right now it works, but I'm looking for efficiency. First efficiency is something to be careful about. How often is this running as part of your whole program? Are you spending an inordinate amount of time in it and why? Now for your code: The loops themselves would probably be better labeled using a comment or even better your method has a descriptive name minimizing the need for a comment. The inner loop is a good target for a private method. Java doesn't support private methods for a given method, so you will have to put it in the class itself. The outer loop could be also, but I can't see your whole method. Do not worry about checking for nulls. This is actually how you want to write your code if you expect nulls. Avoid throwing exceptions except in exceptional circumstances. With more knowledge about what you are trying to achieve, I possibly could recommend a better way of solving it, however, there is nothing super wrong about code.
|
# ? Dec 15, 2009 06:24 |
|
HFX posted:I'm having a bit of trouble figuring out the bigger picture of what you are doing (which could help me recommend something better for you). A simple weighting system for recommending an item for a user. I have, for example, 7 tags on an Item. Each of those tags maybe used on 40 or 50 other Items. If an item already exists in the HashMap, then its weight (the Integer) is added. If it's a certain "mega" Item, its weight is +2 instead of +1. That's a simple if/else in the if (null != page) quote:
It runs only when a user saves an item to their cart. I don't know how often this will run. We know very little of the behavior of our visitors aside from what basic HTTP stats will tell us. Our site however gets upwards of 600-700k unique visits per month and 4-5 million page views per month. How many people will register and save content - I don't know. Nobody does and yes, that is the problem. This is an entirely new feature and we're going from a completely static HTML site (managed with a "baked" CMS) to a JCR-based content repository. Basically I'm thinking worst case scenario. quote:Now for your code: That is the whole method, minus an if/else statement in the inner loop. I used to write a lot of loops for some hosed up business logic that needed to break the outer loop, so it's an old habit to label them in that way. quote:
That's been the problem with development, we have so much content and items that it's several gigs to import onto our local machines. That and there were several iterations of importing (to find the best structure in the JCR) so it was just easier to check for null or else it would throw a NullPointer and kill action. quote:With more knowledge about what you are trying to achieve, I possibly could recommend a better way of solving it, however, there is nothing super wrong about code.
|
# ? Dec 15, 2009 17:03 |
|
I'm under the impression that in Java*, exception handling is slow compared to conditional statements and that it is not intended to be part of the normal flow of program execution. In general, you should only throw and expect to catch an exception if something ... exceptional happens. If having a null value in your collection is normal and common, don't write code as if it won't happen and catch the NullPointerException. *Silly Python with its StopIteration exceptions. I kid; I love Python and think that this is a perfectly reasonable way to implement something like this, especially if exception handling is designed to be fast enough for this to be feasible.
|
# ? Dec 16, 2009 07:57 |
|
|
# ? May 25, 2024 14:53 |
|
Lysidas posted:I'm under the impression that in Java*, exception handling is slow compared to conditional statements and that it is not intended to be part of the normal flow of program execution.
|
# ? Dec 16, 2009 20:28 |