Problem:
=======
Copy data from one array to another array using JAVA
Algorithm:
===========
1. Initialize the array with random values.
2. Create an another array with the size of previous array or source array.
3. Iterate the source array and assign the value of source array to destination array.
4. Print out both source array and destination array.
Solution:
==========
import java.util.*;
import java.security.SecureRandom;
class CopyArrayUtility{
public static void main(String[] args){
int[] a= new int[3];
int[] b= new int[a.length];
initializeArray(a);
System.out.println("Printing source array");
printArray(a);
copyArray(a, b);
System.out.println("Printing destination/copeid array");
printArray(b);
}
public static void initializeArray(int[] array){
Random random = new SecureRandom();
for(int i=0;i < array.length; i++){
array[i] = random.nextInt(100);
}
}
public static void printArray(int[] array){
Arrays.stream(array).forEach(System.out::println);
}
public static void copyArray(int[] sourceArray, int[] destinationArray){
for(int i=0; i < sourceArray.length; i++){
destinationArray[i] = sourceArray[i];
}
}
}
output:
==========
Printing source array
46
4
56
Printing destination/copied array
46
4
56
=======
Copy data from one array to another array using JAVA
Algorithm:
===========
1. Initialize the array with random values.
2. Create an another array with the size of previous array or source array.
3. Iterate the source array and assign the value of source array to destination array.
4. Print out both source array and destination array.
Solution:
==========
import java.util.*;
import java.security.SecureRandom;
class CopyArrayUtility{
public static void main(String[] args){
int[] a= new int[3];
int[] b= new int[a.length];
initializeArray(a);
System.out.println("Printing source array");
printArray(a);
copyArray(a, b);
System.out.println("Printing destination/copeid array");
printArray(b);
}
public static void initializeArray(int[] array){
Random random = new SecureRandom();
for(int i=0;i < array.length; i++){
array[i] = random.nextInt(100);
}
}
public static void printArray(int[] array){
Arrays.stream(array).forEach(System.out::println);
}
public static void copyArray(int[] sourceArray, int[] destinationArray){
for(int i=0; i < sourceArray.length; i++){
destinationArray[i] = sourceArray[i];
}
}
}
output:
==========
Printing source array
46
4
56
Printing destination/copied array
46
4
56
No comments:
Post a Comment