|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming | |
Java char[] to string?
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#11 (permalink) | ||||||||
|
*cough* Stock *cough*
|
Typically in college courses using the Java API to do something in such a simple way isn't accepted as the way that was intended for it to be done(i.e. Using Arrays.sort instead of sorting yourself). I agree using the char[] constructor of the String class is probably the way to go. The approach I would have used in this setting would have been to use a StringBuffer to append each character to it then returning the toString of the StringBuffer.
__________________
Member of the OCN Diablo III Club
|
||||||||
|
|
|
|
|
#12 (permalink) | ||||||||||||||
|
Kernel Sanders
|
Quote:
|
||||||||||||||
|
|
|
|
#13 (permalink) | ||||||||
|
*cough* Stock *cough*
|
I believe you knowing how to do it is between you and yourself. Using the Java API to the max isn't the solution for doing things in college or else you would learn very little in my opinion.
If you were in a class teaching algorithms and you were dealing with sorting the way to learn sorting is to write the algorithms yourself, the flip side of that is to just use the sorts that are built into Java. I am glad that your school teaches you to do everything with a pre-existing API, it is another way of learning I suppose, we all learn differently. I was just here to present another approach to solving this problem, I apologize if I offended you with my unoptimized solution.
__________________
Member of the OCN Diablo III Club
|
||||||||
|
|
|
|
|
#14 (permalink) | |||||||||||||||
|
Kernel Sanders
|
Quote:
I ran a quick benchmark with this code Code:
import java.util.Random;
import java.util.Arrays;
import java.lang.String;
import java.lang.StringBuffer;
public class Exec {
public static void main(String[] args){
Random rand = new Random();
int num_reps = Integer.valueOf(args[0]), array_size = Integer.valueOf(args[1]);
char[][] char_arrays = new char[num_reps][array_size];
String[] string_array = new String[num_reps];
long start, builtin, buffer, concat;
for(int i=0; i<num_reps; ++i)
for(int j=0; j<array_size; ++j)
char_arrays[i][j] = (char)(rand.nextInt());
System.out.println("Using built in constructor");
start = System.currentTimeMillis();
for(int i=0; i<num_reps; ++i)
string_array[i] = new String(char_arrays[i]);
builtin = System.currentTimeMillis()-start;
System.out.println("Using StringBuffer");
start = System.currentTimeMillis();
for(int i=0; i<num_reps; ++i)
string_array[i] = buildStringBuffer(char_arrays[i]);
buffer = System.currentTimeMillis()-start;
System.out.println("Using concatenation");
start = System.currentTimeMillis();
for(int i=0; i<num_reps; ++i)
string_array[i] = buildStringConcat(char_arrays[i]);
concat = System.currentTimeMillis()-start;
System.out.println("Built in: " + builtin + " Buffer: " + buffer + " Concat: " + concat);
}
public static String buildStringBuffer(char[] array){
StringBuffer sb = new StringBuffer();
for(int i=0; i<array.length; ++i)
sb.append(array[i]);
return sb.toString();
}
public static String buildStringConcat(char [] array){
String ret = "";
for(int i=0; i<array.length; ++i)
ret += array[i];
return ret;
}
}
Quote:
|
|||||||||||||||
|
|
|
|
#15 (permalink) | ||||||||||||||
|
New to Overclock.net
|
Quote:
For example, my most recent college class (Computer Systems), teaches lower level coding (assembly, bytecode, etc.). For one of our assignments, we had to re-write the malloc functions for C. If we go by your method and just use the API, we could have simply passed the malloc functions through our new function and be done. Obviously this wasn't allowed -- we literally did have to re-invent the wheel to do this lab, and it helped a ton in understanding memory allocation.
|
||||||||||||||
|
|
|
|
|
#16 (permalink) | ||||||||||||||
|
Kernel Sanders
|
Quote:
[ot]You don't go to CMU do you? I didn't think malloc lab was that common[/ot]
|
||||||||||||||
|
|
|
|
#17 (permalink) | |||||||||||||
|
First Time Build
|
When I did my java course we went through coding in binary all the way to using the buit-in functions. Naturally using the intermedary steps taught us so many ways to do the same thing and which ways are more efficient for different tasks allowing us to be able to manage memory and stuff much better than if we were just taught the API commands.
__________________
New Build [||||||||||||||||||||] 0$ 455$ 500$
|
|||||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|