Difference between copyof and copyofrange
Answers
Answered by
0
Arrays is a powerful class to manipulate arrays in java. It works on all types of data type in same way. Arrays class provides various methods that are useful when working with arrays. Although these methods technically aren’t part of the collections framework, they help bridge the gap between collections and arrays.
copyOf() method copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length. For all indices that are valid in both the original array and the copy, the two arrays will contain identical values. The resulting array is of exactly the same as the original array. It’s signature is as follows:
static Type[] copyOf(Type[] original, int newLength)
Note: here Type refers to boolean, byte, char, double, float, int, long, short, Object
Here original is the array to be copy and newLength is the size of new array. If successfully then it return new array of the specified type.
copyOfRange() method copies the specified array from a starting location to a end-1 location. Its signature is as follows:
static Type[] copyOfRange(Type[] original, int start, int end)
Note: here Type refers to boolean, byte, char, double, float, int, long, short, Object
Here original is the array to be copy and start is the starting position to be copied and end is the ending position to be copied. If successfully then it return new array of the specified type. This method may throw an IllegalArgumentException if start is greater than end, or and ArrayIndexOutOfBoundsException if start or end is out of bounds. It also throw a NullPointerException if original is null.
deepToString() method returns a string representation of the “deep contents” of the specified array. If the array contains other arrays as elements, the string representation contains their contents and so on. This method is designed for converting multidimensional arrays to strings. Its signature is shown here:
static String deepToString(Object []a)
toString() methods return a string representation of the array. The string representation consists of a list of the array’s elements, enclosed in square brackets (“[]”). Adjacent elements are separated by the characters “, ” (a comma followed by a space). Elements are converted to strings as by String.valueOf(boolean). Returns “null” if a is null. Its signature is shown here:
static String toString(Type[] array)
Note: here Type refers to boolean, byte, char, double, float, int, long, short, Object
copyOf() method copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length. For all indices that are valid in both the original array and the copy, the two arrays will contain identical values. The resulting array is of exactly the same as the original array. It’s signature is as follows:
static Type[] copyOf(Type[] original, int newLength)
Note: here Type refers to boolean, byte, char, double, float, int, long, short, Object
Here original is the array to be copy and newLength is the size of new array. If successfully then it return new array of the specified type.
copyOfRange() method copies the specified array from a starting location to a end-1 location. Its signature is as follows:
static Type[] copyOfRange(Type[] original, int start, int end)
Note: here Type refers to boolean, byte, char, double, float, int, long, short, Object
Here original is the array to be copy and start is the starting position to be copied and end is the ending position to be copied. If successfully then it return new array of the specified type. This method may throw an IllegalArgumentException if start is greater than end, or and ArrayIndexOutOfBoundsException if start or end is out of bounds. It also throw a NullPointerException if original is null.
deepToString() method returns a string representation of the “deep contents” of the specified array. If the array contains other arrays as elements, the string representation contains their contents and so on. This method is designed for converting multidimensional arrays to strings. Its signature is shown here:
static String deepToString(Object []a)
toString() methods return a string representation of the array. The string representation consists of a list of the array’s elements, enclosed in square brackets (“[]”). Adjacent elements are separated by the characters “, ” (a comma followed by a space). Elements are converted to strings as by String.valueOf(boolean). Returns “null” if a is null. Its signature is shown here:
static String toString(Type[] array)
Note: here Type refers to boolean, byte, char, double, float, int, long, short, Object
Similar questions