0
import java.util.ArrayList;

public class ConcatString {

public static void main(String[] args) {
        // TODO Auto-generated method stub

    ArrayList list=new ArrayList();

    String[] str={"Hello","World!!!!","Java"};
    for(int i=0;i<str.length;i++)
    {
        list.add(str[i]);
    }
    for(int i=0;i<str.length;i++)
    {
        System.out.print(list.get(i));
    }
}

Is this the right approach since i am new to java? Concat using no inbuiltin functions or + or StringBuffer...Its an interview question

3
  • 2
    Where is the concatenation in your code exactly ? Commented Mar 28, 2014 at 12:57
  • Am just printing it...Can u tell ne how I would concatenate it? Commented Mar 28, 2014 at 12:58
  • What is your goal exactly? Commented Mar 28, 2014 at 13:00

6 Answers 6

3

If your string array is large, you want to use StringBuilder, because using the += string concatenation is inefficient due to Java String immutability.

String[] str={"Hello","World!!!!","Java"};
StringBuilder sb = new StringBuilder();
for(String s : str)
{
    sb.append(s);
}
System.out.println(sb.toString());
Sign up to request clarification or add additional context in comments.

2 Comments

the subject says without the use of StringBuffer. I assume it also covers StringBuilder.
@klarki, this answer was posted before the question was edited.
1

Like this :

    String[] str={"Hello","World!!!!","Java"};
    String concat ="";
    for(String s : str)
    {
        concat+=s;
    }
    System.out.println(concat);

Comments

0

I assume you can't use StringBuffer and you can't use StringBuilder either, as this wouldn't make sense otherwise.

You can concat two Strings after converting them to a char array:

public String concat(String[] args){
    int length = 0;
    for (String s : args) {
        length += s.length();
    }
    char[] result = new char[length];

    int i = 0;

    for (String s : args) {
        s.getChars(0, s.length(), result, i);
        i += s.length();
    }

    return new String(result);
}

Here's how you test it:

public void testConcat() {
    String[] s = new String[3];
    s[0] = "abc";
    s[1] = "def";
    s[2] = "ghi";
    System.out.print(concat(s));

    /*
     * Output: abcdefghi
     */
}

Comments

0

What is your target?.Here you simply copies the values from string array and stored it in an ArrayList.Where the code for concatination.To string concatination in java just use + operator.Don't go for complicated logic , if you have simple alternatives.

Comments

-1

You could use one simple one of the following

String[] str = {"Hello","World!!!!","Java"};
String concat ="";
for(String s : str)
{
    concat+=s;
}
System.out.println(concat);

Or using StringBuilder which is more efficient like this:

String[] str={"Hello","World!!!!","Java"};
StringBuilder sb = new StringBuilder();
for(String s : str)
{
    sb.append(s);
}
System.out.println(sb.toString());

Comments

-1

You dont need Arraylist for concatenation. Try the below approach

    String[] str={"Hello","World!!!!","Java"};
    StringBuilder finalString = new StringBuilder();
    for(int i=0;i<str.length;i++)
    {
        finalString.append(str[i]);
    }
    System.out.println(finalString);

1 Comment

It was asked in an inteview(to concat a string without w/o concat() or + or SB append)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.