将数组传递给方法Java

Dac*_*cto 31 java arrays

如何将整个数组传递给方法?

private void PassArray() {
    String[] arrayw = new String[4];
    //populate array
    PrintA(arrayw[]);
}

private void PrintA(String[] a) {
    //do whatever with array here
}
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?

Naw*_*Man 52

你做这个:

private void PassArray(){
    String[] arrayw = new String[4]; //populate array
    PrintA(arrayw);
}

private void PrintA(String[] a){
    //do whatever with array here
}
Run Code Online (Sandbox Code Playgroud)

只需将其作为其他变量传递.在Java中,数组通过引用传递.

  • so ...如果我在方法中更改数组(作为参数传入),我是否在调用者中更改原始数组中的值? (2认同)

jjn*_*guy 13

只需从原始代码中删除括号即可.

PrintA(arryw);

private void PassArray(){
    String[] arrayw = new String[4];
    //populate array
    PrintA(arrayw);
}
private void PrintA(String[] a){
    //do whatever with array here
}
Run Code Online (Sandbox Code Playgroud)

就这些.


Dan*_* T. 8

数组变量只是一个指针,所以你只需传递它:

PrintA(arrayw);
Run Code Online (Sandbox Code Playgroud)

编辑:

更详细一点.如果你想要做的是创建一个数组的COPY,你必须将数组传递给方法,然后在那里手动创建一个副本(不确定Java是否有类似的东西Array.CopyOf()).否则,您将传递数组的REFERENCE,因此如果您更改其中元素的任何值,它也将更改为其他方法.


小智 5

要点

  • 你必须使用 java.util 包
  • 数组可以通过引用传递

在方法调用语句中

  • 不要使用任何对象来传递数组
  • 只使用数组的名称,不要使用数据类型或数组括号[]

示例程序

import java.util.*;

class atg {
  void a() {
    int b[]={1,2,3,4,5,6,7};
    c(b);
  }

  void c(int b[]) {
    int e=b.length;
    for(int f=0;f<e;f++) {
      System.out.print(b[f]+" ");//Single Space
    }
  }

  public static void main(String args[]) {
    atg ob=new atg();
    ob.a();
  }
}
Run Code Online (Sandbox Code Playgroud)

输出示例程序

1 2 3 4 5 6 7


yto*_*amn 5

数组有一个重要的点,在 Java 类中通常没有教授或遗漏。当数组传递给函数时,会创建另一个指向同一个数组的指针(永远不会传递同一个指针)。您可以使用这两个指针来操作数组,但是一旦您将第二个指针分配给被调用方法中的新数组并通过 void 返回到调用函数,那么原始指针仍然保持不变。

你可以在这里直接运行代码:https : //www.compilejava.net/

import java.util.Arrays;

public class HelloWorld
{
    public static void main(String[] args)
    {
        int Main_Array[] = {20,19,18,4,16,15,14,4,12,11,9};
        Demo1.Demo1(Main_Array);
        // THE POINTER Main_Array IS NOT PASSED TO Demo1
        // A DIFFERENT POINTER TO THE SAME LOCATION OF Main_Array IS PASSED TO Demo1

        System.out.println("Main_Array = "+Arrays.toString(Main_Array));
        // outputs : Main_Array = [20, 19, 18, 4, 16, 15, 14, 4, 12, 11, 9]
        // Since Main_Array points to the original location,
        // I cannot access the results of Demo1 , Demo2 when they are void.
        // I can use array clone method in Demo1 to get the required result,
        // but it would be faster if Demo1 returned the result to main
    }
}

public class Demo1
{
    public static void Demo1(int A[])
    {
        int B[] = new int[A.length];
        System.out.println("B = "+Arrays.toString(B)); // output : B = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        Demo2.Demo2(A,B);
        System.out.println("B = "+Arrays.toString(B)); // output : B = [9999, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        System.out.println("A = "+Arrays.toString(A)); // output : A = [20, 19, 18, 4, 16, 15, 14, 4, 12, 11, 9]

        A = B;
        // A was pointing to location of Main_Array, now it points to location of B
        // Main_Array pointer still keeps pointing to the original location in void main

        System.out.println("A = "+Arrays.toString(A)); // output : A = [9999, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        // Hence to access this result from main, I have to return it to main
    }
}
public class Demo2
{
    public static void Demo2(int AAA[],int BBB[])
    {
        BBB[0] = 9999;
        // BBB points to the same location as B in Demo1, so whatever I do
        // with BBB, I am manipulating the location. Since B points to the
        // same location, I can access the results from B
    }
}
Run Code Online (Sandbox Code Playgroud)