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

Course Content

Java Extended

Java Extended

1. Deep Java Structure
2. Methods
3. String Advanced
4. Classes
5. Classes Advanced

bookUsing 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
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
1234567891011121314151617
package com.example; // 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 every element from 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 also can see that the sorted array is printed in the console. The values are displayed from the smallest to the largest. The method we imported from the Arrays library sorted the array.

Note

We will familiarize ourselves with the concepts of methods and classes throughout this section, so don't worry if it seems difficult to grasp at the moment. The main thing to remember now is how and where imports are used.

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

java

Main

copy
1234567891011121314151617
package com.example; // 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 array with value "1" Arrays.fill(array, 1); // printing every element from 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.

Task

Your task is to import the Arrays library, then sort the given array of char elements and display them on the screen.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 5
toggle bottom row

bookUsing 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
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
1234567891011121314151617
package com.example; // 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 every element from 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 also can see that the sorted array is printed in the console. The values are displayed from the smallest to the largest. The method we imported from the Arrays library sorted the array.

Note

We will familiarize ourselves with the concepts of methods and classes throughout this section, so don't worry if it seems difficult to grasp at the moment. The main thing to remember now is how and where imports are used.

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

java

Main

copy
1234567891011121314151617
package com.example; // 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 array with value "1" Arrays.fill(array, 1); // printing every element from 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.

Task

Your task is to import the Arrays library, then sort the given array of char elements and display them on the screen.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 5
toggle bottom row

bookUsing 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
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
1234567891011121314151617
package com.example; // 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 every element from 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 also can see that the sorted array is printed in the console. The values are displayed from the smallest to the largest. The method we imported from the Arrays library sorted the array.

Note

We will familiarize ourselves with the concepts of methods and classes throughout this section, so don't worry if it seems difficult to grasp at the moment. The main thing to remember now is how and where imports are used.

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

java

Main

copy
1234567891011121314151617
package com.example; // 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 array with value "1" Arrays.fill(array, 1); // printing every element from 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.

Task

Your task is to import the Arrays library, then sort the given array of char elements and display them on the screen.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

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
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
1234567891011121314151617
package com.example; // 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 every element from 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 also can see that the sorted array is printed in the console. The values are displayed from the smallest to the largest. The method we imported from the Arrays library sorted the array.

Note

We will familiarize ourselves with the concepts of methods and classes throughout this section, so don't worry if it seems difficult to grasp at the moment. The main thing to remember now is how and where imports are used.

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

java

Main

copy
1234567891011121314151617
package com.example; // 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 array with value "1" Arrays.fill(array, 1); // printing every element from 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.

Task

Your task is to import the Arrays library, then sort the given array of char elements and display them on the screen.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 1. Chapter 5
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt