Following is a utility method used to copy an existing array into a new freshly created array in java.
package basics; import java.util.Arrays; public class ArrayUtility { static int[] copy( int[] input ) { if ( input==null || input.length==0 ) return null; int N = input.length; int[] copy = new int[N]; for( int i = 0; i<N ; i++ ) copy[i] = input[i]; return copy; } public static void main(String[] args) { int[] original = {22,55,66,11,32,56,67,89,95,10}; int[] copyArray = copy( original ); boolean result = Arrays.equals( original, copyArray ); System.out.println( "Arrays are equal = "+result ); System.out.print("\nOriginal Array = "); for( int i = 0; i< original.length; i++ ) System.out.print( original[i]+" " ); System.out.print("\nCopy Array = "); for (int i = 0; i < copyArray.length; i++) System.out.print(copyArray[i]+" "); } }
Create a ArrayUtility.java file in your workspace.
When the main method inside the ArrayUtility class executed it is going to print :
Arrays are equal = true
Original Array = 22 55 66 11 32 56 67 89 95 10
Copy Array = 22 55 66 11 32 56 67 89 95 10
No comments:
Post a Comment