[英]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[]. 我需要使用Cat []的名称获取String []。 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 : 作为assylias在评论中写道-你会遍历它这种或那种方式,也就是说,如果你不想重复明确它-你可以使用Arrays.toString :
Cat[] catArray = ...
String catArrayStr = Arrays.toString(catArray);
考虑使用延迟评估的Google Guava的transform
,因此在使用时仅迭代一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.