Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Using Libraries In Our Code | Deep Java Structure
Java Extended

book
Using Libraries In Our Code

java.util.Arrays

Let's move on to practice, and as an example, we will use the library java.util.Arrays. We can see that the parent library is java, followed by the child library util, and then the specific library we need, Arrays.

java

Main

copy
import java.util.Arrays;
1
import java.util.Arrays;

We have already mentioned this library in the course on arrays when discussing array methods. Now, let's see how we can use these methods in code using an example:

java

Main

copy
package com.example;

// do not modify the code below this comment

// importing Arrays library into our code
import java.util.Arrays;

public class Main {
public static void main(String[] args) {
// creating an int array with some unsorted values
int[] array = {1, 5, 6, 2, 0, -4, 2};
// using Arrays library to sort our elements
Arrays.sort(array);
// printing each element from the sorted array to the console
for (int element : array) {
System.out.print(element + " ");
}
}
}
12345678910111213141516171819
package com.example; // do not modify the code below this comment // importing Arrays library into our code import java.util.Arrays; public class Main { public static void main(String[] args) { // creating an int array with some unsorted values int[] array = {1, 5, 6, 2, 0, -4, 2}; // using Arrays library to sort our elements Arrays.sort(array); // printing each element from the sorted array to the console for (int element : array) { System.out.print(element + " "); } } }

Let's go through the code written above.

We import the necessary library and use a class from that library to call its sorting method. You can see the syntax of how we use it: ClassName.methodName(array);. You can also see that the sorted array is printed in the console, with values displayed from smallest to largest. The method we imported from the Arrays library handled the sorting.

Let's also take a look at another method from Arrays - fill.

java

Main

copy
package com.example;

// do not modify the code below this comment

// importing Arrays library into our code
import java.util.Arrays;

public class Main {
public static void main(String[] args) {
// creating an int array with some different values
int[] array = {1, 5, 6, 2, 0, -4, 2};
// using Arrays library to fill the array with the value "1"
Arrays.fill(array, 1);
// printing each element from the filled array to the console
for (int element : array) {
System.out.print(element + " ");
}
}
}
12345678910111213141516171819
package com.example; // do not modify the code below this comment // importing Arrays library into our code import java.util.Arrays; public class Main { public static void main(String[] args) { // creating an int array with some different values int[] array = {1, 5, 6, 2, 0, -4, 2}; // using Arrays library to fill the array with the value "1" Arrays.fill(array, 1); // printing each element from the filled array to the console for (int element : array) { System.out.print(element + " "); } } }

We have the same integer array, but we're not sorting it this time. Instead, we're replacing each element of the array with a specified value. Notice that we first specify the array we want to fill in the parentheses, and then we provide the value with which we want to fill the array.

After all the operations, you can see that the array displayed on the screen consists of all elements equal to 1.

Tarefa

Swipe to start coding

  1. Import the Arrays library.

  2. Sort the given array of char elements.

  3. Display the sorted array on the screen.

Solução

java

solution

package com.example;

// import Arrays library here
import java.util.Arrays;

public class Main {
public static void main(String[] args) {
char[] unsortedArray = {'y', 'e', 'e', 'p', 'w', 'c', '>', '3'};
// sort the array and print it to the console
Arrays.sort(unsortedArray);
for (char element : unsortedArray) {
System.out.print(element + " ");
}
}
}

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 5
package com.example;

// import the Arrays library here
import ___;

public class Main {
public static void main(String[] args) {
char[] unsortedArray = {'y', 'e', 'e', 'p', 'w', 'c', '>', '3'};
// sort the array and output it to the console
}
}
toggle bottom row
some-alt