Как преобразовать arraylist в string java
Completing the CAPTCHA proves you are a human and gives you temporary access to the web property.
What can I do to prevent this in the future?
If you are on a personal connection, like at home, you can run an anti-virus scan on your device to make sure it is not infected with malware.
If you are at an office or shared network, you can ask the network administrator to run a scan across the network looking for misconfigured or infected devices.
Another way to prevent getting this page in the future is to use Privacy Pass. You may need to download version 2.0 now from the Chrome Web Store.
Cloudflare Ray ID: 71a8d73039dd9b22 • Your IP : 82.102.23.104 • Performance & security by Cloudflare
Вывести ArrayList в String без появления [,] (скобок)
Хорошо, у меня есть ArrayList, который мне нужно вернуть как String. Сейчас я использую такой подход:
Это нормально работает, но проблема в том, что я заключаю текст в скобки. Я получаю что-то вроде:
Когда я хочу что-то вроде этого:
Однако, в отличие от аналогичных вопросов, я не хочу просто выводить построчно. Мне нужно сохранить ArrayList в строке без скобок, чтобы я мог с ним работать.
РЕДАКТИРОВАТЬ: Следует отметить, что одна запятая в версии, которую я хочу, чтобы она выглядела, была помещена туда методом другого класса:
РЕДАКТИРОВАТЬ 2: мне удалось найти полное решение, адаптировав два полученных мной ответа. Было трудно решить, какой был «ответ», потому что я нашел оба полезными, но выбрал тот, который мог бы использовать более избирательно.
Для снятия скобок я использовал модифицированный возврат. Удаление запятой потребовало от меня сосредоточиться на том факте, что каждая строка будет начинаться с «N», но недостаток этого подхода состоит в том, что код сломается, если я забуду изменить его здесь, если я изменю его там. Тем не менее, это решает мою конкретную проблему, и я всегда могу обозначить себе оба места по мере необходимости.
How to convert ArrayList to String Array in Java
Learn different ways to convert ArrayList to string array in Java with examples.
1. Convert arraylist to array – List.toArray()
This is most basic way to convert an arraylist containing string values to a string array.
2. Convert arraylist to string array – Java 8 stream
If you are using Java 8 or later, you can use stream and collector to get the string array from arraylist.
3. Conclusion
There are other ways also for this conversion from arraylist to string array using libraries such as apache commons and google guava, but those solutions add unnecessary complexity without adding any value. You can skip those solutions and stick to basics.
My preferred way is to use Java 8 streams. Feel free to share what’s your preferred way.