如何返回多个值? [重复]

时间:2022-02-04 02:00:45

Possible Duplicate:
How to return multiple objects from a Java method?

可能重复:如何从Java方法返回多个对象?

Is it possible to return two or more values from a method to main in Java? If so, how it is possible and if not how can we do?

是否可以在Java中将方法中的两个或多个值返回到main?如果是这样,如何可能,如果不可能,我们该怎么做?

4 个解决方案

#1


62  

You can return an object of a Class in Java.

您可以在Java中返回Class的对象。

If you are returning more than 1 value that are related, then it makes sense to encapsulate them into a class and then return an object of that clas.

如果要返回多个相关的值,则将它们封装到类中然后返回该clas的对象是有意义的。

If you want to return unrelated values then you can use java's built-in container classes like Map, List, Set etc. Check the java.util package's JavaDoc for more details.

如果要返回不相关的值,则可以使用java的内置容器类,如Map,List,Set等。检查java.util包的JavaDoc以获取更多详细信息。

#2


28  

You can do something like this:

你可以这样做:

public class Example
{
    public String name;
    public String location;

    public String[] getExample()
    {
        String ar[] = new String[2];
        ar[0]= name;
        ar[1] =  location;
        return ar; //returning two values at once
    }
}

#3


26  

You can only return one value, but it can be an object that has multiple fields - ie a "value object". Eg

您只能返回一个值,但它可以是具有多个字段的对象 - 即“值对象”。例如

public class MyResult {
    int returnCode;
    String errorMessage;
    // etc
}

public MyResult someMethod() {
    // impl here
}

#4


-3  

Yes you can retrieve multiple value but you should combine the value within the ArrayLists. I will show you the example , and take a look on the code :

是的,您可以检索多个值,但您应该组合ArrayLists中的值。我将向您展示示例,并查看代码:

class mahasiswa{
    public String nama;
    public String nrp;
    public String sks;
    public String namakul; 

    public mahasiswa(String nm, String ps, String sk, String nmkul){
        this.nama = nm;
        this.nrp =ps;
        this.sks = sk;
        this.namakul = nmkul;
    }

    public String getsks(){
        return sks;
    }

    public String getnamakul(){
        return namakul;
    }

    public String getnama(){
        return nama;
    }

    public String getnrp(){
        return nrp;
    }
}

ArrayList<mahasiswa> myObject; 

public myArray() {
    initComponents();
    myObject = new ArrayList<mahasiswa>();

    for(int i =0;i<myObject.size();i++){
        jTextArea1.append("Nama" + " = " + myObject.get(i).getnama() + "\n" + "NRP "  + " = " + myObject.get(i).getnrp() + "\n" + "Nama Matakuliah  = " + myObject.get(i).getnamakul() + "\n" + "SKS = " + myObject.get(i).getsks() + "\n");
    }         
 }

#1


62  

You can return an object of a Class in Java.

您可以在Java中返回Class的对象。

If you are returning more than 1 value that are related, then it makes sense to encapsulate them into a class and then return an object of that clas.

如果要返回多个相关的值,则将它们封装到类中然后返回该clas的对象是有意义的。

If you want to return unrelated values then you can use java's built-in container classes like Map, List, Set etc. Check the java.util package's JavaDoc for more details.

如果要返回不相关的值,则可以使用java的内置容器类,如Map,List,Set等。检查java.util包的JavaDoc以获取更多详细信息。

#2


28  

You can do something like this:

你可以这样做:

public class Example
{
    public String name;
    public String location;

    public String[] getExample()
    {
        String ar[] = new String[2];
        ar[0]= name;
        ar[1] =  location;
        return ar; //returning two values at once
    }
}

#3


26  

You can only return one value, but it can be an object that has multiple fields - ie a "value object". Eg

您只能返回一个值,但它可以是具有多个字段的对象 - 即“值对象”。例如

public class MyResult {
    int returnCode;
    String errorMessage;
    // etc
}

public MyResult someMethod() {
    // impl here
}

#4


-3  

Yes you can retrieve multiple value but you should combine the value within the ArrayLists. I will show you the example , and take a look on the code :

是的,您可以检索多个值,但您应该组合ArrayLists中的值。我将向您展示示例,并查看代码:

class mahasiswa{
    public String nama;
    public String nrp;
    public String sks;
    public String namakul; 

    public mahasiswa(String nm, String ps, String sk, String nmkul){
        this.nama = nm;
        this.nrp =ps;
        this.sks = sk;
        this.namakul = nmkul;
    }

    public String getsks(){
        return sks;
    }

    public String getnamakul(){
        return namakul;
    }

    public String getnama(){
        return nama;
    }

    public String getnrp(){
        return nrp;
    }
}

ArrayList<mahasiswa> myObject; 

public myArray() {
    initComponents();
    myObject = new ArrayList<mahasiswa>();

    for(int i =0;i<myObject.size();i++){
        jTextArea1.append("Nama" + " = " + myObject.get(i).getnama() + "\n" + "NRP "  + " = " + myObject.get(i).getnrp() + "\n" + "Nama Matakuliah  = " + myObject.get(i).getnamakul() + "\n" + "SKS = " + myObject.get(i).getsks() + "\n");
    }         
 }