Results 1 to 7 of 7
- 01-16-2011, 12:34 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
- 01-16-2011, 12:39 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
There may be a more efficient way, but I would create a String object set to "", then loop through concatenating each item in the array to my string object.
Java Code:String s = "";
-
Declare a String variable above a for loop, then loop through the array concatenating each item in the array into the String variable mentioned above. By concatenate I mean that you add each String held in the array to the summary String with the += operator. Note that if you have to do this a lot in your program, then you're better off using a SwingBuilder object instead a summary String.
- 01-16-2011, 12:53 AM #4
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
Hmm wait, so if I do something like:
for (int counter = 0; counter < example.length; counter++) {
whatIWant += example[counter];
}
That will combine all the strings?
I completely forgot about the += operator.
-
- 01-16-2011, 07:45 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
A bit better: use a StringBuilder instead of concatenating Strings in a loop. Strings are immuatble so saying a+= b for two String values a and b, creates yet another String and forgets about the previous value of a. Read the API documentation of that class for the details.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
-
Yep, we are in agreement as mentioned in post 3 above.
I try not to push this too hard to newbies as in most of their small apps it smells too much of premature optimization. However if they're doing this sort of call repetitively in a critical portion of their code, then it is absolutely correct.
Similar Threads
-
create a 2d char array from a 1D string array
By jschmall12 in forum New To JavaReplies: 1Last Post: 04-27-2010, 09:01 PM -
String to String Array
By mitty in forum New To JavaReplies: 2Last Post: 04-08-2010, 12:01 AM -
Convert Char Array to String Array
By Mayur in forum New To JavaReplies: 8Last Post: 10-12-2009, 11:41 AM -
string to array
By Drukqs in forum New To JavaReplies: 5Last Post: 04-22-2009, 05:03 AM -
I can't seem to pass the value of a string variable into a string array
By mathias in forum Java AppletsReplies: 1Last Post: 08-03-2007, 10:52 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks