declare

Java Tutorial - 20 - An Array of Objects



Sharing buttons:

hey guys absolute zero here and today

we're going to be talking about how to

make an array of objects in Java so up

to this point we've covered some basics

of object-oriented programming and I've

also taught you guys part arrays so now

we're going to put the two things

together and I'm gonna show you guys how

to make an array of objects so I have a

car class set up and all this class does

is it has a model variable which is a

string so we're gonna be able to

basically say what the model car is and

we can get the model and we can set the

model it's a really simple example but

that's all you guys need to understand

this concept so if we come over here the

way we declare an array of reference

variables is the exact same way we would

declare a normal array so if you guys

remember we do something like int Nam's

equals new int and then we would say how

many um elements we want in the array so

let's say we wanted 10 moving for the 10

and now we have our array of int well it

works the exact same way except you give

it the type of the reference variable

you want to use so our classes cars we

want to have it named car and maybe

change the name of the array to make it

more appropriate so basically this array

is going to hold an array of reference

variables to the type car now remember

this is really important I know I said

it was an array of objects but it's

really an array of reference variables

the reason that's important to remember

is because a reference variable points

to an object on the heap and that's

gonna help you guys out a lot later on

if you guys realize that reference

variables aren't objects so keep that in

mind so the way we assign values to it

is the same as any other array so we go

cars so let's say the first element in

our array will send equal to a new car

pretty simple we're setting it to equal

to a new instance

of the car class and we could do that

for every single reference variable in

the array but what we can also do is we

can create a for loop to cycle through

the array of objects just like we would

a normal array so what I'm going to do

is I'm going to import the scanner I can

type right today

Java dot util dot scanner that's better

and we're just going to create a new

scanner

there we go now Barbara dues were to

create a four loop that will cycle

through every single reference variable

in our array so what we're gonna do is

already go for int I equals zero

remember raise our index by zero even if

there are an array of reference

variables so it starts at 0 and we want

I is less than cars dot legs now

something I didn't mention about dot

length what I talked about for loops and

stuff before is it returns the length of

the array so the length of the array is

10 but it stops at 0 or starts at 0 and

stops at 9 that's why we don't go less

than or equal to because then our for

loop would try to go to cars 10 the 10th

element of the array but it's really

cars 9 so we want to go less than so we

don't get that problem and we're gonna

increment by 1 because we want to go for

every single element in the array pretty

simple now what we're gonna do is I'm

going to create a string variable and

call it temp what we're gonna do is

gonna go over here and we're going to go

temp actually we should probably give

the user prompt sy s oh and that's just

a little shortcut for you guys I didn't

show you that before but you know sys oh

and hit ctrl spacebar and it'll print

system dot out print line for you enter

the our model please pretty simple and

now all we want to do is go um excuse me

temp equals input dot next line I'll

just go next doesn't matter type X and

then what we want to do is we want to go

card cars there we go I is equal to a

new instance of the car class and then

what we want to do is you want to go

cars I dot set model temp pretty simple

and now what we're gonna do is going to

go down here we're gonna create a for

loop to print the model of each car so

what we're gonna do is we're gonna go

for ITI equals 0 I is less than cars dog

lengths and increment by 1 again and all

we're gonna do is go sy s oh the our

model that works we'll do that model

plus and we can call on the methods this

way cars the index we're at dot get

model pretty simple and I'm just going

to make it so there's a lesson there

I'll put it to like three and I'll just

show you how this works

expand this a little bit so you guys can

see enter the model please f150 our

dough I'm not into cars um

Porsche probably spelt that wrong and

then we'll have GMC I don't know and see

works just fine so guys that's how you

create an array of reference variables

in Java I really hope you guys enjoyed

the video you'll have to excuse the

background noise there's some people

walking around my house and I do have a

bit of a cold so I'm sorry for that too

but anyways I hope you guys learn

something

and until next time as always stay

frosty I hope you guys all have a great

day