【问题标题】:How to use multiple values returned by one method in another method如何在另一种方法中使用一种方法返回的多个值
【发布时间】:2014-06-23 00:09:56
【问题描述】:

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

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"
}

我试图做的事情是不可能的吗?我敢肯定这很简单。谢谢

【问题讨论】:

  • 你能说说语言吗?
  • 一个方法只能返回一个值,标记语言
  • 我们能看到sortFile的代码吗?通常,要保存方法调用的结果,您必须将其分配给本地范围内的变量:int words = sortFile(...);。但是,您不能轻易地从方法中返回三个变量。您可能想使用一个类来存储结果。
  • 这是sortFile的代码link
  • 你介意结束这个问题吗?

标签: java methods return


【解决方案1】:

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

【讨论】:

  • @Mark Deleuran 这如何适用于多个值?或者您是否建议使用多个字段?
【解决方案2】:

一个方法只能返回一个对象。

如果你想返回多个值,你可以为每个值创建一个属性。

【讨论】:

    【解决方案3】:

    您的代码有两个主要问题:

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

    2. 1234563 /li>

    【讨论】:

      【解决方案4】:

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

      喜欢

      public String[] sortFile(args[0]){
      
      String[] array;
      //your logic goes here
        return array
      }
      

      【讨论】:

        【解决方案5】:

        您的问题是您想从 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() 返回 Words 的实例,并使您的 getFre() 接受 Words 的实例> 作为参数。您的主要课程如下所示:

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

        【讨论】:

          【解决方案6】:

          方法一:

          您还可以查看JavaTuples library,它可能适合您的需求并提供非常优雅的解决方案。使用三元组

          更多详情请见here

          方法二:

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

          // 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;
                     }
              }
          

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

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

          SO 问题here中的更多详细信息

          【讨论】:

          • 第三种方法是向定义了sortFilegetFRE 方法的类添加三个字段(nWordsnSyllablesnSentences)。 (顺便说一句:我会考虑使这些方法成为非静态方法,并在主方法中创建一个类的实例来调用这些方法;这样字段也可以是非静态的。)
          【解决方案7】:

          首先你需要检查 sortFile(args[0]) 方法的返回类型是什么,并将返回值存储在相同的数据类型中(任何方法只能返回一个值或 Object一个时间,但返回的对象可以在其中保存多个值。)。所以使用适当的数据结构(如 ArrayList..etc 作为返回类型)或任何其他对象存储这三个值并从该 sortFile(args[0]) 函数返回。最后在下一个方法调用 getFRE(returnedObject) 中传递该变量。

          【讨论】:

            【解决方案8】:

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

            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());
            
            }
            
            
            
            }
            

            【讨论】:

              【解决方案9】:

              别人提到的方法肯定行得通。

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

              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++;
                  }
              }
              

              【讨论】:

                【解决方案10】:

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

                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
                }
                

                您可能认为我提出的做法是一种不好的做法(而且确实如此!),但是,由于您的计数程序似乎不是一个不断发展的项目,它会在最少的干预下使其工作。

                【讨论】:

                  【解决方案11】:

                  您可以将地图返回为:

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

                  来自sortFile()方法

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

                  并且可以用作:

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

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

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2017-03-19
                    • 1970-01-01
                    • 2018-12-24
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多