How to convert ArrayList to String[] in java, Arraylist contains VO objects
Asked Answered
S

6

15

Please help me to convert ArrayList to String[]. The ArrayList contains values of type Object(VO).

For example,

The problem is that I need to convert a country List to String Array, sort it and then put it in a list. However I am getting a ClassCastException.

Somite answered 13/7, 2012 at 5:35 Comment(3)
need more description, can you please paste code hereOndine
Properly override the toString() method of your class. And then do as Pramod Kumar has answered. That way no class cast exception. But you should know this is a bad workaround. You should find where your List is getting elements of a type other than Country. This is exactly what Java Generics prevent. So you might want to start using it...Bedesman
I guess other guys got misunderstood of the question. The actual task is under your quotes below? Could you please clarify this? Take a look at my answer plz https://mcmap.net/q/757302/-how-to-convert-arraylist-to-string-in-java-arraylist-contains-vo-objectsAer
O
27
String [] countriesArray = countryList.toArray(new String[countryList.size()]);

I have assumed that your country List name is countryList.

So to convert ArrayList of any class into array use following code. Convert T into the class whose arrays you want to create.

List<T> list = new ArrayList<T>();    
T [] countries = list.toArray(new T[list.size()]);
Opprobrious answered 13/7, 2012 at 5:38 Comment(3)
This also will throw a ArrayStoreException.Aer
It means that your are storing the wrong type of object into an array. You should reveal your codeOpprobrious
It depends on what type the input list is :) I believe it is rather of type Value Object than the String type. You can see this in the question topic.Aer
A
10

Please help me to convert ArrayList to String[], ArrayList Contains Values Object(VO) as Values.

As you mentioned that list contains Values Object i.e. your own class you need toString() overridden to make this work correctly.

This code works. Assuming VO is your Value Object class.

    List<VO> listOfValueObject = new ArrayList<VO>();
    listOfValueObject.add(new VO());
    String[] result = new String[listOfValueObject.size()];
    for (int i = 0; i < listOfValueObject.size(); i++) {
        result[i] = listOfValueObject.get(i).toString();
    }
    Arrays.sort(result);
    List<String> sortedList = Arrays.asList(result);

The snippet of

    List<VO> listOfValueObject = new ArrayList<VO>();
    listOfValueObject.add(new VO());
    String[] countriesArray = listOfValueObject.toArray(new String[listOfValueObject.size()]);

will give you ArrayStoreException due VO is not the String type as required by native method arraycopy subsequently called from toArray one.

Aer answered 13/7, 2012 at 5:50 Comment(0)
B
7

In case your ArrayList contains Strings, you can simply use the toArray method:

String[] array = list.toArray( new String[list.size()] );

If that is not the case (as your question is not completely clear on this), you will have to manually loop over all elements

List<MyRandomObject> list;
String[] array = new String[list.size() ];
for( int i = 0; i < list.size(); i++ ){
  MyRandomObject listElement = list.get(i);
  array[i] = convertObjectToString( listElement );
}
Barrera answered 13/7, 2012 at 5:47 Comment(2)
toArray(new String[...]) will work only if list is of the same String type, won't it? Assuming the question the list is of type ValueObject.Aer
Thank you very much, it helps me a lot to solve my problem, I got exact solution. the code is, List<CountryVO> countryList=new ArrayList<CountryVO>(); CountryVO vo=new CountryVO(); vo.setName("Malaisie"); countryList.add(vo); CountryVO vo1=new CountryVO(); vo1.setName("Allemagne"); countryList.add(vo1); String[] array = new String[countryList.size() ]; for( int i = 0; i < countryList.size(); i++ ){ CountryVO listElement = countryList.get(i); array[i]=listElement.getName(); }Arrays.sort(array); Thanks to AllSomite
S
4
String[] array = list.toArray(new String[list.size()]);

What are we doing here:

  • String[] array is the String array you need to convert your ArrayList to
  • list is your ArrayList of VO objects that you have in hand
  • List#toArray(String[] object) is the method to convert List objects to Array objects
Seisin answered 13/7, 2012 at 5:51 Comment(0)
F
2

As correctly suggested by Viktor, I have edited my snippet.

The is a method in ArrayList(toArray) like:

List<VO> listOfValueObject // is your value object
String[] countries  = new String[listOfValueObject.size()];
for (int i = 0; i < listOfValueObject.size(); i++) {
    countries[i] = listOfValueObject.get(i).toString();
}

Then to sort you have::

Arrays.sort(countries);

Then re-converting to List like ::

List<String> countryList = Arrays.asList(countries);
Flipflop answered 13/7, 2012 at 5:40 Comment(2)
And this will throw a ArrayStoreException too.Aer
@ViktorStolbin: thanks of your suggestion, have edited my snippet, plz checkFlipflop
D
2

Prior to Java 8 we have the option of iterating the list and populating the array, but with Java 8 we have the option of using stream as well. Check the following code:

   //Populate few country objects where Country class stores name of country in field name.
    List<Country> countries  = new ArrayList<>();
    countries.add(new Country("India"));
    countries.add(new Country("USA"));
    countries.add(new Country("Japan"));

    // Iterate over list
    String[] countryArray = new String[countries.size()];
    int index = 0;
    for (Country country : countries) {
        countryArray[index] = country.getName();
        index++;
    }

    // Java 8 has option of streams to get same size array
    String[] stringArrayUsingStream = countries.stream().map(c->c.getName()).toArray(String[]::new);
Dovetail answered 23/6, 2015 at 12:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.