简体   繁体   English

如何在java中将ArrayList转换为String [],Arraylist包含VO对象

[英]How to convert ArrayList to String[] in java, Arraylist contains VO objects

Please help me to convert ArrayList to String[]. 请帮我转换ArrayList到String []。 The ArrayList contains values of type Object(VO). ArrayList包含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. 但是我得到了ClassCastException。

String [] countriesArray = countryList.toArray(new String[countryList.size()]);

I have assumed that your country List name is countryList . 我假设您的国家/地区列表名称是countryList

So to convert ArrayList of any class into array use following code. 因此,要将任何类的ArrayList转换为数组,请使用以下代码。 Convert T into the class whose arrays you want to create. 将T转换为要创建其数组的类。

List<T> list = new ArrayList<T>();    
T [] countries = list.toArray(new T[list.size()]);

Please help me to convert ArrayList to String[], ArrayList Contains Values Object(VO) as Values. 请帮我转换ArrayList到String [],ArrayList包含值对象(VO)作为值。

As you mentioned that list contains Values Object ie your own class you need toString() overridden to make this work correctly. 正如您所提到的那样,list包含Values Object,即您自己的类,您需要重写String()以使其正常工作。

This code works. 这段代码有效。 Assuming VO is your Value Object class. 假设VO是您的Value Object类。

    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. 将给你ArrayStoreException因为VO不是随后从toArray调用的本机方法arraycopy所需的String类型。

In case your ArrayList contains String s, you can simply use the toArray method: 如果您的ArrayList包含String ,您只需使用toArray方法:

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 );
}
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 String[] array是您需要将ArrayList转换为的String数组
  • list is your ArrayList of VO objects that you have in hand list是您手头的VO对象的ArrayList
  • List#toArray(String[] object) is the method to convert List objects to Array objects List#toArray(String[] object)是将List对象转换为Array对象的方法

As correctly suggested by Viktor, I have edited my snippet. 正如Viktor正确建议的那样,我编辑了我的代码片段。

The is a method in ArrayList(toArray) like: 这是ArrayList(toArray)中的一个方法,如:

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 ::

List<String> countryList = Arrays.asList(countries);

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. 在Java 8之前,我们可以选择迭代列表并填充数组,但是使用Java 8,我们也可以选择使用流。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM