在参数中传递数组对象

时间:2022-03-12 16:58:44

I am new to java, I would like to know, how can we pass an array object as a parameter to a method.Lets say if I have:

我是java新手,我想知道,如何将数组对象作为参数传递给方法。假设我有:

public void sortArray(A[7])

What should I put in between the parenthesis? Should it be A[length of array] or what?

在括号之间应该加什么?它应该是[数组长度]还是什么?

6 个解决方案

#1


3  

When you pass an array as a parameter its length and what's stored in it gets passed so you don't need to specifically specify the length. For implementation, see the example below:

当您将数组作为参数传递时,它的长度和存储在其中的内容将被传递,因此不需要指定长度。为了实现,请参见下面的示例:

Simple example of a method that takes in an int array as a parameter:

以int数组作为参数的方法的简单示例:

public void takeArray(int[] myArray) {
    for (int i = 0; i < myArray.length; i++) { // shows that the length can be extracted from the passed in array.
        // do stuff
    }
}

You would call this method thusly:

你可以这样叫这个方法:

Say you have an array like so:

假设你有一个这样的数组:

int[] someArray = {1, 2, 3, 4, 5};

Then you call the above method with:

然后将上述方法调用为:

takeArray(someArray);

#2


1  

Just pass the array to the method. You no need to mention any size.

只需将数组传递给方法。你不需要提及任何尺寸。

void sortArray(int[] array) {
  // Code
}

// To call the method and pass this array do.

//调用该方法并传递此数组。

int[] array = new int[10];
sortArray(array);

#3


0  

for example you have procedure as you said:

例如,你有你说过的程序

public void sortArray(typeArray[] A){
 //code
 //code
}

calling array:

调用数组:

typeArray[] A = new typeArray[N]; //N is number of array you want to create
searchArray(A); //this how I call array

#4


0  

You just pass the name of the array to the method.

您只需将数组的名称传递给方法。

int[] a = new int[10];

...

bar(a);

where bar is defined like:

其中bar的定义为:

void bar(int[] a)
{
    ...
}

#5


0  

This way you can pass an array

这样就可以传递一个数组

int[] a = new int[100];
myFunction(a);

 public void myFunction(int[] a){
       for(int i =0; i< a.lenght();i++){

                System.out.println(i);

              }

       }

#6


0  

You can also create an anonymous array if you don't want an array to be named like:

如果不希望数组命名为:

public void array(int arr[])
{
    // code handling arr
}

Now for the above method you can pass an array object without creating it like:

现在对于上面的方法,您可以传递一个数组对象,而不创建它:

public static void main(String[] args)
{
   array(int[] {1,2,3,4,5});
}

This is also named as an Un-Named Array or Anonymous array. There is no need to create an array for call by value. If you don't want that array inside the main() method anymore you can use an un-named array. This helps in memory saving. Thankyou

这也被命名为一个未命名的数组或匿名数组。不需要为按值调用创建数组。如果不再希望该数组位于main()方法中,则可以使用未命名数组。这有助于节省内存。谢谢

#1


3  

When you pass an array as a parameter its length and what's stored in it gets passed so you don't need to specifically specify the length. For implementation, see the example below:

当您将数组作为参数传递时,它的长度和存储在其中的内容将被传递,因此不需要指定长度。为了实现,请参见下面的示例:

Simple example of a method that takes in an int array as a parameter:

以int数组作为参数的方法的简单示例:

public void takeArray(int[] myArray) {
    for (int i = 0; i < myArray.length; i++) { // shows that the length can be extracted from the passed in array.
        // do stuff
    }
}

You would call this method thusly:

你可以这样叫这个方法:

Say you have an array like so:

假设你有一个这样的数组:

int[] someArray = {1, 2, 3, 4, 5};

Then you call the above method with:

然后将上述方法调用为:

takeArray(someArray);

#2


1  

Just pass the array to the method. You no need to mention any size.

只需将数组传递给方法。你不需要提及任何尺寸。

void sortArray(int[] array) {
  // Code
}

// To call the method and pass this array do.

//调用该方法并传递此数组。

int[] array = new int[10];
sortArray(array);

#3


0  

for example you have procedure as you said:

例如,你有你说过的程序

public void sortArray(typeArray[] A){
 //code
 //code
}

calling array:

调用数组:

typeArray[] A = new typeArray[N]; //N is number of array you want to create
searchArray(A); //this how I call array

#4


0  

You just pass the name of the array to the method.

您只需将数组的名称传递给方法。

int[] a = new int[10];

...

bar(a);

where bar is defined like:

其中bar的定义为:

void bar(int[] a)
{
    ...
}

#5


0  

This way you can pass an array

这样就可以传递一个数组

int[] a = new int[100];
myFunction(a);

 public void myFunction(int[] a){
       for(int i =0; i< a.lenght();i++){

                System.out.println(i);

              }

       }

#6


0  

You can also create an anonymous array if you don't want an array to be named like:

如果不希望数组命名为:

public void array(int arr[])
{
    // code handling arr
}

Now for the above method you can pass an array object without creating it like:

现在对于上面的方法,您可以传递一个数组对象,而不创建它:

public static void main(String[] args)
{
   array(int[] {1,2,3,4,5});
}

This is also named as an Un-Named Array or Anonymous array. There is no need to create an array for call by value. If you don't want that array inside the main() method anymore you can use an un-named array. This helps in memory saving. Thankyou

这也被命名为一个未命名的数组或匿名数组。不需要为按值调用创建数组。如果不再希望该数组位于main()方法中,则可以使用未命名数组。这有助于节省内存。谢谢