I think that last line should be System.arrayCopy( s1Arr, 0, complete, sArr.length, s1Arr.length ); Other than that, I think you've hit on the best reasonably simple way to do this. If you determine that you really need to improve performance, it may be possible to replace it with something quicker. Use a profiler to find out where the bottleneck is currently. I'm guessing the split() method could be improved - since it accepts a regex pattern as an argument (which may be arbitrarily complex), it may not be optimized for the fastest possible search for something nice and simple like ",". You could hand-make something that loops through the comma-delimited string using indexOf(',', lastCommaPosition). You'd probably want to go through the sting twice - once just to count commas (so you can correctly size the complete array), and once to load the values in. You could also use StringTokenizer to do this; I'm guessing you could make a slightly faster version yourself, but a StingTokenizer version would be worth looking at for comparison.
"I'm not back." - Bill Harding, Twister
Joined: Jul 28, 2002
yeah, my last line I had wrong but I caught that in time. Thanks! I thought about looping through the string - do you really think that's faster than arraycopy? I never would have thought that. Especially since I do have to loop through twice. It just all seems so wasteful and inelegant. I was thinking, there just has to be a better way.
Joined: Jan 30, 2000
Well, you don't have to loop through twice. The alternative is to use an ArrayList to store the tokens as you find them. That's what the split() method does internally. I'm not really sure which is faster in this case - I think they might have used the ArrayList solution inside split() because they didn't know how complex the pattern might be. Since a really complex pattern might be really slow, they decided to minimize the risk and only loop once. Paying for it with the slightly slower access methods of an ArrayList vs an array, plus an extra array copy. (To go from the internal array of ArrayList to the exactly-sized array returned by split()). For a really simple pattern it might be quicker to loop twice and get the array size just right in the first place. Just a guess; could be way off. In any event, I don't think the main possible slowness of the first method is in the System.arraycopy() - I think it's inside split(). And if you replace split() correctly you may not need another arraycopy() after anyway. [ January 30, 2003: Message edited by: Jim Yingst ]
subject: Is there a faster / less memory-intensive way of doing this?