Array Methods
Part of the Fundamentals section of Coddy's Java journey. Lesson 61 of 73.
The Arrays class (from java.util) provides many helper methods for working with arrays. To call one, write:
Arrays.methodName(arrayName, otherParameters)Here is a list of the basic methods:
fill(array, value)- fills the array with a specific value
toString()- converts the array to a string
sort(array)- sorts the array in ascending order
equals(array1, array2)- compares two arrays to determine if they are equal
Here is an example of how to use the fill method with toString:
int[] numbers = new int[5];
Arrays.fill(numbers, 10);
System.out.println(Arrays.toString(numbers));This will output [10, 10, 10, 10, 10].
Example of the sort method:
int[] numbers = {5, 2, 9, 1, 5, 6};
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers));This will output [1, 2, 5, 5, 6, 9].
Challenge
EasyCreate a method named merge that receives two arrays as arguments. The method merges the two arrays into one sorted array and returns it.
Important: The final merged array must be sorted in ascending order.
For example the following arguments: merge(new String[] {"1", "4", "2"}, new String[] {"2", "5", "9"}) will return ["1", "2", "2", "4", "5", "9"] (notice the result is sorted)
Use System.arraycopy() to copy elements from one array to another. The syntax is:
System.arraycopy(sourceArray, sourceStartPosition, destinationArray, destinationStartPosition, length)System.arraycopy() only copies into an array that already exists; it never grows the destination. Create the merged array with arr1.length + arr2.length slots first, copy arr1 to position 0, then copy arr2 starting at position arr1.length.
For example:
// Source array
String[] sourceArray = {"1", "2", "3", "4", "5"};
// Destination array
String[] destinationArray = new String[5];
// Copy elements from sourceArray to destinationArray
System.arraycopy(sourceArray, 0, destinationArray, 0, 5);Destination array after copy: 1 2 3 4 5
Try it yourself
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static String[] merge(String[] arr1, String[] arr2) {
// Write code here
return null;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String textArr1 = scanner.nextLine();
String textArr2 = scanner.nextLine();
String[] arr1 = textArr1.split(",");
String[] arr2 = textArr2.split(",");
String[] mergedArray = merge(arr1, arr2);
System.out.println(Arrays.toString(mergedArray));
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic ShortcutsComparison OperatorsString Comparison5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 411Arrays Basics
Declaring ArraysAccessing ElementsModifying ArraysArray MethodsRecap - Product ArrayRecap - Reversed Array3Variables Part 2
ConstantsNaming ConventionsRecap - Initialize VariablesType Casting Part 1Type Casting Part 26Decision Making
If StatementIf - ElseSwitch StatementTernary OperatorRecap - If ElseNested If - ElsePractice on your own: Online Java compiler