有没有办法以某种方式从 returns 无效的方法中提取 int[ ] 数组值?
Is there a way to somehow extract an int[ ] array value from a method that returns a void?
如果我有以下代码修改创建的数组并且 return 只是一个 void:
public static void modifyArray()
{
myFunction(createArray(1));
}
其中createArray()
只是一些生成数组的函数,如下所示:
public static int[] createArray(int n)
{
//Lines of code that generate an array;
}
而myFunction()
只是一些修改数组的任意函数,return将其作为空值:
public static void myFunction(int[] anArray)
{
//Lines of codes that modifies any int[] array passed to this function;
}
如何在不更改 return 类型的情况下使用另一个函数将 modifyArray()
函数中修改后的数组作为 int[ ] 取回myFunction()
到一个 int[],例如:
public static int[] convertModifiedArray()
{
return modifyArray(); //This is an error since modifyArray() is a void, not an int[]. I just don't know a way to somehow "translate" a void into an int[] and I need help with this part.
}
您可以在 class 中声明一个实例变量,例如 int[] myArray。在 modifyArray 函数中,您可以将 myArray 分配给修改后的数组。您将在 myArray 中获得修改后的数组,无需更改 return 类型的方法。
public static void modifyArray() {
int[] a = createArray(1);
myFunction(a);
// Whatever you want to do with a
}
或者你可以将 return 类型更改为 int[]
或者您可以创建一个全局变量并在方法中设置该变量的值。
如果我有以下代码修改创建的数组并且 return 只是一个 void:
public static void modifyArray()
{
myFunction(createArray(1));
}
其中createArray()
只是一些生成数组的函数,如下所示:
public static int[] createArray(int n)
{
//Lines of code that generate an array;
}
而myFunction()
只是一些修改数组的任意函数,return将其作为空值:
public static void myFunction(int[] anArray)
{
//Lines of codes that modifies any int[] array passed to this function;
}
如何在不更改 return 类型的情况下使用另一个函数将 modifyArray()
函数中修改后的数组作为 int[ ] 取回myFunction()
到一个 int[],例如:
public static int[] convertModifiedArray()
{
return modifyArray(); //This is an error since modifyArray() is a void, not an int[]. I just don't know a way to somehow "translate" a void into an int[] and I need help with this part.
}
您可以在 class 中声明一个实例变量,例如 int[] myArray。在 modifyArray 函数中,您可以将 myArray 分配给修改后的数组。您将在 myArray 中获得修改后的数组,无需更改 return 类型的方法。
public static void modifyArray() {
int[] a = createArray(1);
myFunction(a);
// Whatever you want to do with a
}
或者你可以将 return 类型更改为 int[] 或者您可以创建一个全局变量并在方法中设置该变量的值。