0

Here is my attempt to build a stringBuffer using an array. How can I fix this?

import java.util.ArrayList;
public class StringBufferProj {
public static String appendItems(ArrayList list){
    StringBuffer b = new StringBuffer();
    for (int i=0; i<list.length(); i++) {
        b.append(list(i));
        b.append(" ");
    }
    return b.toString();
    }

    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add("StringBuilder ");
        list.add("executes ");
        list.add("multiple ");
        list.add("threads ");
        list.add("at ");
        list.add("a ");
        list.add("time ");
System.out.println(StringBufferProj.appendItems(list));
    }

}

Thank you for your help.

3
  • 1
    What is the problem that needs assistance? Commented Apr 18, 2017 at 21:20
  • What's the matter with List.toString()? Commented Apr 18, 2017 at 21:22
  • However, List (and ArrayList) have a .size() method, not .length. And list(i) is likely supposed to be something akin to list.get(i) Commented Apr 18, 2017 at 21:25

3 Answers 3

1

First, program to the List interface and don't use raw-types. Also, prefer StringBuilder to StringBuffer and you might use a for-each loop.

public static String appendItems(List<String> list) {
    StringBuilder b = new StringBuilder();
    for (String str : list) {
        b.append(str).append(" ");
    }
    return b.toString();
}

or, in Java 8+, with a stream like

public static String appendItems(List<String> list) {
    return list.stream().collect(Collectors.joining(" "));
}

And then to invoke it, you might use

public static void main(String[] args) {
    List<String> list = new ArrayList<>(Arrays.asList("StringBuilder ", //
            "executes ", "multiple ", "threads ", "at ", "a ", "time "));
    System.out.println(appendItems(list));
}
Sign up to request clarification or add additional context in comments.

Comments

0

You code should be something like this

public static String appendItems(ArrayList<String> list) {
    StringBuffer b = new StringBuffer();
    for (int i = 0; i < list.size(); i++) {
        b.append(list.get(i));
        b.append(" ");
    }
    return b.toString();
}

The changes are
1. Method signature - added type for elements of the list ArrayList<String>
2. In Java list interface doesn't contain length() method but size() method.
3. To get element from the list you should use get method - list.get(i)

Since Java 7 you can use enhanced for loop. I would refactor you code to be something like this

public static String appendItems(ArrayList<String> list) {
    StringBuffer buffer = new StringBuffer();
    for (String word : list) {
        buffer.append(word).append(" ");
    }

    return buffer.toString();
}

And finally, since Java 8 you can use stream and collectors

public static String appendItems(ArrayList<String> list) {
    String result = list.stream().collect(Collectors.joining(" "));

    return result;
    // or just 
    // return list.stream().collect(Collectors.joining(" "));
}

Comments

0

(There are better ways to do this in Java. However, based on your original code) You get an element from the ArrayList using get(). If you also define the type for the ArrayList, then you don't need to cast types and it is much clearer to read:

public static String appendItems(ArrayList<String> list) {
    StringBuffer b = new StringBuffer();
    for (int i = 0; i < list.size(); i++) {
        b.append(list.get(i));
        b.append(" ");
    }
    return b.toString();
}

1 Comment

And size() instead of length().

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.