Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CopyingArrays {
- public static void main(String[] args){
- // copying arrays using a loop
- int[] sourceArray = new int[10];
- // intialize sourceArray with even numbers
- int count=0;
- for (int i=0; count <sourceArray.length; i++){
- int num = (int)(Math.random() * 31);
- if (num % 2== 0){
- sourceArray[count]= num;
- count++;
- }
- }
- int[] targetArray= new int[10];
- for (int i=0; i<sourceArray.length; i++)
- targetArray[i]= sourceArray[i];
- // using the Arrays class arraycopy method
- String[] gems ={"Diamond", "Ruby", "Emerald", "Opal", "Sapphire"};
- String[] gemsStorage= new String[10];
- System.arraycopy(gems, 0, gemsStorage, 0, gems.length);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement