简体   繁体   English

是否可以将CustomObject []转换为String []而无需迭代?

[英]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.

相关问题 转换列表 <CustomObject> 到了json - convert List<CustomObject> to json java DTO中将日期类型转换为字符串格式,无需迭代 - java Convert Date type to String format in DTO without Iteration Android / FireStore QuerySnapshot转换为CustomObject - Android/FireStore QuerySnapshot convert to CustomObject 可以使用迭代在Java中生成字符串吗? - Possible to use iteration to produce a string in Java? 是否可以将 OutStream 转换为字符串? - Is it possible to convert OutStream to a string? 你能转换一个Java Map吗? <Long,CustomObject> 到byte []然后回到Map <Long,CustomObject> ? - Can you convert a Java Map<Long,CustomObject> to byte[] then back to Map<Long,CustomObject>? 是否可以在没有迭代的情况下加载具有所有惰性属性的对象? - Is it possible to load object with all lazy properties without a iteration? 一种转换 List 的有效方法<Integer>无需迭代即可转换为 int[] ( array ) - An efficient way to convert List<Integer> to int[] ( array ) without iteration Spark Streaming 转换数据集<row>到数据集<customobject>在 java</customobject></row> - Spark Streaming Convert Dataset<Row> to Dataset<CustomObject> in java 有没有办法发送地图 <String, CustomObject> 通过休息服务? - Is there a way to send Map<String, CustomObject> via Rest service?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM