【问题标题】:Return an array and a variable together in main function在主函数中一起返回一个数组和一个变量
【发布时间】:2018-09-19 06:16:05
【问题描述】:

我有这样的功能

Class Return_two{
     public static void main(String args[]){
     int b=0;// Declare a variable
     int a []= new int[3];// Declare an array [both are return at the end of the user define function fun()]
      Return_two r=new Return_two();
       int result_store= r.fun(a,b);//where should I store the result meaning is it a normal variable or an array where I store the result?
   }
 public int [] fun (int[] array,int var)//may be this is not a good Return type to returning an array with a variable so what will be change in return type? 
 {    
      for(int counter=0;counter <array.length;counter++)
      { var=var+counter;
       }
       return( array,var);// Here how could I return this two value in main function?
   }
  }

现在,这是我的问题。如上所述,我想返回一个带有变量的数组。但据我所知,可以返回一个数组或一个变量,但不能同时返回两者。或者可以返回一个或多个变量,使这些变量作为数组元素。但是如何在 main 函数中返回一个带有变量的数组呢?

【问题讨论】:

标签: java arrays return return-value return-type


【解决方案1】:

如果要创建多个值,请将它们包装在一个对象中。

(我无法从你发布的内容中想出一个有意义的名字)

class Result {
    private int[] a;
    private int b;

   public Result(int[] a, int b) {
      this.a = a;
      this.b = b;
   }
   //Getters for the instance variables
   public int[] getA() {
      return a;
   }
   public int getB() {
       return b;
   }
}

fun结尾

return new Result(array, var);

一些最佳实践:

  1. 不要声明与参数同名的变量名(a in fun

  2. 在上述Result类中,最好在数组a上创建副本,以避免类外发生突变。

  3. 如果可能,不要使用数组并使用List(这会给您带来很大的灵活性)

编辑:

你的来电者看起来像

 Return_two r=new Return_two();
 Result result = r.fun(a, b); 
 result.getA();//Do whatever you want to do with the array
 result.getB();//Do whatever you want to do with that variable

使用您当前版本的(修改后的)代码,为什么要返回数组,因为它与您传递给fun 方法的相同?只返回计算出的var 对你有用(因此返回类型可以简单地是int)。

你也可以在一行中实现你在fun中所做的事情

return (array.length * (array.length - 1)) / 2;

【讨论】:

  • 在这里创建一个类名Result。然后创建这个类的构造函数,它有两个参数,一个是数组,另一个是普通变量。然后定义两个单独的函数getA()getB() 并返回它们。但是在我的程序中,我只有一个 fun 函数,它可以返回一个数组和一个变量。那可能吗?我编辑我的问题以便更好地理解。
  • 我知道返回该数组不是必需的,但我想知道如何将数组和变量一起返回。
  • 一个函数不能返回多个值
【解决方案2】:
Wrap these properties into a object, say 

    Public class FunModel
    {
        public int[] a;
        public int b;
    }

then you can return an instance of `FunModel`.

Or

you can use `Tuples`

------------------
Futher Explanation
 ------------------

这里的返回类型应该是一个模型。 该模型应该具有您想要作为属性返回的所有内容。 您可以从您的方法中返回此模型。

    public class FunModel
    {
        public int[] a;
        public int b;

        public FunModel(int[] a, int b) {
            this.a = a;
            this.b = b;
        }
    }

并且该方法应该返回这个模型的一个实例。

public class ReturnTwo {

            public static void main(String args[]){
                int b=0;
                int a []= new int[3];
                ReturnTwo returnTwo = new ReturnTwo();
                FunModel funModel =  returnTwo.fun(a,b);
                //other processing
            }

            public FunModel fun (int[] array,int tempVar)
            {
                FunModel temp = new FunModel(array,tempVar);
                for(int counter=0;counter <array.length;counter++)
                {
                    temp.b = temp.b + counter;
                }
                return temp;// you return the model with different properties
            }
        }

【讨论】:

  • 不明白..请解释更多
猜你喜欢
  • 2015-01-16
  • 2014-08-30
  • 1970-01-01
  • 1970-01-01
  • 2021-05-13
  • 2013-11-13
  • 1970-01-01
  • 1970-01-01
  • 2017-02-03
相关资源
最近更新 更多