right /prog/ i've never used a text board on here, so, here goes. I would like a small hand with some basic java programming. I am using the BlueJ environment.
Given two Strings s1 and s2 already defined as
String s1 = "SESQUIPEDALIANISM";
String s2 = "";
write a sequence of lines of java code that will make String s2 contain (only)
the letters number 3, 10 and 12 from s1
(e.g. if s1 was "piano"
s2 containing letters 1, 3 and 4 would make s2 = "pan").
hmm, i believe what he meant was the he actually wanted to change s2 to contain letters 3, 10 and 12 from s1. Making it SAI? Please correct me if im wrong? IF so, would the charAt work for this? Im not sure.
Name:
`Peyote` Simon Joints2009-01-19 10:27
>>7
What is this ``change'' concept? I am afraid I have never heard of such a concept.
>>7 IF so, would the charAt work for this? Im not sure.
Well, I'm pretty fucking sure that it will. If you don't know elementary Java, why post in this thread at all?
>>5,6
This can be done in JavaScript too(using small arrays is wasteful however):
var s2='sdiusadishdsf';
var sg1=[3,5,7];s1=''
for(i in sg1){s1[i]=s2[sg1[i]]};s1.join("")
_________________________
orbis terrarum delenda est
>>15
An earthquake occurred and all the singletons are now trapped under the factory. Only an expert can save them!
Are an expert enough to save singletons?
_________________________
orbis terrarum delenda est
/* StringBuffer provides synchronization so this code can be executed
* correctly by multiple threads. */
StringBuffer stringBuffer = new StringBuffer();
/* Construct the result string by concatenating substrings
of s1. */
stringBuffer.append(s1.charAt(3-1));
stringBuffer.append(s1.charAt(10-1));
stringBuffer.append(s1.charAt(12-1));
/* Set the result to s2. */
s2 = stringBuffer.toString();
I don't know a while lot about Java, but, if the string is allocated on stack, then each thread has its own stack, so there's no need to global object to access, so no need to synchronize, if it's done via heap, then it's still a local variable to your function, so you wouldn't need to lock it, as no other code can access it(not even your function when running twice). You would only need to do this if you were editing a member of some class, and then, but then, you'd need to take extra precautions, which are not included in your code.
Have I been trolled?
Name:
Anonymous2009-01-19 22:04
Have I been trolled?
You seem to have forgotten, sir; This is /prog/.
I too lol'd. However, the faggot in >>19 got the "synchronization" aspect wrong, but using StringBuffer is a good idea because string are immutable (as in their would be 5 string allocated for the program). And even though these conditions are known at compile time I highly doubt any JVM or compiler would be smart enough to optimize that.
If you used immutable stings in .Net, it would optimize to not to the excessive allocations.
Name:
Anonymous2009-01-20 0:01
>>22
The + operator in Java is typically converted by the compiler to StringBuffer anyway (or more likely StringBuilder, since you're using a 1.5+ JVM). So most uses of + are already append() calls. You'll only run into performance problems when you start reassigning String references (since the bytes of the string's old value must be garbage collected). And finally, arguing about asymptotic runtime is irrelevant when the OP's example only involves three concats.