int[] original = {199, 299, 399, 499, 599, 699, 799, 899, 999};
int[] copy1 = Arrays.copyOf(original, original.length);
int[] copy2 = Arrays.copyOf(original, 5);
int[] copy3 = Arrays.copyOf(original, 15);
System.out.println("original array: " + Arrays.toString(original));
System.out.println("copy #1 (same length): " + Arrays.toString(copy1));
System.out.println("copy #2 (truncted): " + Arrays.toString(copy2));
System.out.println("copy #3 (padded): " + Arrays.toString(copy3));Output:original array: [199, 299, 399, 499, 599, 699, 799, 899, 999] copy #1 (same length): [199, 299, 399, 499, 599, 699, 799, 899, 999] copy #2 (truncated): [199, 299, 399, 499, 599] copy #3 (padded): [199, 299, 399, 499, 599, 699, 799, 899, 999, 0, 0, 0, 0, 0, 0]Note that the padded elements are initialized to zeroes (for primitive numbers).
String[] original = {"Apple", "Banana", "Carrot", "Lemon", "Orange", "Grape"};
String[] copy1 = Arrays.copyOf(original, original.length);
String[] copy2 = Arrays.copyOf(original, 3);
String[] copy3 = Arrays.copyOf(original, 10);
System.out.println("original array: " + Arrays.toString(original));
System.out.println("copy #1 (same length): " + Arrays.toString(copy1));
System.out.println("copy #2 (truncated): " + Arrays.toString(copy2));
System.out.println("copy #3 (padded): " + Arrays.toString(copy3));Output:original array: [Apple, Banana, Carrot, Lemon, Orange, Grape] copy #1 (same length): [Apple, Banana, Carrot, Lemon, Orange, Grape] copy #2 (truncated): [Apple, Banana, Carrot] copy #3 (padded): [Apple, Banana, Carrot, Lemon, Orange, Grape, null, null, null, null]Here, note that the padded elements are initialized to nulls (for object reference types). The following example shows how to copy an array of Integer to an array of Number (Number is the super type of Integer):
Integer[] integers = {16, 32, 64, 128, 256, 512};
Number[] numbers = Arrays.copyOf(integers, integers.length, Number[].class);
System.out.println("Numbers: " + Arrays.toString(numbers));Output:Numbers: [16, 32, 64, 128, 256, 512]And the following example illustrates how to copy a specified range of an array into a new array:
int[] original = {8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096};
int[] copy = Arrays.copyOfRange(original, 3, 8);
System.out.println("Original: " + Arrays.toString(original));
System.out.println("Sub copy: " + Arrays.toString(copy));Output:Original: [8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096] Sub copy: [64, 128, 256, 512, 1024]
int[] numbers = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21};
System.out.println("Before fill: " + Arrays.toString(numbers));
Arrays.fill(numbers, 0);
System.out.println("After fill: " + Arrays.toString(numbers));Output:Before fill: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21] After fill: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]The following code “resets” Strings in an array to null:
String[] fruits = {"Banana", "Apple", "Orange", "Lemon", "Mango"};
System.out.println("Before fill: " + Arrays.toString(fruits));
Arrays.fill(fruits, null);
System.out.println("After fill: " + Arrays.toString(fruits));Output:Before fill: [Banana, Apple, Orange, Lemon, Mango] After fill: [null, null, null, null, null]And the following code snippet fills only a half of an array:
double[] doubles = {1.23, 2.34, 3.45, 4.56, 5.67, 6.78, 7.89, 8.90};
System.out.println("Before fill: " + Arrays.toString(doubles));
Arrays.fill(doubles, 0, doubles.length / 2, 0.0);
System.out.println("After fill: " + Arrays.toString(doubles));Output:Before fill: [1.23, 2.34, 3.45, 4.56, 5.67, 6.78, 7.89, 8.9] After fill: [0.0, 0.0, 0.0, 0.0, 5.67, 6.78, 7.89, 8.9]
String deepToString(Object[] a)
Here’s an example that prints content of a 2-dimenson array:String[][] persons = {
		{"Tom", "USA", "Developer", "Jogging"},
		{"John", "Canada", "Designer", "Painting"},
		{"Alice", "UK", "Tester", "Biking"}
	};
System.out.println(Arrays.deepToString(persons));Output:[[Tom, USA, Developer, Jogging], [John, Canada, Designer, Painting], [Alice, UK, Tester, Biking]]
 Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.