Advertisement
therrontelford

Copying arrays

Jan 16th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.71 KB | None | 0 0
  1. public class CopyingArrays {
  2.     public static void main(String[] args){
  3.         // copying arrays using a loop
  4.         int[] sourceArray = new int[10];
  5.        
  6.         // intialize sourceArray with even numbers
  7.         int count=0;
  8.         for (int i=0; count <sourceArray.length; i++){
  9.             int num = (int)(Math.random() * 31);
  10.                 if (num % 2== 0){
  11.                     sourceArray[count]= num;
  12.                     count++;
  13.                 }
  14.             }
  15.        
  16.         int[] targetArray= new int[10];
  17.         for (int i=0; i<sourceArray.length; i++)
  18.             targetArray[i]= sourceArray[i];
  19.        
  20.         // using the Arrays class arraycopy method
  21.         String[] gems ={"Diamond", "Ruby", "Emerald", "Opal", "Sapphire"};
  22.         String[] gemsStorage= new String[10];
  23.         System.arraycopy(gems, 0, gemsStorage, 0, gems.length);
  24.        
  25.         }
  26.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement