Difference between revisions of "Concatenating arrays"
From CompSciWiki
(spacing consistency, grammar, punct.) |
(Changed to utilize CodeBlock and OutputBlock templates) |
||
(One intermediate revision by one other user not shown) | |||
Line 4: | Line 4: | ||
Example:<br> | Example:<br> | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
String[] strings = { "First ", | String[] strings = { "First ", | ||
"Second ", | "Second ", | ||
"Third" }; | "Third" }; | ||
− | + | }} | |
Would have output: | Would have output: | ||
− | + | {{OutputBlock | |
+ | |Code= | ||
"First Second Third" | "First Second Third" | ||
− | + | }} | |
|SideSectionTitle=More with Arrays | |SideSectionTitle=More with Arrays | ||
|SideSection= | |SideSection= | ||
− | [[Image:Wiki_method01.jpg|center]] | + | [[Image:Wiki_method01.jpg|center]]<BR> |
− | <BR> | + | |
|Solution= | |Solution= | ||
There are multiple ways of joining strings together. The simplest way is to use the plus symbol, which you are probably familiar with. | There are multiple ways of joining strings together. The simplest way is to use the plus symbol, which you are probably familiar with. | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
for( int i = 0; i < strings.length; i++ ) | for( int i = 0; i < strings.length; i++ ) | ||
{ | { | ||
Line 27: | Line 29: | ||
} | } | ||
System.out.println( "1. " + tempString1 ); | System.out.println( "1. " + tempString1 ); | ||
− | + | }} | |
The String class also offers the method concat() to concatenate strings. | The String class also offers the method concat() to concatenate strings. | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
for( int i = 0; i < strings.length; i++ ) | for( int i = 0; i < strings.length; i++ ) | ||
{ | { | ||
Line 37: | Line 40: | ||
} | } | ||
System.out.println( "2. " + tempString2 ); | System.out.println( "2. " + tempString2 ); | ||
− | + | }} | |
Java also offers the StringBuilder class which is best used when building up larger strings. | Java also offers the StringBuilder class which is best used when building up larger strings. | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
for( int i = 0; i < strings.length; i++ ) | for( int i = 0; i < strings.length; i++ ) | ||
{ | { | ||
Line 49: | Line 53: | ||
System.out.println( "3. " + tempString3 ); | System.out.println( "3. " + tempString3 ); | ||
− | + | }} | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
Latest revision as of 16:31, 4 December 2011
Back to the Program-A-Day homepage
ProblemJoin an array of strings together to create a single string. Example: String[] strings = { "First ", "Second ", "Third" }; Would have output: "First Second Third" |
More with Arrays | |
---|---|---|
SolutionThere are multiple ways of joining strings together. The simplest way is to use the plus symbol, which you are probably familiar with. for( int i = 0; i < strings.length; i++ ) { tempString1 = tempString1 + strings[i]; } System.out.println( "1. " + tempString1 );
for( int i = 0; i < strings.length; i++ ) { tempString2 = tempString2.concat( strings[i] ); } System.out.println( "2. " + tempString2 );
for( int i = 0; i < strings.length; i++ ) { sb.append( strings[i] ); } tempString3 = sb.toString(); System.out.println( "3. " + tempString3 ); | ||
Code |