java - What is more costly? Instantiating an Advanced Data Structure or performing a couple of loops (one after the other O(n)) and using arrays? -


i tasked create algorithm solve given problem instructed give optimal solution regards time , memory. given array of strings. want characters of strings of array in char[].

my question (especially have dealt code , performance optimisation) should use list<char> manually destroy object , copy contents char[] or do manually , working arrays (which unsure of how do.). cost of instantiating list<> object greater looping through string[], converting each string char[] , them merging char[] arrays together?

edit:

okay, let me more precise then. better convert strings of string[] list<> or other abstract data structure , work or following: looping through string[], converting each string char[] , them merging char[] arrays together?

if need dpulicates appending of strings together. in case why not use string buffer.

stringbuilder sb = new stringbuilder(); (string s : mystringarray){     sb.append(s); } char chars[] = sb.tostring().tochararray(); 

the stringbuilder can created initial capacity avoid cost of dynamic resizing. won't fast allocating char[] , directly inserting chars should fast enough uses. alternative check out charbuffer, backed char[].


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -