Arrays

Java Collections
Level 7 , Lesson 7
Available

"Hi, Amigo!"

"How was your morning lesson?"

"Well, it was decent. I'll tell you about it."

"Bilaabo gave me a bunch of design patterns, and Ellie showed me a whole bunch of collections. Not an easy day after all."

"Don't you worry — I won't burden you with very much."

"I want to tell you about the two utility classes that you've already encountered."

"The Arrays and Collections classes. All their methods are static and designed to work with collections and arrays."

"I'll start with the simpler one: Arrays. Here are its methods:"

Methods Explanation
List<T> asList(T... a)
Returns an immutable list filled with the passed elements.
int binarySearch(int[] a, int fromIndex, int toIndex, int key)
Searches for an element (key) in array a or the subarray from fromIndex to toIndex.
The array must be sorted!
Returns the element index, or fromIndex-1 if the element is not found.
int[] copyOf(int[] original, int newLength)
Returns a copy of the original array, starting from the zeroth index and consisting of newLength elements.
int[] copyOfRange(int[] original, int from, int to)
Returns a copy of the original array, from 'from' to 'to'.
boolean deepEquals(Object[] a1, Object[] a2)
Performs a deep comparison of two arrays. Arrays are considered equal if their elements are equal. If the elements themselves are arrays, then a deep comparison is also performed on them.
int deepHashCode(Object a[])
Returns a deep hashcode based on all of the elements. If an element is an array, then deepHashCode is also called on the element.
String deepToString(Object[] a)
Performs a deep conversion of an array to a string. Calls toString() on every element. If an element is an array, then it is also converted to a string based on its deep contents.
boolean equals(int[] a, int[] a2)
Compares two arrays element by element.
void fill(int[] a, int fromIndex, int toIndex, int val)
Fills an array (or subarray) with the specified value.
int hashCode(int a[])
Computes the total hash code of all elements of an array.
void sort(int[] a, int fromIndex, int toIndex)
Sorts an array (or subarray) in ascending order.
String toString(int[] a)
Converts an array to a string. Calls toString() on every element;

"Well, these are very useful methods. Many would be helpful to me."

I'd also like to mention that I haven't presented all of the methods here. Almost all of the methods in the table have identical counterparts for all the primitive types. For example, the table has a String toString(int[] a) method, and the class also has String toString(boolean[] a), String toString(byte[] a), String toString(long[] a), String toString(float[] a), String toString(double[] a), and String toString(char[] a) methods."

"Well, that changes things. That makes it a simply indispensable class."

"I'm glad you liked it. Well, we'll continue after a break."

Comments (2)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
BlueJavaBanana Level 37
18 April 2021
Why do we get the absolute basic lessons we learnt in the first quest again here, then an unrelated, very difficult task directly after? This is a pattern of this quest and quite an frustrating one.
John Squirrels Level 41, San Francisco, Poland
20 April 2021
You will come across those topics in the future. You can skip the task that frustrates you and come back to it later.