繁体   English   中英

在Java方法中返回不同的数据类型

[英]Return different datatypes in a method in java

我在Java中有一个非常愚蠢的问题。 我有一个main方法,该方法依次调用传递一个字符串值的方法。 但作为回报,我需要获取3个字符串值。 有人可以帮忙吗?

在main方法中,我只传递一个字符串值,它必须返回3个字符串,或者有时可能是2个字符串和一个int。

public static void main(String[] args) {
        List <String> ls= cusmethod("string1");
        ls.get(0); //get string A
}

Private static string cusmethod(String test)
{
    String A = "A"+test;
    String B = "B"+test;
    String C = "C"+test;

    // Need to return all these 3 string to main method.
    return A , B, C;
}

该方法应将3个字符串值返回给main方法。 请告知如何实现此目标。 我不确定如何将这些字符串返回到main方法以及如何在main中检索这些字符串

尝试在辅助方法中返回列表:

private static List<String> cusmethod(String test) {
    List<String> list = new ArrayList<>();
    list.add("A" + test);
    list.add("B" + test);
    list.add("C" + test);

    return list;
}

您不能在Java中返回多个值。 您可以将结果包装在一个类中并返回该类。

如果需要返回相同类型的n个值,则可以创建一个List或一个数组并将其返回。

您可以创建一个包含这3个字符串的类并返回该类,例如...

class String3 {
    private String res1;
    private String res2;
    private String res3;
    public String3(String res1, String res2, String res3) {
        super();
        this.res1 = res1;
        this.res2 = res2;
        this.res3 = res3;
    }
    public String getRes1() {
        return res1;
    }
    public String getRes2() {
        return res2;
    }
    public String getRes3() {
        return res3;
    }
}

然后,您可以像这样使用Custom类。

private String3 method(String test) {
    String a = "A"+test;
    String b = "B"+test;
    String c = "C"+test;

    // Need to return all these 3 string to main method.
    return new String3(a, b, c);    
}

您可以使用vavr之类的库来返回元组

Tuple3<String, String, String> result = Tuple.of(A, B, C);

最好的选择是重新评估代码,以了解如何在Java中执行此操作。

您正在尝试使用Java惯用的解决方案,应该尝试查看如何以Java惯用的方式分解问题,而不是尝试应用其他语言的典型解决方案,从长远来看,它将带来回报。

它必须返回3个字符串,或者有时可能是2个字符串和1个整数

在Java中,您可以使用Object类型来表示任何对象,例如:

public void foo(Object obj){
    if(obj instanceof String){  // obj is a String
        System.out.println("Hello, " + (String) obj);
    }
    else if(obj instanceof Integer) {  // obj is an integer value
        // Integer is an object analog of int, because int is a primitive type
        int my_int = (Integer) obj;  // but we can implicitly convert Integer to int
        System.out.println("I doubled your integer: " + my_int * 2);
    }

}

// somewhere
foo("Steve");  // Hello, Steve
foo(2);  // I doubled your integer: 4

因此,在您的情况下,您可以返回对象列表:

private static List<Object> cusmethod(String test)
{
    List<String> list = new ArrayList<>();
    if(test.equals("return 3 strings")){
        list.add("A" + test);
        list.add("B" + test);
        list.add("C" + test);
    }
    else if(test.equals("return 2 strings and an int")){
        list.add("A" + test);
        list.add("B" + test);
        list.add(12345);
    }
    return list;
}

public static void main(String[] args) {
    List <Object> ls = cusmethod("string1");
    String s = (String) ls.get(0); //get string A
    int x = (Integer) ls.get(2);
}

暂无
暂无

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

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