简体   繁体   中英

Is it possible to convert CustomObject[] to String[] without iteration?

I have a custom object:

public class Cat {
    private String mName;
    private int mAge;

    public Cat(String name, int age) {
        mName = name;
        mAge = age;
    }

    @Override
    public String toString() {
        return mName;
    }
}

I need to get String[] with names from Cat[]. It it possible? How to get it?

As assylias wrote in the comments - you will iterate it one way or another, that said, if you don't want to explicitly iterate it - you can use Arrays.toString :

Cat[] catArray = ...
String catArrayStr = Arrays.toString(catArray);

考虑使用延迟评估的Google Guava的transform ,因此在使用时仅迭代一次。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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