代码之家  ›  专栏  ›  技术社区  ›  Dacto

传递数组到方法Java

  •  30
  • Dacto  · 技术社区  · 15 年前

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

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

    如何正确执行此操作?

    9 回复  |  直到 8 年前
        1
  •  52
  •   bugwheels94    10 年前

    你这样做:

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

    把它作为其他变量传递。在爪哇中,数组是通过引用传递的。

        2
  •  13
  •   jjnguy Julien Chastang    15 年前

    只需从原始代码中删除括号。

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

    仅此而已。

        3
  •  8
  •   Daniel T.    15 年前

    数组变量只是一个指针,所以您只需按如下方式传递它:

    PrintA(arrayw);
    

    编辑:

    再详细一点。如果要做的是创建一个数组的副本,则必须将数组传递到方法中,然后手工创建那里的副本(不确定Java是否有类似的内容)。 Array.CopyOf() )。否则,您将传递数组的引用,因此如果您更改其中元素的任何值,其他方法也将更改该值。

        4
  •  3
  •   Michael    12 年前

    要点

    • 您必须使用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();
      }
    }
    

    输出样本程序

    1 2 3 3 4 5 6 7

        5
  •  2
  •   ytoamn    8 年前

    在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
        }
    }
    
        6
  •  0
  •   Bostone    15 年前

    你的语法有问题。只需传入数组的名称。顺便说一下,阅读一些常见的格式化工具也是个好主意,例如在Java方法中应该从小写字母开头(这不是惯例的错误)。

        7
  •  0
  •   Prasad Khode    8 年前
    class test
    {
        void passArr()
        {
            int arr1[]={1,2,3,4,5,6,7,8,9};
            printArr(arr1);
        }
    
        void printArr(int[] arr2)
        {
            for(int i=0;i<arr2.length;i++)
            {
                System.out.println(arr2[i]+"  ");
            }
        }
    
        public static void main(String[] args)
        {
            test ob=new test();
            ob.passArr();
        }
    }
    
        8
  •  0
  •   Sunil kumawat    8 年前
    public static void main(String[] args) {
    
        int[] A=new int[size];
          //code for take input in array
          int[] C=sorting(A); //pass array via method
          //and then print array
    
        }
    public static int[] sorting(int[] a) {
         //code for work with array 
         return a; //retuen array
    }
    
        9
  •  0
  •   SilverNak jach    8 年前

    通过这种方式,我们可以将数组传递给一个函数,在这里,这个打印函数将打印数组的内容。

    public class PassArrayToFunc {
    
        public static void print(char [] arr) {
            for(int i = 0 ; i<arr.length;i++) {
                System.out.println(arr[i]);
            }
        }
    
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            char [] array = scan.next().toCharArray();
            print(array);
            scan.close();
        }
    
    }