0

I am trying to create two Arrays in main then call a method where the values of the first array(which I randomized) are copied to the second array,I am getting stuck at calling the method and passing. I am lost on how to call and pass arrays any help would be appreciated.

class C9hw5 

 {

   public static void main(String[] args)
     {

        int[] ar = new int[10]; // random array

        int[] at = new int[10]; // array two

        Random r = new Random();

        for(int i = 0; i < ar.length; i++) // initializing it to random

         ar[i] = r.nextInt();

        System.out.println("The random array displays");

        for(int i = 0; i < ar.length; i++)

         System.out.println( ar[i]);

        copyArray();
     }

   public static void copyArray(int ar[], int at[])

     {

        for (int i = 0; i < at.length; i++) 

            at[i] = ar[i];


     }
  }
}
  • You are not passing any arguments to the copyArray() function. You should pass both of your arrays like `copyArray(ar, at)` – Coder Oct 18 '16 at 21:14

2 Answers2

1

You didn't passed your arrays to the method, replace copyArray(); by copyArray(ar, at);.

Filipp Voronov
  • 4,077
  • 5
  • 25
  • 32
0

the code is cleaned up a bit to make it work. Coder gave the essence of the answer above. a reference to some options (e.g., System.arraycopy) is: Is there any reason to prefer System.arraycopy() over clone()?

import java.util.Random;

class C9hw5 {
    public static void main(String[] args) {
        int[] ar = new int[10]; // random array
        int[] at = new int[10]; // array two

        Random r = new Random();

        for(int i = 0; i < ar.length; i++) // initializing it to random
            ar[i] = r.nextInt();

        System.out.println("The random array displays");

        for(int i = 0; i < ar.length; i++)
            System.out.println(ar[i]);

        copyArray(ar, at);

        System.out.println("The second array displays");

        for(int i = 0; i < at.length; i++)
            System.out.println(at[i]);
    }

    public static void copyArray(int ar[], int at[]) {
        for (int i = 0; i < at.length; i++) 
            at[i] = ar[i];
    }
}
Community
  • 1
  • 1
ryonts
  • 126
  • 6