Java Essentials - Arrays in java
3K views
Nov 18, 2024
Java Essentials - Arrays in java
View Video Transcript
0:00
In this video we are discussing arrays in Java
0:03
We know that array is nothing but one subscripted variable. Against one variable consecutive memory spaces are getting occupied and all those memory
0:12
spaces will be holding a same data type value and all those locations will be accessed
0:18
using some index value or using some subscription value. So that is the basic concept of an array
0:26
So let us go for more discussion on it. Arrays are nothing but containers and arrays can hold some data of the same data type
0:36
In the arrays, there are two sections. One is the index and another one is the value
0:41
So index is actually denoting the location number within that array. Arrays are fixed sized
0:48
That means when one array is created, we cannot change the size of the array in the later on
0:54
So using the array index, we can directly use one element. without sequentially visiting all the elements until getting that target element value
1:05
So, using the index, I can randomly access one array locations content
1:10
I need not to do the searching sequentially because if I want to access the location
1:16
number 10's content, I can directly go to the location number 10 and retrieve the next
1:22
retrieve the respective locations data. So in the sub-single variable, a memory space will be occupied, some values will be
1:29
be stored how much memory will be occupied that will depend upon the size of the data type In case of array we having this index and here we having the values So this index will have the values ranging from 0 1 2 3 4 in this way
1:44
And the respective index, at the respective index, some values will be residing
1:49
And how many bytes are going to be occupied here? That will depend upon the data type of this data
1:55
So if it is integer type of data, in case of Java, integer occupies 4 bytes in the computer's memory
2:01
So it is having 1 2 3 4 5 consecutive locations so 20 bytes of memory space will be allocated for it
2:09
So let us go for one practical demonstration for the easy understanding of this add implementation in our Java coding
2:17
In this demonstration we are discussing how to use Java arrays So here we have defined one array integer type of array name of the array is I-N-T-A-R and this is one integer array and while we are declaring this
2:31
array we're also initializing width with our values so that means here we are
2:37
passing our values which will be filled up in this particular array and here
2:42
it will be having five values are stored directly in the array from the
2:46
subscript ranging from zero to four in case of Java the array subscript the
2:51
location number will be starting from zero so this is our I N-T-A-R-R
2:56
here we have defined one float array which will be having five consecutive
3:00
locations and it can hold hold only the flow type of data and we have to also define one character
3:05
array which is still uninitialized but it will have five locations to character to hold five different characters we having one scanner class object which has been initialized to take values from the keyboard as input this float array here we
3:21
initializing it we are just putting values manually assigning multiple different values that means
3:27
here the five values float values in the array and index will start from zero to four here because
3:33
we are having five locations and also using poll loop we can initialize one array
3:38
you can find that here i is a loop control variable so i is ranging from 0 to 4 because the condition is i less than 4 i plus
3:46
이라는 starting from 0 so the value of i will be ranging from 0 to 4 and then we are going for car a r r i is equal to
3:54
we shall be taking input from the keyboard using this scanner object next we'll actually return a string
4:00
and dot carrot at 0 that means the first character of the string will be taken as a character and that will be
4:06
filling up this card ARR array at the iath position at the ieth index and then we're
4:13
printing this integer array so for i will be ranging from 0 to 4 and then we i nt arr i will get
4:21
printed similarly we're printing this float array and also we are printing this character
4:26
array as well so now you can find that in this particular character at a printing we're
4:32
using the for each loop and also this float array printing we're using the four
4:36
each loop and to print this integer array we're using this four loop where I will be ranging
4:41
from 0 to 4 and I will get increased by 1 at a time in each and every iteration In case of 4 each loop you know that float item colon float array that means for each and every looping for each and every iteration one value will be picked up at a time
4:58
and consecutive values will be picked up in the order in which they are appearing in the array
5:03
And similar case will happen when we are picking up characters from this character array
5:07
one value at a time in an iteration, and the characters will be picked up in the order
5:12
in which they are, they are appearing in this character array. and then we are closing this scanner object also
5:18
So now let us go for the execution. You can find that. So enter the character for this index
5:24
So it shall go for H E L L.O. So we have given the five characters
5:32
Now the integer array has got printed. You can find that this is our integer array
5:38
So this is our integer array. The content is there. So let me go for input again
5:45
So the integer has got printed because it has got initialized when the array was defined
5:52
and then here we're having the respective manual entries are there so the values we're finding
5:58
in this float array and in the character array as according to the given input the values
6:03
have got put has got put in the respective index of this character array so in this demonstration
6:09
we have we have shown you that how to define one array how to deal with array and how
6:15
what are the different ways to initialize the array contents and what are the different ways to print the array contents
6:22
either using for loop or using for each loop and so on. Thanks for watching this video
#Computers & Electronics
#Java (Programming Language)
#Programming