1

I have a problem with ArrayList in Java. I writing module for get all files from directory. But, i don't know how to print ALL elements from Array in ONE message. I mean:

File1
File2
File3
File4
File5
etc...

Print message should print new object in new line. No new print.

So, i think you know what i mean. I really don't know how to good explain it. Hope you can help me :D

If you have any questions, please comments :)

Also sorry for my bad english knowledge. Just learning it right now

Thanks.

2
  • Can you show us your current code? Do you want all the output to be printed on one line, or print all lines with one line of code? Commented Jul 1, 2020 at 22:13
  • Please be more specific for a more beneficial answer. I will update when you provide more context. Commented Jul 1, 2020 at 22:20

4 Answers 4

3

You can use ArrayList#forEach to print each element of the list on a separate line. See the below code in action here.

list.forEach(System.out::println);

To combine all the objects as one String, you can append them all to a StringBuilder, as long as the object overrides the toString method. See the below code in action here.

final StringBuilder sb = new StringBuilder();
list.forEach(el->sb.append(el + System.lineSeparator()));
final String result = sb.toString();
System.out.print(result);

For Lists of Strings, you can use the simpler String.join(delimiter, list).

final String result = String.join(System.lineSeparator(), list);
System.out.println(result);
Sign up to request clarification or add additional context in comments.

10 Comments

This prints each one out separately. -1
@BenjaminW. The question is unclear in that aspect. For one thing, it says "Print message should print new object in new line."
"...how to print ALL elements from Array in ONE message."
@BenjaminW. Parts of the question are equivocal.
I agree, but not the part that it wants to print them all at once. Such as previously mentioned and "No new print."
|
1

You can use a for loop to cycle through all elements of the arrayList and print them

Comments

1

Just assemble the elements into an object to print out at the same time. You weren't too specific about data type or what you are exactly doing so take this as more of a theoretical answer.

ArrayList arrayOfWhatever;
WhateverObject combinedWhatever;
for(Whatever whatever : arrayOfWhatever){
    //put or append or add or whatever your file type is. 
    combined.add(whatever);
}
System.out.println(combinedWhatever);

If truly a string, you want to use a String Builder

ArrayList<String> strings;
StringBuilder combined;
for(String element : strings){
    combined.append(element)
}
System.out.println(combined.toString())

https://docs.oracle.com/javase/tutorial/java/data/buffers.html

Comments

1

Stream with Collectors.joining

You may join all strings in the input list together with "\n" thus any element will be printed on its line by using Collectors.joining.

public void printList(List<String> input) {
    if (null != input) {
        System.out.println(input.stream().collect(Collectors.joining("\n")));
    }
}

Example usage.

    String output = printList(List.of("File1", "File2", "File3", "File4", "File5" ));
File1
File2
File3
File4
File5

Comments

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.