"Amigo, I hope by now you've realized how useful arrays are in programming?"

"Most definitely, Rishi! I've already solved more than one task."

"Did your tasks have repetitive actions? The kind that you have done over and over again."

"If I understood you correctly, you mean similar actions in different tasks? For example, everywhere using a loop to display the contents of an array — I'm so tired of doing that!"

"Yes, that's what I mean. Java's creators noticed that Java programmers often write the same code when working with arrays. For example, code to copy part of an array to another array, or code to fill each cell of an array with the same value. Or your example: code to display the contents of an array in a readable form on the screen.

"And real programmers have one very important rule: don't repeat yourself. You'll agree that it's wrong to do superfluous work — you won't get paid for it. Effective work is well paid, believe me. Incidentally, a newbie's code is immediately recognizable by the large amount of repetitive code.

"Java's creators pondered this, and created the special Arrays class (its full name is java.util.Arrays), putting the most popular array-related actions into it."

"Hmm... Very interesting. So what does it have exactly?

"It has a lot of methods for every occasion, but first we'll consider just 10 of them — the simplest and most often used.

Arrays.toString()

"The first method we'll look at is Arrays.toString(). But first, a little background.

"Each array in Java has a toString() method, which returns a 'textual representation of the array'. You can get a textual representation of an array using the following statement:

String str = name.toString();

"Where name is the name of the array variable, and str is the name of the variable that will store the string representation of the array. Amigo, have you tried printing an array to the screen using the System.out.println(name) method?

I confess, that did happen. I saw some gibberish. I decided stay out of harm's way and to continue using loops.

"Most likely you saw something like:

I@37afeb11

"The first letter I means that it is an int array, and the symbols after @ are the address of a single object in memory. You can think of it as the array's address in memory. On the one hand, this is precisely the information stored in the array variable, but on the other, you expected something different, didn't you?"

"Exactly! I was planning to see the values in the array. This was the last thing I expected."

"And that's exactly why they came up with the Arrays.toString() method — to display the values of an array. We call it like this:

String str = Arrays.toString(name);

Examples:

int[] array = {1, 2, 3};
String str = Arrays.toString(array);
The str variable will contain the string "[1, 2, 3]"
int[] array = {};
String str = Arrays.toString(array);
The str variable will contain the string "[]"
String[] array = {"Hi", "How's", "life?"};
String str = Arrays.toString(array);
The str variable will contain the string "[Hi, How's, life?]"
undefined
6
Task
New Java Syntax, level 6, lesson 7
Locked
Displaying arrays
Implement the main(String[]) method, which displays the strings and ints arrays on the console using the Arrays.toString() method.

Arrays.deepToString()

"However, the toString() method isn't adequate when it comes to two-dimensional arrays. In other words, if you try to call Arrays.toString(), you will see something familiar:

[I@37afeb11, I@37afeb21, I@37afeb31]

"This is all because the cells of a two-dimensional array store references to one-dimensional arrays. And how are one-dimensional arrays converted to a string? Exactly as you see above.

"What to do then? How do we correctly display a two-dimensional array?"

"Java's creators anticipated this too. To this end, the Arrays class has another special method — deepToString(). Calling it looks like this:

String str = Arrays.deepToString(name);

"This method can be passed arrays that are two-dimensional, one-dimensional, three-dimensional or, in general, any dimension, and it will always display the elements of the array.

Examples:

int[] array = {1, 2, 3};
String str = Arrays.deepToString(array);
The str variable will contain the string "[1, 2, 3]"
int[][] array = { {1, 1}, {2, 2}, {3, 3} };
String str = Arrays.deepToString(array);
The str variable will contain the string "[[1, 1], [2, 2], [3, 3]]"
int[][][] array = { {{1, 2, 3}, {1}}, {{}} };
String str = Arrays.deepToString(array);
The str variable will contain the string "[[[1, 2, 3], [1]], [[]]]"
undefined
6
Task
New Java Syntax, level 6, lesson 7
Locked
Displaying two-dimensional arrays
Implement the main(String[]) method, which displays the strings and ints arrays on the console using the Arrays.deepToString(Object[][]) method.

Arrays.equals()

"We figured out how to display arrays on the screen. What about comparing arrays? Do you remember what methods we can use to compare strings?"

"I usually use the equals method!

"Yes, equals, and also equalsIgnoreCase (which compares strings without regard to uppercase and lowercase).

"Good news: you can also use the equals method for arrays. Bad news: it doesn't compare the contents of arrays. The equals method of arrays does the same thing as the == operator — it compares references.

Examples:

int[] x1 = {1, 2, 3};
int[] x2 = {1, 2, 3};
x1 == x2;
false (the references are not equal)
int[] x1 = {1, 2, 3};
int[] x2 = {1, 2, 3};
x1.equals(x2);
The equals method of arrays simply compares the references of two arrays.

false (the references are not equal)

"So what do we do? How do we compare arrays based on their contents?"

"Again the Arrays class comes to our rescue. More specifically, its Arrays.equals() method. This is how we call it:

Arrays.equals(name1, name2)

"The method returns true if the arrays are of equal length and their elements are equal. Otherwise, it returns false.

Examples:

int[] x1 = {1, 2, 3};
int[] x2 = {1, 2, 3};
x1.equals(x2);
The equals method of arrays simply compares the references of two arrays.

false (the references are not equal)
int[] x1 = {1, 2, 3};
int[] x2 = {1, 2, 3};
Arrays.equals(x1, x2);


true (the contents are equal)
int[] x1 = {1, 2, 3};
int[] x2 = {1, 2, 3, 4};
Arrays.equals(x1, x2);


false (the contents of the arrays are different)

Arrays.deepEquals()

"And, as you probably already guessed, the Arrays.equals method will not work correctly for two-dimensional arrays: it treats two-dimensional arrays like a one-dimensional array whose elements are addresses of one-dimensional arrays.

"Thus, to correctly compare multidimensional arrays (n = 1, 2, 3,... ), they came up with the Arrays.deepEquals()method. Calling it looks like this:

Arrays.deepEquals(name1, name2)

"The method returns true if the arrays are of equal length and their elements are equal. Otherwise, it returns false. If the elements inside the array are also arrays, then the Arrays.deepEquals() method is used to compare them, and so on.

Examples:

int[][] x1 = {{1, 2, 3}, {4, 5, 6}};
int[][] x2 = {{1, 2, 3}, {4, 5, 6}};

x1.equals(x2);
The equals method of arrays simply compares the references of two arrays.

false (the references are not equal)
int[][] x1 = {{1, 2, 3}, {4, 5, 6}};
int[][] x2 = {{1, 2, 3}, {4, 5, 6}};

Arrays.equals(x1, x2);
The Arrays.equals method will compare x1 and x2 as one-dimensional arrays that store references. They contain different references.
false (the contents of the arrays are not equal)
int[][] x1 = {{1, 2, 3}, {4, 5, 6}};
int[][] x2 = {{1, 2, 3}, {4, 5, 6}};

Arrays.deepEquals(x1, x2);



true (the contents are equal)
undefined
6
Task
New Java Syntax, level 6, lesson 7
Locked
Comparing two-dimensional arrays
Fix the logic of the main(String[]) method. It should display true if arrayFirst is the same as arraySecond. Otherwise, it should return false.

"Thank you, Rishi! This lesson is just what I needed to make my life easier and happier in the future. Now I will use the Arrays class's methods and write my programs even faster.

"That's what I was counting on, ha-ha. But these aren't all of the interesting methods in the Arrays class. I will tell you about others next time."