|
Also, the solution 27^5 + 84^5 + 110^5 + 133^5 = 144^5 is a hint to the algorithm.
|
# ? Feb 2, 2010 18:55 |
|
|
# ? May 20, 2024 10:44 |
|
I'm new to Java 5+ What's going on in this code, what is the colon character and classname after it? code:
mister_gosh fucked around with this message at 20:00 on Feb 2, 2010 |
# ? Feb 2, 2010 19:58 |
|
That's C#.
|
# ? Feb 2, 2010 20:01 |
|
How can I iterate over Keys in a LinkedHashMap? Will this work for looping through the keys in fileMap, will record be given the first value of fileMap? LinkedHashMap<String, Integer> fileMap; //String is key/value, Integer is frequency code:
code:
|
# ? Feb 2, 2010 23:21 |
|
...
maskenfreiheit fucked around with this message at 03:45 on Sep 29, 2010 |
# ? Feb 2, 2010 23:33 |
|
Check your PMs
|
# ? Feb 2, 2010 23:40 |
|
GregNorc posted:
|
# ? Feb 2, 2010 23:48 |
|
GregNorc posted:lots of useless code How about realising the conditions that e has to be larger than max(a,b,c,d) and smaller than (2^63)^0.2 if you're using longs. I'm sure that gives you a suitable starting point. lewi fucked around with this message at 00:11 on Feb 3, 2010 |
# ? Feb 3, 2010 00:06 |
|
lewi posted:How about realising the conditions that e has to be larger than max(a,b,c,d) and smaller than (2^63)^0.2 if you're using longs. I'm sure that gives you a suitable starting point. Is there even any need to iterate e?
|
# ? Feb 3, 2010 01:09 |
|
gregnorc: here is a basic lesson on nesting loops. A For loop like the following: code:
code:
Looking at a For loop again, we can identify four parts: quote:for( A; B; C) { A - the initializer. An expression evaluated before the loop executes. B - the precondition. An expression that controls when the loop terminates (loop while this is true, break when false) C - the postoperation. An expression evaluated after every iteration of a loop. D - the body, a series of statements making up the content of a loop. you can make the precondition more complex: code:
code:
code:
I highly recommend you pretend the break and continue keywords don't exist until you've mastered these ideas.
|
# ? Feb 3, 2010 01:37 |
|
karuna posted:How can I iterate over Keys in a LinkedHashMap? To iterate over the keys of a Map (any kind of a class that implements the Map interface: HashMap, LinkedHashMap,Hashtable,etc.), do this: code:
code:
|
# ? Feb 3, 2010 17:16 |
|
rhag posted:To iterate over the keys of a Map (any kind of a class that implements the Map interface: HashMap, LinkedHashMap,Hashtable,etc.), do this: I believe it's technically faster to iterate over the entry set if you need both key and value in your block. code:
|
# ? Feb 3, 2010 19:33 |
|
TRex EaterofCars posted:I believe it's technically faster to iterate over the entry set if you need both key and value in your block.
|
# ? Feb 4, 2010 10:58 |
|
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 |
# ? Feb 4, 2010 17:35 |
|
There doesn't have to be anything magical about it; it's up to the implementation of the interface. But do you really care? If you want the set of (key, value) pairs of a Map, entrySet() gives you exactly what you want. Using keySet() and then calling get() repeatedly is at-best reimplementing the same functionality.
|
# ? Feb 4, 2010 18:11 |
|
epswing posted:Assuming "lookup" means retrieving a value based on a key, does Map.Entry magically obtain a value without a lookup?
|
# ? Feb 4, 2010 18:42 |
|
Fly posted:When iterating over a HashMap's entrySet, there is no hash lookup done at all. The entry objects contain the key and value references so retrieving the key and value are simple accessor methods on the Entry object. One gotcha I'd like to mention, Entry contains apparently only weak references to the key and value it holds so the following code may throw NPE if you get a bit unlucky with GC: code:
|
# ? Feb 4, 2010 18:53 |
|
Parantumaton posted:One gotcha I'd like to mention, Entry contains apparently only weak references to the key and value it holds so the following code may throw NPE if you get a bit unlucky with GC: This is the first I've ever heard of these, how interesting. http://weblogs.java.net/blog/enicholas/archive/2006/05/understanding_w.html
|
# ? Feb 4, 2010 19:57 |
|
Parantumaton posted:One gotcha I'd like to mention, Entry contains apparently only weak references to the key and value it holds... code:
code:
code:
|
# ? Feb 4, 2010 21:27 |
|
Alright, I'm a total Java retard so please bare with me. I found a snippet of code online that I was feeding in different input to see what I could make it do and I've run into a wall of sorts. I don't know java well enough to describe what I want to do without showing the code so here it goes.code:
code:
code:
|
# ? Feb 7, 2010 13:05 |
|
Coredump posted:However if try to use the line: Try this: code:
edit:grammars Flobbster fucked around with this message at 23:30 on Feb 7, 2010 |
# ? Feb 7, 2010 13:24 |
|
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."
|
# ? Feb 7, 2010 18:39 |
|
I have a LinkedList<ArrayList<String>>. I need to write it out to a text file. Each ArrayList is one line. I need to insert a separator between each element in the ArrayList. i.e ArrayList [1,2,3,4,5,6] Should be (if the separator is say "-") "1-2-3-4-5-6" What would be the most efficent way to acheive this? Really not liking having to keep creating new Strings seen as they are not mutable. code:
|
# ? Feb 8, 2010 04:45 |
|
If I understand the question right, shouldn't you be able to do something like this?code:
|
# ? Feb 8, 2010 05:01 |
|
karuna posted:What would be the most efficent way to acheive this? Not going to answer the question as Internet Janitor already did a fine job of that, but for future reference you might want to check out the StringBuilder class. That lets you concatenate lots of Strings together without the overhead of creating a new String object for each intermediate step. Interestingly, the '+' operator on a String is just shorthand for using a StringBuilder, so a piece of code like 'string1 + string2 + string3' will actually compile to something like 'new StringBuilder(string1).append(string2).append(string3).toString()'. e: There's also StringBuffer too which does the exact same thing but is threadsafe if that's important to you. Contra Duck fucked around with this message at 11:52 on Feb 8, 2010 |
# ? Feb 8, 2010 11:48 |
|
Contra Duck posted:Not going to answer the question as Internet Janitor already did a fine job of that, but for future reference you might want to check out the StringBuilder class. That lets you concatenate lots of Strings together without the overhead of creating a new String object for each intermediate step. Interestingly, the '+' operator on a String is just shorthand for using a StringBuilder, so a piece of code like 'string1 + string2 + string3' will actually compile to something like 'new StringBuilder(string1).append(string2).append(string3).toString()'. Be careful with assuming + always converts to StringBuilder. There are circumstances where javac will not do that. Sadly, I'm lazy and I don't know offhand what those circumstances are but know that they exist. When in doubt, javap your classes.
|
# ? Feb 8, 2010 14:59 |
|
TRex EaterofCars posted:Be careful with assuming + always converts to StringBuilder. There are circumstances where javac will not do that. Sadly, I'm lazy and I don't know offhand what those circumstances are but know that they exist. The circumstances are: at least JDK 1.5 from Sun . (Mainly because that's when StringBuilder appeared). Any other JDK, or earlier versions do not do that. As such, since one shouldn't really rely on new compiler features, and since writing the proper code is so easy, I would recommend just using StringBuilder to concatenate strings in a loop. Since 1.5 (I think) the javac (from Sun, no idea about other vendors), got a bunch of features that make life easier, but confuse the hell out of new programmers. For example, == with strings on both sides translates into .equals(). Newcomers to java may think that's true for objects as well (that's why, never use == unless you really want to compare instances, otherwise just use .equals). Autoboxing : there doesn't seem to be any difference between int and Integer now. I imagine that's helluva confusing for a newcomer. I love the new features introduced then, as they do make life easier, but new people who don't carefully read the documentation and just want to write Hello World as fast as possible won't even notice the subtle differences between operators on String and MyShinyObject.
|
# ? Feb 8, 2010 16:30 |
|
rhag posted:For example, == with strings on both sides translates into .equals(). code:
code:
code:
|
# ? Feb 8, 2010 16:56 |
|
Mustach posted:What Well, yes, its not really equals, since it maintains a static hashtable of strings, but it certainly behaves the same way. that is: code:
edit: The entire idea here is how confusing is this for newcomers: do you have 2 instances there or don't you? If you don't, when why when I do MyShinyObject x=new MyShinyObject()3 times, I end up with 3 instances (thus == doesnt work). Most (certainly not all) won't read the Java language specification before they write their first hello world. Nor will they look at the generated code. Volguus fucked around with this message at 17:23 on Feb 8, 2010 |
# ? Feb 8, 2010 17:11 |
|
rhag posted:Well, yes, its not really equals, since it maintains a static hashtable of strings, but it certainly behaves the same way. It either should or can (the language spec is vague, IIRC), and not because it's replacing == with .equals, but exactly because it's not creating redundant string objects. "a" + "b" == "ab" either can or should return true, too.
|
# ? Feb 8, 2010 17:21 |
|
rhag posted:it certainly behaves the same way code:
code:
Mustach fucked around with this message at 17:38 on Feb 8, 2010 |
# ? Feb 8, 2010 17:31 |
|
Mustach posted:
I'm sorry sir, but your code is a completely different story. Reading strings from streams is a different ball game on many levels. I know you just want to prove me wrong for the sake of it, but your example doesn't really cut it. If you read a string from system.in (or anywhere else, as an input from something) is pretty logical that it will create a new object in memory.
|
# ? Feb 8, 2010 17:42 |
|
rhag posted:Autoboxing : there doesn't seem to be any difference between int and Integer now. I imagine that's helluva confusing for a newcomer. == may have some unexpected behavior since the values in [-128,127] are cached: code:
code:
code:
|
# ? Feb 8, 2010 17:47 |
|
rhag posted:I'm sorry sir, but your code is a completely different story. "== translates to .equals()." "Oh, well I didn't realllyyyyyy mean the .equals() method, it just behaves the same way." "A program reading input? Fool, this whole time I was only referring to string literals, mwahahahahaha"
|
# ? Feb 8, 2010 17:50 |
|
RitualConfuser posted:So, as already pointed out several times, use the equals method when comparing objects.
|
# ? Feb 8, 2010 18:41 |
|
Are you discussing of interned Strings as being converted to .equals() comparison on the fly?
|
# ? Feb 8, 2010 21:30 |
|
Parantumaton posted:Are you discussing of interned Strings as being converted to .equals() comparison on the fly?
|
# ? Feb 8, 2010 22:26 |
|
Summary of the String.equals() thing: s1 == s2 is never equivalent to s1.equals( s2 )[1] and treating it as such is both dangerous and wrong. Let's see use-cases for using == with String objects:
In conclusion: don't do it, don't tell people to do it. [1] (or the nullsafe equivalent of the same)
|
# ? Feb 9, 2010 14:43 |
|
zootm posted:s1 == s2 is never equivalent to s1.equals( s2 )[1] and treating it as such is both dangerous and wrong. It looks like deserialization does create Enums as the same reference (http://java.sun.com/j2se/1.5.0/docs/guide/serialization/spec/serial-arch.html#6469): code:
a == b ? true Fly fucked around with this message at 20:28 on Feb 9, 2010 |
# ? Feb 9, 2010 19:45 |
|
|
# ? May 20, 2024 10:44 |
|
Fly posted:Regarding Enums, I do not remember reading docs or code that would indicate a deserialized Enum would have the same reference as it did before. Do you have any reference to whether Enums are interned somehow upon deserialization? quote:The rules for serializing an enum instance differ from those for serializing an "ordinary" serializable object: the serialized form of an enum instance consists only of its enum constant name, along with information identifying its base enum type. Deserialization behavior differs as well--the class information is used to find the appropriate enum class, and the Enum.valueOf method is called with that class and the received constant name in order to obtain the enum constant to return. A lot of thought seems to have gone into making enums "work right". I guess there's a chance it might be possible to circumvent this using reflection, but even that seems unlikely. e: f, b
|
# ? Feb 9, 2010 20:23 |