New Posts  All Forums:Forum Nav:

Java char[] to string? - Page 2

post #11 of 17
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.
Inspiron E1705
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Mobile Core 2 Duo T7200 Dell 0YD479 NVIDIA GeForce Go 7900 GS 256Mb 
OS
Windows XP 
  hide details  
Reply
Inspiron E1705
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Mobile Core 2 Duo T7200 Dell 0YD479 NVIDIA GeForce Go 7900 GS 256Mb 
OS
Windows XP 
  hide details  
Reply
post #12 of 17
Quote:
Originally Posted by stphung View Post
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.
That's going to be way slower than Java's built in, optimized tools. I don't know what college you went to, but my school teaches us to use the tools and resources that are available. You should know how to do it yourself, but no decent professor would advocate reinventing the wheel every time you want to code something. The Java API is a powerful resource, you should use it to the max.
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #13 of 17
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.
Inspiron E1705
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Mobile Core 2 Duo T7200 Dell 0YD479 NVIDIA GeForce Go 7900 GS 256Mb 
OS
Windows XP 
  hide details  
Reply
Inspiron E1705
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Mobile Core 2 Duo T7200 Dell 0YD479 NVIDIA GeForce Go 7900 GS 256Mb 
OS
Windows XP 
  hide details  
Reply
post #14 of 17
Quote:
Originally Posted by stphung View Post
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.
I disagree. Using the API to the max is the foundation of Object Oriented Programing, code reusage. In java, it's just the right way to do things.

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;
}
}
The results tell it all

Quote:
Using built in constructor
Using StringBuffer
Using concatenation
Built in: 16 Buffer: 96 Concat: 13468
Times are in milliseconds with arguments of 100 reps and an array of size 10240 (10KB given 1 byte per char). There are better ways to do it and worse ways to do it. Iterating through the array and appending chars is just not as good (6x worse, in fact). Concatenation is horrible, but given your StringBuffer solution you knew that.
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #15 of 17
Quote:
Originally Posted by rabidgnome229 View Post
I disagree. Using the API to the max is the foundation of Object Oriented Programing, code reusage. In java, it's just the right way to do things.
The point stphung is using is that simply using something isn't enough to understand it. It might be more efficient, but college classes are there to teach, not to produce.

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.
Forge
(13 items)
 
  
CPUMotherboardGraphicsRAM
q6600 Gigabyte GA-P35-DS3L 8800GT 512 Meg 4(2x2)gig DDR2-800 Mushkin 
Hard DriveOptical DriveOSMonitor
WD 7200RPM 750gigs Samsung 20x Windows Vista 64-bit Acer p223w 
KeyboardPowerCaseMouse
Logitech G15 Antec 550Watts Antec 900 mid-tower Logitech mx518 
Mouse Pad
Generic 
  hide details  
Reply
Forge
(13 items)
 
  
CPUMotherboardGraphicsRAM
q6600 Gigabyte GA-P35-DS3L 8800GT 512 Meg 4(2x2)gig DDR2-800 Mushkin 
Hard DriveOptical DriveOSMonitor
WD 7200RPM 750gigs Samsung 20x Windows Vista 64-bit Acer p223w 
KeyboardPowerCaseMouse
Logitech G15 Antec 550Watts Antec 900 mid-tower Logitech mx518 
Mouse Pad
Generic 
  hide details  
Reply
post #16 of 17
Quote:
Originally Posted by Venerence View Post
The point stphung is using is that simply using something isn't enough to understand it. It might be more efficient, but college classes are there to teach, not to produce.

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.
Same here - we had to implement malloc in Intro to Computer Systems. But the entire point of that lab was implementing malloc. In every other lab, however, we were encouraged to use the standard libraries because you can't really be productive if you write every function from scratch every time you write a program. From the wording of the OP, I take it that he is writing a program and part of that program just happens to need a conversion from char[] to String - it's tangential rather than the entire point of the lab. Just because he's learning doesn't mean he should disregard the standard tools. He should focus on the task that the lab intends to teach him. Besides which, teaching him to use the constructor is teaching him good Java coding practice. Writing your own function using a StringBuffer is more work, slower, and requires more resources. It's just plain bad style.

[ot]You don't go to CMU do you? I didn't think malloc lab was that common[/ot]
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #17 of 17
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.
    
CPUMotherboardGraphicsRAM
Q8300 @ 3.0Ghz EP45-DS3L Saphirre 5770 Vapor-X 6 gig A-Data 800mhz (2x2 + 2x1) 
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 60Gig + 4.5TB worth of drives Samsung Windows 7 Ultimate 64-bit 2xAsus VW266H 25.5" 
KeyboardPowerCaseMouse
G110 650W Cooler Master Antec 300 G500 
Mouse Pad
Razer pad 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Q8300 @ 3.0Ghz EP45-DS3L Saphirre 5770 Vapor-X 6 gig A-Data 800mhz (2x2 + 2x1) 
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 60Gig + 4.5TB worth of drives Samsung Windows 7 Ultimate 64-bit 2xAsus VW266H 25.5" 
KeyboardPowerCaseMouse
G110 650W Cooler Master Antec 300 G500 
Mouse Pad
Razer pad 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming