So, you know how to store different types of data types?
Yes, you already know, by using different variables.
But what if we say we have similar sort of data to be stored, do we need to declare different variables for every single piece of data.
Well, the answer is a big NO, because there comes an Array to help us out. So let's start with ...
Arrays can be super useful in many ways like, collecting the names of students that are of the same class, or list of items you want to pick up from the grocery store.
So now we know what an Array is, so let's check out how it works.
How does an Array work?
When a program is compiled, it allocates the required memory for the Array in the computer memory. The amount of memory depends upon the number and the data type of the Array. The compiler reserves a fixed block of memory for every expected data element in the array.
Once an Array is declared, it can be initialized any moment by assigning values to it. Every block of memory within the Array has a unique address which can be used to assign the values to the Array.
The same address can also be used to access the particular block of memory.
But what if we have names and the classes of the students, that is needed to be stored at a single place? What we gonna do?
Well, that's why we have different types of arrays in java.
As shown in the above diagram, the numbers from 0 to 9 are called subscripts or index numbers.
Summarizing all the above point, let's take a look at the program given below to better understand how it works.
So, just when you run this program, it gonna print the Number of days in the month of March.
You can also initialize the value of elements at the time of its declaration, it won't change any result. See the program given below.
Here, we will be discussing, how to initialize a variable if we already know the values for the array.
So let's take a look at the below-given program here,
We have a program telling us the no. of days in every month.
And what if we have to get the data from the user at run time, what we gonna do.?
Well, let's take a look at this program where we are taking 10 names of the students of a class.
Multi-Dimensional arrays can be very useful in many cases where we need to have multiple pieces of information to store under a single name like a time table or a list of students with their and marks on it.
Let's take a look at the below-given example -
And now when the compiler will reach this piece of code, it will create a 4 by 5 array and assign it to the two_d.
Now let's take a look at the below-given program, here we have an array of 4 by 5.
Yes, you already know, by using different variables.
But what if we say we have similar sort of data to be stored, do we need to declare different variables for every single piece of data.
Well, the answer is a big NO, because there comes an Array to help us out. So let's start with ...
What is an Array?
An array is a collection of similar types of data at an adjoining memory location. They can store a fixed number of values under the same name.Arrays can be super useful in many ways like, collecting the names of students that are of the same class, or list of items you want to pick up from the grocery store.
So now we know what an Array is, so let's check out how it works.
How does an Array work?
When a program is compiled, it allocates the required memory for the Array in the computer memory. The amount of memory depends upon the number and the data type of the Array. The compiler reserves a fixed block of memory for every expected data element in the array.Once an Array is declared, it can be initialized any moment by assigning values to it. Every block of memory within the Array has a unique address which can be used to assign the values to the Array.
The same address can also be used to access the particular block of memory.
But what if we have names and the classes of the students, that is needed to be stored at a single place? What we gonna do?
Well, that's why we have different types of arrays in java.
Types of Arrays in Java
So basically there are two types of arrays in Java.
01. Single Dimensional Array
02. Multi-Dimensional Array
What are Single Dimensional Arrays?
Single Dimensional Arrays are a linear list of elements of the same type.
As shown in the above diagram, the numbers from 0 to 9 are called subscripts or index numbers.
These subscripts make the input-output work inside an array really simple.
The different values at different subscripts are called elements of the array where we can store different data based upon the data type of the declared array.
By classifying the element position with the use of these subscripts we can easily store or manipulate the data at any index of any classified array.
How to declare Single Dimensional Array?
Every programming language has a special syntax that we need to follow while writing the code of any coding language. The same goes with java, there is a special syntax for declaring a single-dimensional array, which we need to take care of.
In order to create a Single Dimensional Array, you first need to declare an array variable of the same type. Let's see how to do it.
Let's see what this syntax states, here, type declares which type of data array gonna store and var-name is the name given to the variable. For Example -
Although this array variable is of no use as there is no physical where the data can be assigned. To link month_days with a physical array, we first need to allocate one using new and then assign it to month_days.
Let me show you how
So just when the statement gets executed, the month_days will refer to an array of 12 integers.
Now after you have created an array, it's time to feed him. So we just need to assign the values directly into its subscript.
<variable-type> <var-name>[ ];
Let's see what this syntax states, here, type declares which type of data array gonna store and var-name is the name given to the variable. For Example -
int month_days[ ];
Let me show you how
int month_days[ ]= new int[12];
So just when the statement gets executed, the month_days will refer to an array of 12 integers.
Now after you have created an array, it's time to feed him. So we just need to assign the values directly into its subscript.
An element of an array can be accessed & initialized by specifying it's subscript within the square brackets.
(Remember, Java index subscript start with zero, so the value [2] is actually for the third month).
int month_days[2]= 31;
Now, to print that particular element, all we need is just the subscript of the element.
System.out.println(month_days[2]);
Summarizing all the above point, let's take a look at the program given below to better understand how it works.
So, just when you run this program, it gonna print the Number of days in the month of March.
You can also initialize the value of elements at the time of its declaration, it won't change any result. See the program given below.
How to initialize a single-dimensional Array?
Initializing a single-dimensional array is pretty much easy to do as all it requires the index number where the value is meant to be fed and the value itself.Here, we will be discussing, how to initialize a variable if we already know the values for the array.
So let's take a look at the below-given program here,
We have a program telling us the no. of days in every month.
And what if we have to get the data from the user at run time, what we gonna do.?
Well, let's take a look at this program where we are taking 10 names of the students of a class.
What is a Multi-Dimensional Array?
Multi-Dimensional Arrays are actually Arrays of Arrays. These arrays are almost similar to the one we learned about above.Multi-Dimensional arrays can be very useful in many cases where we need to have multiple pieces of information to store under a single name like a time table or a list of students with their and marks on it.
How to declare a Multi-Dimensional Array?
Declaring a Multi-Dimensional is just like to declaring a Single-Dimensional Array but with an extra pair of square brackets ([ ]) for every dimension of the array.Let's take a look at the below-given example -
int two_d[ ] [ ]= new int[4] [5];
And now when the compiler will reach this piece of code, it will create a 4 by 5 array and assign it to the two_d.
How to initialize a Multi-Dimensional Array?
Initializing a Multi-Dimensional Array is similar to initializing a Single-Dimensional Array. Just like Single-Dimensional Array, it also requires the index number of the places where it needs to be placed.Now let's take a look at the below-given program, here we have an array of 4 by 5.
Your Turn!
Now it's your turn, tell us what do you like about this amazing way to store a vast amount of data under a single name. Try these programs on your own because as we all know, "Practise makes a man perfect!".
Thanks for being here!
No comments:
Post a Comment