How an object array can be converted to string array in java

0 votes

 I use the following code to convert an Object array to a String array :

Object Object_Arr[]=new Obj[100];


String String_Arr[]=new String[Object_Arr.length];

for (int i=0;i<String_Arr.length;i++) String_Arr[i]=Object_Arr[i].toString();

But I wonder if there is another way to do this?

Jun 8, 2018 in Java by Daisy
• 8,120 points
4,691 views

2 answers to this question.

0 votes

Another alternative could be:

String[] stringArr = Arrays.copyOf(objectArr, objectArr.length, String[].class);
answered Jun 8, 2018 by sophia
• 1,400 points
0 votes
System.arraycopy is the most efficient way, but for aesthetics, I would prefer
Arrays.asList(Object_Array).toArray(new String[Object_Array.length]);
answered Aug 8, 2018 by Sushmita
• 6,910 points

Related Questions In Java

0 votes
3 answers

How can I add new elements to an Array in Java

String[] source = new String[] { "a", ...READ MORE

answered Sep 19, 2018 in Java by Sushmita
• 6,910 points
11,347 views
0 votes
2 answers
+16 votes
25 answers

How can I convert String to JSON object in Java?

Hi @Daisy You can use Google gson  for more ...READ MORE

answered Feb 7, 2019 in Java by Suresh
• 720 points
250,266 views
0 votes
2 answers

How to convert an int array to string using tostring method in java?

Use java.util.Arrays: String res = Arrays.toString(array); System. ...READ MORE

answered Aug 16, 2019 in Java by Sirajul
• 59,230 points
2,124 views
0 votes
1 answer

How can two strings be concatenated in java?

You can concatenate Strings using the + operator: System.out.println("Your number ...READ MORE

answered Jun 6, 2018 in Java by Daisy
• 8,120 points
621 views
0 votes
1 answer

Why is char[] preferred over a string?

Strings are immutable. That means once you've ...READ MORE

answered Jun 14, 2018 in Java by scarlett
• 1,290 points
442 views
0 votes
1 answer

Can quotes be added in a java string.how?

In Java, you can escape quotes with \: String ...READ MORE

answered Jun 14, 2018 in Java by Akrati
• 960 points
442 views
0 votes
2 answers

How to convert a java string into byte array

Try using String.getBytes(). It returns a byte[] ...READ MORE

answered Aug 22, 2019 in Java by Sirajul
• 59,230 points
1,145 views
0 votes
1 answer

How to print java array in the simplest way?

String[] arr = new String[] {"John", "Mary", ...READ MORE

answered Apr 17, 2018 in Java by sophia
• 1,400 points
626 views
0 votes
1 answer

How to initialize multi-dimensional array in java?

A multidimensional array in Java is really an array ...READ MORE

answered Jun 6, 2018 in Java by sophia
• 1,400 points
754 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP