Arrays Part 2

1
2
Arrays Part 1
PREVIEW18m 56s
3

## The course is part of this learning path

Start course
Difficulty
Beginner
Duration
1h 15m
Students
65
Ratings
5/5
Description

In this course, we'll learn about Arrays in Java.

### Learning Objectives

• What is an Array?
• Declaration and Initialization
• Sorting Arrays
• Searching Arrays
• Multi-Dimensional Arrays

### Intended Audience

• Anyone looking to get Oracle Java Certification
• Those who want to learn the Java Programming language from scratch
• Java developers who want to increase their knowledge
• Beginners with no previous coding experience in Java programming
• Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

### Prerequisites

• No prior knowledge is required about the Java programming language.
• Basic computer knowledge
Transcript

Hi there. In our previous lesson, we learned what an array is, how it is declared and how it's initialized. We also mentioned the important points that you should pay attention to when defining array. In this lesson, we'll do some more examples about arrays. If you're ready, let's start. In exercise project, right click on the array package and select 'New class'. Specify the class name as ArrayExamples, and select the checkbox to add the main method. Let's declare an int array, that its name is numbers. The size of the numbers array is four. This means it can hold four numbers.

Let's assign values to elements of the array, the first element of the array and assign four, the second element and assign six, the third element and assign negative three, and the last element assigns negative two. Let's calculate the sum of array values, declare variable sum with int type and assign array elements by adding an index. Numbers  represents the first element of the array. Numbers  represents the second element of the array. Numbers  represents the third element of the array. Number  represents the fourth element of the array. Also, note that the index number of the last element is one less than the size of the array. That is, the number of elements. So, if you know the number of elements in an array, you can find the last index number of that array. For example, if an array consists of 50 elements, the index number of the last element of this array is 49.

Now, let's print the sum of the array values by using the println method, S.out.println "sum =" + sum. Okay. Let's run the code. We see 'sum = 5' in the console. Also, there is no need to assign a new variable. We can use directly elements of the array between parentheses in the println method. Okay. Let's run the code again. And we see the same result. Let's try to add an element with the fourth index. Let's run the code. We got an error. Remember, if we want to add an index that is either negative or greater than or equal to the size of an array, an array index out of bounds exception is thrown. It's thrown only at runtime. The Java compiler does not check for this error during the compilation of a program. Let's declare an int array, that it's name is number, with the initial values 4, 6, -3, -2. The elements can be the same as the elements of the numbers array.

Also, if you remember, we could create an array this way too. Now, let's alter the second element of this array. We can alter the value of any element of the array with the new value. Let's alter the second element of the array. Since we will alter the second element, we'll use the index of the second element, that is one. So, I will write one in the square brackets. Also, the new value of the second element can be 12. Let's print the new sum of array elements by using the print method. S.out.println ("Sum of the new array =" +, Inside the parentheses I write, number  number  + number  + number . Okay. Let's run the code. And the new sum is 11. Also, I'll print the elements of the array before and after the altering. If you remember, we can print the elements of the array using the arrays.toString method. So, here I'll write, S.out.println "Before altering : + arrays.toString(number). And I'll copy this print method and paste it here. And I'll write here 'after altering'.

Okay. Let's run the code. As you can see, the previous sum is five and the previous elements are 4, 6, -3, -2. And after altering, new elements are 4, 12, -3, -2. And the new sum is 11. So, we altered the second element of the array. So, let's look at another example. I'll create an array type of string, string [] cars = {}. I will write each element in the double quotes. Because this array is a string array, we will define the strings between double quotes. We'll learn much more about strings in the next lectures. So, the first element can be Mercedes. The second element can be BMW. The last element can be Ferrari. First, let's print each element of the array separately on the console. S.out.println "cars  :" + cars .

So, we can reach the elements with the index number. This line of code prints the first element of the array because the index number is zero. Now, let's print the other elements. I'll copy this line and paste it below two times. This will be index one and this, and this will be index two and this. Let's run the code. As you can see, we printed all the elements separately on the console. Now, let's try to print the fourth element, i.e, index three, to the console. I'll copy this line and paste it here. I write three instead of two. If you pay attention, the size of the array is three and the last index is two. So, can we print the element on the third index? Let's run and see. As you can see, we got the array index out of bounds exception again.

Yes guys, I think the array issue is understood. Before concluding the lesson, I would like to give a couple of tips. Let's declare an array first, int [] ages = new int []. If you notice, we need to specify how many elements this array will consist of. Otherwise, we get a compilation error. Well, if we specify the number of elements inside the first square brackets, not here, is it valid? Of course not. We'll still get a compilation error. If you see such usage, it's wrong. Another issue is about the length we use when learning the size of the array. For example, the age array here consists of four elements. When learning the size of the array, we write ages.length. So, can we change the size of the array using this length constant?

So, if I write here, ages.length = 10, will it work? No, that's an incorrect usage because we cannot change the number of elements of the array later. Yes, friends. We will do different examples of arrays in the loop section. Let's take a short break here. See you in the next lesson. 