|
triplekungfu posted:What type is name in the Product class? If it's a String already, you don't need those casts to String. It's only a minor issue, but every time I see stuff like that at work I die a little. String is final, so name is either a String or an Object (or CharSequence or Comparable<String>(!)). Brain Candy fucked around with this message at 12:56 on Mar 5, 2008 |
# ¿ Mar 5, 2008 12:42 |
|
|
# ¿ May 4, 2024 18:28 |
|
triplekungfu posted:If you're not concerned with performance you could use the apache commons SerializationUtils; as mentioned in the javadoc, all objects you want cloned must implement Serializable (which ArrayList implements already). Use it if you aren't concerned about good programming or sanity either. This seems like a horrible abuse of Serialization, which is supposed be about passing objects across a network. ArrayList also has a copy-constructor so if the data is immutable its very easy, otherwise use foreach loops. Manually copying data from one instance to another is not going to be that bad. There is an problem with marker interfaces (interfaces that don't actually require methods, including Cloneable and Serializable) in that you can't get rid of them in subclasses. 1337JiveTurkey posted:Immutable objects are also a possible alternative. If you're not constantly modifying a class of objects, you can get around all of that by just creating a new object for every change. Immutable objects work well with each other because any two objects can share a third object without needing to worry if the third object will change underneath them. This reduces memory usage requirements and makes it easier to turn other objects immutable if all of their fields are immutable. Immutable objects definitely nice for many things, but they come at a price. Strings, for example are immutable and the JVM does all sorts of optimization with them. But you'll also notice that there are special helper classes, StringBuilder and StringBuffer, just to create new Strings. In general, every time you change an immutable field you have to create a new object. Brain Candy fucked around with this message at 12:05 on Mar 25, 2008 |
# ¿ Mar 25, 2008 12:02 |
|
epswing posted:There are lots of ways... This makes my brain hurt, or maybe its a coffee deficiency. code:
code:
|
# ¿ Apr 8, 2008 12:46 |
|
epswing posted:Nope, depends on if qry already has content such as "select ...". In the code block you gave, you constructed qry inside the block. So if your while loop executes at least once, the last char of qry will be ','.
|
# ¿ Apr 9, 2008 00:19 |
|
ColdPie posted:How "should" I be doing this in Java? Inheritance and/or enums. Its hard to say exactly without any idea of why you want to use bit flags.
|
# ¿ Apr 9, 2008 01:32 |
|
Clanpot Shake posted:... Are you sure this does what you think it does? i.e. (int) (6.99999997 * 10) = 69 but (int) (7.0000001 * 10 ) = 70. For positive numbers, (int) is equivalent to floor, not the nearest integer.
|
# ¿ Dec 2, 2008 14:56 |
|
Slimy posted:In C++, I'd just have the implementation of getArrayListByType() return a pointer to foo and bar, which I could dereference. Why bother to use a switch at all? Why don't you use a List<List<Message>>?
|
# ¿ Feb 20, 2009 06:06 |
|
Since it's static anyway... :code:
|
# ¿ Feb 20, 2009 14:43 |
|
rhag posted:(concatenate strings with +, while == performs an .equals() ). BZZZT. == foo is NOT .equals(foo) for Strings You'd have to use .intern() or something similar for == to consistently work.
|
# ¿ Jul 16, 2009 03:38 |
|
yatagan posted:
Since you know the size after the iteration, why don't you just pass it in instead of making two arrays?
|
# ¿ Oct 23, 2009 13:42 |
|
rjmccall posted:Functions: still a more fundamental tool of abstraction than anonymous classes. This gives a warning for a reason. If T is also generic, T[] will be of the erased type. Arrays do not play nicely with generics.
|
# ¿ Oct 24, 2009 03:38 |
|
rjmccall posted:Also, I must note that every implementation of Collection.toArray has to do exactly what I'm describing here, uncheckable cast and all. don't use arrays.
|
# ¿ Oct 24, 2009 18:54 |
|
Mustach posted:The whole point of this conversation was that the guy was using an API that wanted an array instead of a collection. Yes, yes, but if you use arrays enough to require a helper method you've got more problems than making the arrays.
|
# ¿ Oct 24, 2009 22:52 |
|
rjmccall posted:Heaven save us from the vicious tyranny of having too many helper functions. Way to miss the point!
|
# ¿ Oct 25, 2009 19:03 |
|
If only there were some way to hide the interaction with bad code without re-writting it.
|
# ¿ Oct 26, 2009 14:06 |
|
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.
|
# ¿ Nov 11, 2009 05:39 |
|
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 |
|
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 |
|
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 |
|
reordered!=swapped Imagine four balls on the edge of a cliff... insertion sort is the same way.
|
# ¿ Dec 10, 2009 06:42 |
|
yatagan posted:I agree, but this is return !A && !B, which takes me a second to parse. DeMorgan's laws should be burned into your brain.
|
# ¿ Mar 9, 2010 13:24 |
|
Necc0 posted:I'm just getting into generics in java and I can't figure out how to compare to generic variables. You can just use E instead of Comparable<E> in your method signature, there is no need for casting. code:
|
# ¿ Mar 30, 2010 11:57 |
|
Parantumaton posted:There isn't convention for this which causes people to use at least these three depending on how bad the coffee was the morning they started programming: Bad programmer, no biscuit! Hungarian notation is terrible unless you program exclusively in notepad.
|
# ¿ Jul 28, 2010 03:26 |
|
Internet Janitor posted:Don't drive yourself insane trying to get rid of exceptions everywhere, but avoid using them anywhere performance is a significant concern. Don't use exceptions for flow control and you won't have to worry about performance issues!
|
# ¿ Nov 30, 2010 01:10 |
|
An exception is thrown because : User provides bad data You log the error. Because you are using I/O this is far slower than a normal operation. You provide feed back to the user. A programmer was made a mistake You log the error. Because you are using I/O this is far slower than a normal operation. If the error is fatal, you exit. Exceptions are being used for flow control or a magical extra return value If you wrote it, gently caress you. Go stick your head in blender. If you didn't write it, speed is the least of your concerns. Outside of rare corner cases, who cares if exceptions are slow?
|
# ¿ Dec 1, 2010 03:15 |
|
Exceptions in java are for exceptional things. If you want to write things more python-y (StopIteration), you can use labeled breaks :code:
|
# ¿ Dec 1, 2010 03:59 |
|
Internet Janitor posted:And if you want to break out of deeply nested recursion... basically don't do that. return
|
# ¿ Dec 1, 2010 04:18 |
|
poemdexter posted:
Kamakaze9 posted:
It’s been said that there are only two hard problems in Computer Science: cache invalidation, naming things and off-by-one errors
|
# ¿ Mar 24, 2011 05:52 |
|
HFX posted:Where are these callback mechanisms for non blocking i/o. Specifically, is there a way I could use them with stuff from concurrent and maybe show an example / tutorial? Look at http://java.sun.com/developer/technicalArticles/releases/nio/, use the links at the bottom if you need more.
|
# ¿ Jun 11, 2011 00:11 |
|
Java 7 fixed some bugscode:
|
# ¿ Jul 29, 2011 04:03 |
|
List is basically useless as an interface for solving real problems because I have no idea whether random access is going to require iterating through half the list.
|
# ¿ Sep 14, 2011 15:11 |
|
HFX posted:Who cares. The point of an interface is that you don't care about the underlying implementation, and if you do you go look up its instantiation / its behavior should be documented if it matters. List (the interface) is is overly broad when I receive it from someone else. I shouldn't have to require an ArrayList, because my method will perform similarly on a any list with constant time element access... like the ones returned from Arrays.toList, or a Collections.unmodifiableList that wraps an ArrayList, let alone user defined ones.
|
# ¿ Sep 15, 2011 01:34 |
|
RandomAccess is a marker interface. Hay, I love using instanceof (just like Collections classes are forced to!)
|
# ¿ Sep 15, 2011 02:35 |
|
The best comment should have occurred in when making Java 1.2 :J. Bloch posted:
I'm mad, mad about List.
|
# ¿ Sep 15, 2011 06:25 |
|
In 2013, multi-threading is important. Please don't corrupt the young with advice based on the terrible enterprisey practices of a decade ago. Your stackholm syndrome is showing.
|
# ¿ Feb 4, 2013 15:10 |
|
Interrupts are everywhere 'cause spurious wakes are a real thing. The key trick is every interruptible call really needs to matched with a loop+condition. If you really want to guarantee a 'sleep' for at least some time : Java code:
|
# ¿ Aug 1, 2013 03:40 |
|
Looking at it, Guava still has the same problem because it's basically the loop I posted plus actually forwarding the interrupt. The heart of the issue is Thread.sleep(long,long). There are no guarantees about the minimum argument it consistently works for. For years, on windows (maybe still?), Thread.sleep(1) actually slept 10ms because that was the period of the available system timer. This isn't a big deal if you pass 1000ms into it, you've just got to be aware that the accuracy is going to be significantly less than the precision and that it's machine/jvm/os dependent.
|
# ¿ Aug 1, 2013 13:30 |
|
You can always put things back into the queue Use LinkedBlockingQueue to have drainTo. Use offer to add things. edit: there were words about iterators, but you don't need to care Brain Candy fucked around with this message at 04:40 on Aug 7, 2013 |
# ¿ Aug 7, 2013 04:16 |
|
|
# ¿ May 4, 2024 18:28 |
|
Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo.
|
# ¿ Aug 15, 2013 03:52 |