So i'm wondering if its possible to populate a String array made from toString() calls of an object ArrayList
So I know this can be done using a loop but is there a one line approach?
Loop approach
ArrayList<Object> objects = new ArrayList<Object>();
//fill object with elements
//
String[] strings = new String[object.length()];
for(int i = 0;i<10;i++)strings[i]=objects.get(i).toString();
Here, we have used the toString() method to convert the arraylist into a string. The method converts the entire arraylist into a single String . Note: The ArrayList class does not have its own toString() method. Rather it overrides the method from the Object class.
The toString method returns a string representation of an object.
Below are the various methods to convert an Array to String in Java: Arrays. toString() method: Arrays. toString() method is used to return a string representation of the contents of the specified array.
Using java-8,
String[] strings = objects.stream().map(Object::toString).toArray(String[]::new);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With