简体   繁体   English

有没有办法从 return 语句打印 toString 而不覆盖 toString 方法?

[英]Is there a way to print a toString from a return statement without overriding a toString method?

Is there a smart and quick way for me to output the string "h" in this code.在这段代码中,是否有一种智能且快速的方法可以输出字符串“h”。 I'm curious to see if there is some kind of function that can quickly convert the return statement return h.toString();很好奇有没有什么函数可以快速转换return语句return h.toString(); to an actual output for me.到我的实际输出。

import java.util.ArrayList;

public class Ex1 {
    public static void main(String[] args) {

        a("4 5 1 34 123 1 5 8");

    }
    public static String a(String b) {
        ArrayList<String> c = new ArrayList<>();
        String[] d = b.split (" ");
        int e = 0;
        if(d != null && d.length > 0) {
            for(; e < d.length ; e = e + 1) {
                c.add(0, d[e]);
            }
        }

        Object[] f = c.toArray();
        String[] g = new String[e];
        for(e = 0; e < f.length ; e=e+1) {
            g[e] = f[e].toString();
        }
        String h = "";
        for(String i : g) {
            h = h.toString() + " " + i.toString();
        }
    return h.toString();
    }
}

If you don't mind the json format, Simply use any json lib will do this quick, I believe every json lib has a way to turn a object to json string如果您不介意 json 格式,只需使用任何 json lib 即可快速完成此操作,我相信每个 json lib 都有将对象转换为 json 字符串的方法

like喜欢

System.out.println(JSONArray.fromObject(myArray).toString());

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

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