Passing Of Array To A Function In Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rakesh12345
    New Member
    • Aug 2007
    • 1

    #1

    Passing Of Array To A Function In Java

    hi
    can u please tell me how to pass the array elements into a function in Java
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by rakesh12345
    hi
    can u please tell me how to pass the array elements into a function in Java
    Just pass the array to the function:

    [code=java]
    T[] yourArray= new T[42]; // this is your array
    ...
    foo(yourArray); // pass it to function foo
    ...
    void foo(T[] anArray) {
    // your array is known by the name anArray here
    }
    [/code]

    kind regards,

    Jos

    Comment

    • gaya3
      New Member
      • Aug 2007
      • 184

      #3
      Hi Rakesh,
      Array can be passed as arguments in the java function as follows

      -->{
      String firstArray[]=new String[10];
      firstArray[0]="12345";
      samplePgm(urArr ay);
      }
      samplePgm(Strin g []nextArray)
      {
      System.out.prin tln(nextArray[0]);
      }

      Comment

      Working...