如何在另一个方法中使用一个方法返回的多个值

时间:2014-05-06 11:38:40

标签: java methods return

我遇到了麻烦(在Java中)试图弄清楚如何使用从一个方法返回的变量然后用这些返回的变量调用另一个方法。这就是我所拥有的:

public static void main(String[] args) {
    sortFile(args[0]); // this returns 3 variables, nWords, nSyllables, nSentences
    getFRE(nWords, nSyllables, nSentences); // these "cannot be resolved to a variable"
}

我想做的不可能吗?我相信这很简单。感谢

11 个答案:

答案 0 :(得分:3)

您应该将返回的值分配给局部变量,然后在下一个方法中使用它。

答案 1 :(得分:2)

方法1:

您还可以查看可能符合您需求的JavaTuples library,并提供一个非常优雅的解决方案。使用Triplet

有关此问题的更多详情,请访问here

方法2:

另一种方法是将所有三个值封装在包装类的单个对象下,如其他人所建议的那样。然后作为第一个方法调用的结果返回它。以后哪个可以作为单个参数传递给第二个方法,如:

<?php
$xd1 = simplexml_load_string('<xml version="1.0">
<ServiceResponse>
<ServiceHotel>
<BookingStatus>CANCELLED</BookingStatus>
</ServiceHotel>
<ServiceHotel>
<BookingStatus>CANCEfLLED</BookingStatus>
</ServiceHotel>
</ServiceResponse>
</xml>');

$a = $xd1->ServiceResponse->ServiceHotel[0]->BookingStatus;
$b = $xd1->ServiceResponse->ServiceHotel[1]->BookingStatus;

var_dump($xd1);
echo '<br><br>';

var_dump($a);
echo '<br><br>';

var_dump($b);
echo '<br><br>';

    if ($a == $b):
      echo 'Equal';
    else:
      echo 'Not Equal';
    endif;

现在可以使用上述类的对象(需要更改第二个方法定义):

// assuming all three variables are integers for example
// can be used for other variable types too

    private class valueHolder{
          private final int nWords,nSyllables, nSentences;
          public valueHolder(int nWords, int nSyllables, int nSentences){
                  this.nWords = nWords;
                  this.nSyllables = nSyllables;
                  this.nSentences = nSentences;
          }
           public final int getNWords(){
                 return nWords;
           }
           public final int getNSentences(){
                 return nSentences;
           }
           public final int getNSyllables(){
                 return nSyllables;
           }
    }

问题here

中的更多细节

答案 2 :(得分:1)

您的情况下的问题是您希望从 sortFile()返回三个值,但是您不能从返回int的方法返回多个值。    解决此问题的一种方法是,不是从 sortFile()返回一个int,而是返回一个包含这三个值的int数组,

public static void main(String[] args) {
    int [] myArray = sortFile(args[0]); // this returns an int array with 3 variables, nWords, nSyllables, nSentences
    getFRE(myArray); 
}


您还需要更改 getFre(),以便它接受一个数组作为参数 但是,正如其他人提到的那样,将值包装在类中是另一种选择:

 public class Words {

    // declare variables;
    private int nWords, nSyllables, nSentences;


    // getters and setters
    public void setnWords(int nWords) {
        this.nWords = nWords;
    }

    public void setnSyllables(int nSyllables) {
        this.nSyllables = nSyllables;
    }

    public void setnSentences(int nSentences) {
        this.nSentences = nSentences;
    }



    public int getnWords() {
        return nWords;
    }

    public int getnSyllables() {
        return nSyllables;
    }

    public int getnSentences() {
        return nSentences;
    }
}


使 sortFile()返回单词的实例,并使 getFre()接受单词的实例>作为参数。你的主要课程看起来像:

public static void main(String[] args) {
       Words myWords = sortFile(args[0]); // this returns an instance of the Words class
        getFRE(myWords); 
    }

答案 3 :(得分:0)

方法只能返回单个对象。

如果要返回多个值,可以创建一个具有每个值的属性的类。

答案 4 :(得分:0)

您的代码存在两个主要问题:

  1. 您不能从方法返回多个值 - 如果您需要全部三个,您可能希望将它们嵌入到类中,并返回该类类型的对象

  2. 当一个方法返回一个变量时,它不会只出现在你的函数中 - 而是需要使用诸如Object o = foo(bar)之类的行来存储你的值并随后使用它。

答案 5 :(得分:0)

在Java方法中返回单个varible/object。您可以将三个字符串变量放在Array中并返回array。或者您可以使用相同的properties返回A对象。

public String[] sortFile(args[0]){

String[] array;
//your logic goes here
  return array
}

答案 6 :(得分:0)

首先,您需要检查 sortFile(args [0])方法的返回类型,并将返回值存储在相同的数据类型中(任何方法只能返回一个值或者对象at一段时间但返回的Object可以在其​​中保存多个值。)。因此使用适当的数据结构(如数组列表 ..等作为返回类型)或任何其他对象存储这三个值并从 sortFile(args [0])函数返回。最后在下一个方法调用 getFRE(returnedObject)

中传递该变量

答案 7 :(得分:0)

在您的代码中返回的变量,您的方法不是任何地方的存储 因此,使用非现有变量调用其他方法会尝试在一个结构中重新组合一个集合或类,并使您的方法返回该类型的结构示例:

public class ReturnedWords{

   int nWords,Sylables ,nSentences;


public int getW(){

 return nWords;
}
 public int getS(){

 return nSylabelles;
}

public int getSe(){



return nWords;
} //yyou get it by now and  then do



public static void main(String[] args){

ReturnedWords r = sort(arg[0]);

getFRE(r.getN(),r.getS(),r.getSe());

}



}

答案 8 :(得分:0)

其他人提到的方法肯定会奏效。

另一种方法是,您也可以使用静态变量,如下例

所示
public class Test
{
    static int i=0;
    static int j=0;
    static int k=0;

    public static void main(String[] args) {
        update();
        System.out.println(i);
        System.out.println(j);
        System.out.println(k);          
    }

    public static void  update() {
        i++;
        j++;
        k++;
    }
}

答案 9 :(得分:0)

如果您的所有代码都在一个类文件中,请将局部变量 nWords nSyllables nSentences 转换为全局变量。因此,从两种方法中都可以看到它们。

static int nWords, nSyllables, nSentences;

public static void sortFile(String fileName) {
    //do not declare nWords, nSyllables, nSentences here, just modify them, as they are global
    //rest of the code goes normally
}

public static void getFRE() { //no need for arguments, as they are now global
    //no change, just set nWords, nSyllables, nSentences as needed
}

public static void main(String[] args) {
    sortFile(args[0]); //will set the global variables
    getFRE(); //will get the global variables
}

你可能会认为我提议的是一种不好的做法(而且它是!),但是,由于你的计算程序似乎不是一个不断发展的项目,它将使它以最小的干预工作。

答案 10 :(得分:0)

您可以将地图返回为:

Map<String,Integer> nMap = new HashMap<String, Integer>();
nMap.put("nWords", 1);
nMap.put("nSyllables", 1);
nMap.put("nSentences", 1);

来自sortFile() mehod

Map<String,Integer> nMap = sortFile(args[0]);

可以用作:

getFRE(nMap); // or
getFRE(nMap.get("nWords"), nMap.get("nSyllables"), nMap.get("nSentences"));

取决于您的getFRE()方法实施

相关问题