Sometimes conversion of primitive types to boxed types is necessary.
To convert the array, it's possible to use streams (in Java 8 and above):
int[] numbers = {5, 10, 15}; Integer[] boxedNumbers = Arrays.stream(numbers).boxed().toArray(Integer[]::new);
With lower versions it can be by iterating the primitive array and explicitly copying it to the boxed array:
int[] values = {10, 20, 30}; Integer[] boxedValues = new Integer[values.length]; for (int i = 0; i < values.length; ++i) { boxedValues[i] = values[i]; // Each element is autoboxed here }
Similarly, a boxed array can be converted to an array of its primitive counterpart:
Integer[] values = {10, 20, 30}; int[] primitiveValues = Arrays.stream(values).mapToInt(Integer::intValue).toArray(); Integer[] values = {5, 6, 7, 8}; int[] primitiveValues = new int[values.length]; for (int i = 0; i < values.length; ++i) { primitiveValues[i] = values[i]; // Each element is unboxed here }
Java doesn't provide a direct method in java.util.Arrays to remove an element from an array. To perform it, you can either copy the original array to a new one without the element to remove or convert your array to another structure allowing the removal.
Using ArrayList
You can convert the array to a java.util.List, remove the element and convert the list back to an array as follows:
String[] words = new String[]{"apple", "orange", "banana"}; List<String> wordList = new ArrayList<>(Arrays.asList(words)); wordList.remove("orange"); // Constructs a new array with the size equal to the list and copies its elements words = wordList.toArray(new String[wordList.size()]); System.out.println(Arrays.toString(words)); // [apple, banana]
Using System.arraycopy
System.arraycopy() can be used to make a copy of the original array and remove the element you want. Below an example:
int[] numbers = new int[] { 5, 10, 15, 20, 25 }; // Original array. int[] output = new int[numbers.length - 1]; // Array to hold the result. int position = 2; // Remove the value "15". // Copy elements to the left of the position. System.arraycopy(numbers, 0, output, 0, position); // Copy elements to the right of the position. System.arraycopy(numbers, position + 1, output, position, numbers.length - position - 1); System.out.println(Arrays.toString(output)); // [5, 10, 20, 25]
Using Apache Commons Lang
To easily remove an element, you can use the Apache Commons Lang library and especially the static method removeElement() of the class ArrayUtils. Below an example:
int[] array = new int[]{1,2,3,4}; array = ArrayUtils.removeElement(array, 2); //remove first occurrence of 2 System.out.println(Arrays.toString(array)); //[1, 3, 4]
Casting Arrays
Arrays are objects, but their type is defined by the type of the contained objects. Therefore, one cannot just cast A[] to T[], but each A member of the specific A[] must be cast to a T object. Generic example:
public static <T, A> T[] castArray(T[] target, A[] array) { for (int i = 0; i < array.length; i++) { target[i] = (T) array[i]; } return target; }
Thus, given an A[] array:
T[] target = new T[array.Length]; target = castArray(target, array);
Java SE provides the method Arrays.copyOf(original, newLength, newType) for this purpose:
Double[] doubles = { 1.0, 2.0, 3.0 }; Number[] numbers = Arrays.copyOf(doubles, doubles.length, Number[].class);
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions