declare

#5.1 Java Tutorial | Array 1D | 2D | Jagged Array



Sharing buttons:

Oh

[Music]

so once we have seen the theory of a day

so we have talked about one dimension a

day we have talked about two dimensional

array and why we even need an a day

because if you have multiple elements

and you want to ensure storing those

elements in two different variables we

can use one common variable where you

can store all the values so let's try to

implement that in a practical scenario

here so let's say I want to create an

array of integers so I would simply say

I have to say integers ain't I mean I

will use end of nums

equal to in Java added an object so we

have to use a new keyword and we'll use

a int to specify the size of an array

let's say I want an array of four

elements now it is composite to define

the size of an array because if you miss

to define the size of an array it will

give you error now once you say for this

size is fixed as soon as you write this

line so inside your inside your JVM you

can see in your heap memory something

happens in this way

so inside your heap memory you got this

you got this box where you have these

four boxes so the first box will have 0

in fact all the boxes will have 0 is

because since we are getting adding of

int so if you create or adding like that

it will create add it off and with all

the values as 0 just to prove my point

what I will do is I will I will print

the values so if I want to print the

values I would simply say system dot out

dot println let me pick up any value so

let's say I want to pick up the third

box there the third box index number

would be too because index number starts

with 0 right so you can see the first

box is 0 then we have 1 then we have 2

then we have 3 so I want to fetch the

third box which is this - which is -

right so if you if you write - you'll be

getting the output which is say we are

getting 0 now why 0 is because the the

value by default is 0 now let's I don't

want the values to be 0 I want my own

values there so what we can do is we can

specify the values individually here so

we can say in nums of 0 i want to assign

the value let's say 8 then we can say

num start 1 we can specify 12 we can say

nums

of 2 we can specify 76 and I mean any

value will do here I'm just specifying

the values so if I say 3 let's say 54

just to have that all numbers

not in sequence so we have 812 for 76

and 54 so we have all this numbers and

you're fetching only two here so if I

fetch two it will of course give you 76

right because we are fetching the value

of this second element which is of

course I mean third element which is in

terms of two so we called 76 now we can

also change the value I mean we can

change the value right once you define

it we can also change it we can say nums

of two we can again change the value

because anyway this array is variable

right so you can change the values of it

so as soon as I write this line I mean

after this line you can see the box

there it has value eight 12 76 and 54

but as soon as you line write this line

you can you can see the change there so

now 76 becomes 99 and that's why when

you fetch this value it will give you 99

simple but what will happen if I try to

print so let's say I don't have I don't

have a value at forth location because

the size of my array is 4 and I'm trying

to access the fifth location now and you

can see outside the box we have nothing

so I put on this code it will give you

an array which is array index out of

bounds exception because there is

nothing nothing after that box right it

only has four elements and you're trying

to fetch the 5th element here so what if

I assign our values to norms of 4 equal

to 77

will it work now you can see while if I

run this code now you can see we got the

error on this line itself on line number

13 we cannot assign the value there now

let's say I have assigned all the values

I want to print all the values now to

print all the values we have to say

number of 0 then we have to say num nums

of 1 then you have to say in terms of

two and I'm sorry right so sure if I

think four lines what we can do is we

can write only one line here we can say

for int I which will start with 0

because adding starts with 0 and will

end I it will give a semicolon there

else will say end at 4 because we have

to end at 3 right so we have to say less

than 4 we don't have to specify equal to

sign there and then we can simply say

nums of i so it will print all the

values i can as you can see we got we

got 8 1276 and 54 so using a for loop we

can print all these all the values here

in fact in the next video where we will

also talk

would enhance for the witches they knew

not knew exactly it was it is there from

a long time so we got one more way of

printing the values from default from

the Attic called as enhanced for-loop

we'll see that later okay so this is how

you use a one dimensional array so this

is one D added now there's one more

thing here when one when you know the

value is predefined when you already

know the values instead of saying new

int we can also specify the value here

itself we can say 8 comma 12 comma 76

comma 54 so if you know the value spree

hand instead of knitting this this thing

here we can directly assign the values

even if I long this code now it will

give the same output so that's how you

can create an array one dimensional

array you can print it in fact we can

also create the object I mean the array

of objects example if I have a class

here and if I have class student' and I

want to create in this student to have

two things we have a roll number and we

have string name what we can do is we

can create multiple objects here of

student we can say student s1 equal to

new student that's the first object I

have again we can specify the values

there but let's see if I create multiple

objects here if I'm creating four

objects this is as true this is s3 this

is s4 so if I'm creating four objects

here I can create instead of that I can

create the object of students I will say

as I really use a square bracket then I

will say new student okay I specify I

have four students here and in this s in

fact we can issue of writing this line

we can also specify s 1 comma S 2 comma

S 3 comma s4 ok so we are creating the

added off students so I will recommend

you to do some experiment with this you

will get the exact point what I'm

talking about in fact you can also apply

a for loop here and you can do that also

so once we got the array of student let

me remove that so this is your one

dimensional array and we have seen how

to use it in the next we'll talk about

two-dimensional arrays

now once we know what is one dimensional

array let's move towards two-dimensional

array so just to define that what I will

do is so let me create an array let's

say this array is let's say a so I have

this a array and which has a certain

value so let's say I want to assign a

value which is 1 2 3 4 so I have this

array which has four values right so

instead of creating 4 variables so

instead of creating 4 variables I'm

using one addy

now let's say we have one more array

which is B and the values of B is 2

comma 4 comma 6 comma 8 and then we have

one more array here which is C which has

let's say 5 comma 6 comma 7 comma H so

these are the values I have so I have 3

arrays and we have 4 values in each

receive instead of creating 4 variables

I have created one array right so

instead of creating 3 arrays can I

create one big adding we can do that so

you can see we can create a table we'll

name this as D now this D is a

two-dimensional array because this will

this is an array which will not have

simple values so D itself will have

small small arrays so D will have a

comma B comma C and the values of a is

not just a it is so that's your first

array then in the second array what we

have is and the next array which you

have here will have values which is 5

comma 6 comma 7 comma 8 right so we got

this two-dimensional array which has

array I mean we which itself has an

added ok so now what if what if what if

I say system dot out dot println D of 0

so when I say D of 0 it is representing

the whole array here okay so Diop 0 that

means the first row so you can imagine

this thing as a row and column so this

is first row second row third row and

then we got columns so we got 1 2 5 2 4

6 these are columns so if I say D of 0

it is the whole row right so I don't

want the whole row I want this second

element from the first row so I can

specify the second element as 1 so first

row second column so we have to specify

0 comma 1 and if i underscore you get

the output as - okay that's that's the

way

if you want to put into all the elements

what we can do is we can use a for loop

and now this for loop so the outer for

loop we'll use this for loop for the

number of rows to count a number of rows

semicolon less then we have three those

rights are going to use three and we say

I plus plus define the number of photos

and we require one more for loop there

which is in J equal to zero and J less

than equal to not equal to let's say I

have four and then J plus plus so the

inner loop will be counting the number

of columns so this loop will count the

number of rows and this local corner

number of columns and now if I want to

print so I will not use println I will

only use print to specify the particular

form because if I use print L then what

will happen is if I specify the of I and

J which will change in some time if you

run this code and if you if you run this

you can see we are getting all this

output here

I don't want output in a linear format

so what I want is here it should print

only in the same line with some space of

course and after this inner fondo we can

print a new line so that we can get the

perfect structure so you can see we got

one two I mean we got the same blocks

here right now how exactly is walking

just let's run through a loop so you can

see this image we haven't added there so

in this array in this box I mean we have

a square box where you have three rows

and four columns so all the values by

default are I mean we have all the back

all that if all the values there one two

three four then all these values so

initially when you have I and J so I

value will be 0 and J value will be 0 so

it will point to the first box which is

1 so it will fetch the value as 1 then

the loop increments your J becomes 2 I

mean J becomes 1 so it is fetching 0 1

which is the first row second element

which is 2 and then your J becomes 2 it

will fetch 3 your J becomes 3 it will

fetch 4

now once it once you are fetching for

you can see we don't have any element

after that it will jump to the second

row so when you say your jump into

second row that means your I value now

becomes 1 and then we can again your J

value becomes 0 so you are fetching 2

then you can cream in increments it

fetch 4 and then 6 and then 8 so that's

how it will fetch all the values from 2

a dimensional array so in

two-dimensional array we can have the

address of same size

now what if I if I change the size if

what if I say this is six okay I'm not

sure I mean in the first row I have four

elements in the second row I have three

elements and they fit in the third row

let's say I have I have five elements

now in this scenario since the number of

columns are not fixed we cannot say for

year right because the number of rows

are fixed so we can say three we cannot

say for year so what we'll do

I will say D of I I mean for that

particular row so if I say D of I so it

will be either this or that will depend

upon the I value I want to fetch the

length of that row and it showed three

year we can also say D dot line so it

will give the number of rows to you so

it will give number of rows I mean the

number of arrays you have first add a

second array throat array so it will

give you three and here depend upon on

which added you're working with or if

you are working with the first array

when your I value is 0 it will return

for if your value is 1 it will return 3

when your I value is 3 it will return 5

and if I'm this code you can say this

you got this type of output so this type

of array where you have the number of

columns are not fixed are normally

called jagd

arrays so if you have all the columns as

same that means it is it is normal to D

adding but if you have different size of

the column that will be your jag daddies

so the only precaution you have to take

his this precaution here ok so that's it

that's how you use two dimensional array

and jag daddy

in the next video we'll talk about

enhanced for-loop so that's it thank you

so much