Just to add 2 cents more: here is another shortcut when you have to just use the StringBuffer in-place:
 
String myString = new StringBuffer().append("Something").append("Something Else").toString();
 
This one is best used for things like long dynamic SQL statements, etc. where you don't append in the loop, but still have a fair amount of the String objects to add. 
 
For your case, the StringBuffer sb = new StringBuffer(40000 * 30); already suggested is probably the best choice.
 
Greg
-----Original Message-----
From: Roger Lacroix [mailto:[EMAIL PROTECTED]]
Sent: Sunday, January 19, 2003 7:42 PM
To: JDJList
Subject: [jdjlist] RE: Slow: string += string

Allan / David,

Thank you very much.  I used both ideas and I swear, my code is 50 times faster.  Excellent!!!

later
Roger...

At 06:47 PM 1/19/2003, you wrote:
much better to create a StringBuffer of the required length than use the default constructor.
It looks like you may not know the right length, but estimate high, every time the StringBuffer expands it has to create a new character array and copy the previous contents. It may be worthwhile to iterate through your strings and add up the length beforehand, but probably better to guess high.
 
db
-----Original Message-----
From: Allan Wick [mailto:[EMAIL PROTECTED]]
Sent: Monday, January 20, 2003 10:34 AM
To: JDJList
Subject: [jdjlist] RE: Slow: string += string

Use a StringBuffer.

 
StringBuffer theBuffer = StringBuffer();

 
   for (int r = 0; r < selected.length; r++)          
   {                                                  
            theBuffer.append( rowdata[r] );
        }

 
String output = theBuffer.toString();

 
Something like that...

 
-Al Wick
-----Original Message-----
From: Roger Lacroix [mailto:[EMAIL PROTECTED]]
Sent: Sunday, January 19, 2003 5:20 PM
To: JDJList
Subject: [jdjlist] Slow: string += string

All:

I have my cells of a table defined as Object[][].  In my program, I added the ability for the user to select rows of the table to copy to the system clipboard (i.e. copy / cut / paste functionality).

Depending on what the user is viewing, there could be 400 rows by 2000 columns.
i.e. Object[][] xx = new Object[400][2000];

Say the user selects 20 rows, then I need to combine 40000 cells (20 * 2000).  I have done every trick that I can think of, but it still is not as fast as I would expect.  Right now I combine each row into a string then I combine each row-string each the final string.

Here is my current code:

   String[] rowData = new String[selected.length];    
   for (int r = 0; r < selected.length; r++)          
   {                                                  
      ptr = selected[r];                              
                                                      
      rowData[r] = msgCells[ptr][1].toString();       
      for(int c = 2; c < endColumn; c++)              
      {                                               
         if (msgCells[ptr][c] != null)                
         {                                            
            rowData[r] += msgCells[ptr][c].toString();
         }                                            
      }                                               
   }                                                  
                                                      
   for (int r = 0; r < selected.length; r++)          
   {                                                  
      tempClipboard += rowData[r];                    
   }                                                  

Does anyone out there in Java land have a tip / trick /snippet of code to quickly & efficiently combine a large number of objects into a SINGLE string.

Thanks to all.

later
Roger Lacroix
Enterprise Architect
Capitalware Inc.
____________________________________________________
To change your JDJList options, please visit:
http://www.sys-con.com/java/list.cfm

Be respectful! Clean up your posts before replying
____________________________________________________

____________________________________________________
To change your JDJList options, please visit:
http://www.sys-con.com/java/list.cfm

Be respectful! Clean up your posts before replying
____________________________________________________

____________________________________________________
To change your JDJList options, please visit:
http://www.sys-con.com/java/list.cfm

Be respectful! Clean up your posts before replying
____________________________________________________
____________________________________________________
To change your JDJList options, please visit:
http://www.sys-con.com/java/list.cfm

Be respectful! Clean up your posts before replying
____________________________________________________
____________________________________________________
To change your JDJList options, please visit:
http://www.sys-con.com/java/list.cfm

Be respectful! Clean up your posts before replying
____________________________________________________

Reply via email to