declare

Java Tutorial - 01 - Declaring Arrays & Accessing Elements



Sharing buttons:

hello welcome to mastering Java volume

number four this is lesson number one

I'm Jason I'll be your host in this set

of lessons and Java programming what

we're going to begin doing in this

lesson is starting to talk about the

concept of an array in Java in fact if

you understand the concept of arrays in

the java programming language then the

concept will carry over to other

programming languages such as C and C++

there of course some differences along

the way but the basic concept of an

array is is very similar among

programming languages essentially an

array is when you have lots of very

similar types of data and you would like

to store them all in one place under

kind of one name so for instance I mean

if I wanted to if I wanted to if I had

five people in the room maybe five kids

and I wanted to store their ages let me

say I have a database well one way I

could do this I could say well Jason's a

kid in the room and he has a value of

five years old and I could have some

other person in the room that's four

years old and then I can have another

person in the room you know Dylan and

his his age could be something like five

you see you get the point I could have

four or five children here and I could

be storing their ages individually now

this is nice if I need to refer to every

person by their name but the thing is it

takes a lot of space to initialize all

of these guys and to allocate memory for

them and I have to come up with a

different name for every entry what if I

have a large collection of kids maybe a

hundred kids maybe I'm running a daycare

and I have like 200 children or maybe

I'm running the state of Texas and I

have a database with like like five

million children or something like that

it's gonna be silly to assign a name for

every single person there it makes a lot

more sense to organize the data into a

database I may not care about storing

the the variable names for their first

name but maybe I just want to hold on to

the ages in a nice organized group so

that's the concept of an array you want

to reserve memory for lots of different

types of data very similar types of data

at the same time and you can access the

individual elements of the array and

it's nice to keep everything all

together so for instance if I had let's

say five people in a room and I

wanted to store their ages without doing

individually like that I could declare

and say well everybody's age is gonna be

an integer I'm just gonna store whole

number years and I can name this array

ages now I need to tell Java that this

is an array and not simply a single

variable and so what I do is I put an

opening and a closing square bracket

there I'll come back to why we're doing

that in a minute so this is basically

reserving the name it's telling Java hey

I want to create an array it's gonna be

called ages but I need to tell it to

assign the memory for these five five

ages here so it's gonna look very much

like declaring an object in Java and use

the new keyword int Open bracket and

then you put the number five inside and

then you stick a semicolon at the end in

fact this is one of the main differences

between Java programming and other

languages such as C++ and so on in Java

arrays are really implemented as objects

and that that's kind of a behind the

scenes sort of sort of thing that may

not mean too much to you now but as you

study more and more Java you'll start to

need to know when things are implemented

as objects and when they aren't this is

why we haven't covered the concept of an

array until now because I'd like you to

understand what an object is and you

already have that background now from

from going and doing volume 3 of

mastering Java so what we have on the

left is basically we're telling Java hey

we want to create a bunch of different

places in memory that are gonna hold a

similar type of data of type integer and

the brackets are telling Java that it's

an array and on the right hand side

we're actually creating that object of

sorts and we're assigning it like this

and so we have the new keyword saying

hey assign memory for an object that's

going to be an array and that's gonna be

an array of type integer and the number

five means there's five elements of the

array I need you to get used to seeing

this declaration like this because this

is how you're gonna declare arrays most

of the time in java programming now up

until this point there's no values

associated with these five elements but

Java when it does initialize the the

clip when we declare the array like this

it actually puts zeros in

to the five elements here so let me show

you that if I wanted to print out these

elements I go system dot out dot println

and inside of here I could do something

like this the first element is well I'll

need to put quotation marks of course if

I want that to print to the screen and

I'll show you along the way what's going

on here I put a plus now if I want to

access the first element of the array

that we've just created the thing that

you need to realize is that array

indexing in Java always begins with the

the number zero so I would say ages

that's the name of my array open a

bracket and inside I would put 0 so what

this is telling you when you see the

name of an array with the bracket and a

0 as now I'm not really referencing the

entire array I'm specifically trying to

pull out that very very first element

you just need to remember that when you

count the elements in an array in Java

you always start counting at element 0

because that's just how that language is

set up so the very first element of this

array is an index of 0 so let me go and

hit run here let me save it hit run the

first element is 0 notice that Java did

that itself I didn't put the value of 0

in the first element Java does that by

default if I change this to second the

second element is and increment this now

I'm pulling out the very next element in

this array which is the second element

even though there's a number one here

it's referencing the second element let

me go ahead and hit save and run this

guy the second element is 0 so as I go

through here and increment this guy and

print out the initialized values for

this array that we just calculated that

we just created Java is gonna always be

putting a value of 0 in when it

initializes arrays let me take that off

now that's pretty boring we don't want a

value of 0 in our our array we want to

put our own data in there so we have our

own kids we want to put their own ages

in there all right so the way you do

that is after we have now declared the

array we have associate assigned to

memory for five elements and I can

simply reference them as I've done in

the print statement I could say ages

bracket 0 okay and then closing bracket

equals so let's say the first

child in my database is gonna have an

age of 4 years old semicolon what this

is doing is it's taking the integer

number 4 and it's putting it into the

very first element of this array and

then I can say ages 1 like this maybe

the next kids 5 years old ages maybe the

next kid this 3 years old notice I have

to terminate everything with a semicolon

like I always do ages 4 whoops skip

number 3 let's put 3 back there that's a

5 year old and then finally we have the

oldest kid in our group which is a 7

year old so I have element the first

element in this array the second element

the third element the fourth element and

the fifth elements I have five elements

of this array you always start counting

an element zero and I'm assigning this

information so if I hit save and I hit

run

I'm not outputting anything but I didn't

get any error so I'm assuming everything

went fine now if I want to print this

out I could simply say system dot out

dot print Ln I could say let's just do

it like this I could say ages zero like

that so I'm printing that element and

what I'm going to do now I want to go

and just run it hit run and see the

number four goes to the screen if I

wanted to be a little bit more clever I

could say the first child's age is space

and I can put a plus here so you

basically use these guys just like any

old variable any you can use them in

print statements just as you've always

done the first child's age is 4 all

right now I can take this and I'll copy

it ctrl C and I'll go ahead and paste it

underneath it and I could say second

child's age is and I'll increment the

array to the next value that's in the

array and I'll hit run the second

child's age is 5 notice that it's just

pulling out what I have already done

here let me go ahead and paste another

line another line in another line so the

third child that's going to be the next

element the fourth child that's going to

be the next element here and the fifth

child's age is

something like this the element zero

element 1 element 2 element 3 element 4

those are the five elements I have and

when I print everything out four or five

three five seven corresponds to

everything that I stored into this array

so basically you first have to declare

the array and this is this is basically

putting the name aside telling Java that

you have an array here you're basically

creating a new array object that has

five elements that's gonna store integer

variables and you're assigning us

together and so now you can reference

this array in reference the individual

elements and you store things in there

and you can also print things to the

screen use them in calculations and

things of that nature

now this array only has five elements

only has five elements what's going to

happen if we accidentally go to the next

element see let's try to assign the

value of five like this so this is

element one

well it's element zero element 1 element

2 element 3 element 4 this is five

elements this is we only assigned memory

for five elements now we're trying to

put another element on the end we

accidentally do that we hit run and

immediately the thing won't run and it

says array index out of bounds exception

when you see anything called an

exception in Java

basically that's javis word for it

encountered an error and it's trying to

tell you exactly what kind of error that

it has array index out of bounds right

so if you see something like that you

need to go back and look at your array

indexing and make sure that you're not

accidentally going over the end of your

boundary later on we'll be using for

loops to loop through array so you need

to make sure that your loop variables

don't extend past the end of your

boundary now if I change my my number of

elements that I'm allocating to six

elements then this will run just fine

because the first four were put in the

next guys put in and then I'm only

printing out the first five elements of

that array so I don't get any error when

I do it that way so that's really wanted

to go over and make sure that you

understand that when you go over the

edge of a boundary that's your

responsibility as a programmer to make

sure you're not doing that all right now

here we have declared and used these

variables we change this back to a five

let me go and change this to delete this

back

like this alright now the next thing I'd

like to do is tell you that you're not

limited to integers here another common

reason that you would declare an array

of similar information is for double

values so what if I were going to record

the heights how tall these kids are so I

have children here and here this array

is holding their ages that's integer

data now I want to record how their

heights in feet let's say so that's

gonna be a double value because it you

don't want to just round to the nearest

Foot you want to have some decimals in

there so I could say Heights and I have

to specify that it's an array with an

empty bracket and I'll say new double

open the bracket and I have five

elements and then I put a semicolon at

the end so the same sort of thing is

going on this is is basically setting

aside the name Heights it's telling Java

that it's gonna be an array it's gonna

hold double data and that's what's going

on the left-hand side of the equal sign

here I'm creating a new object it's

gonna be an array that's gonna hold

double data and I have five elements

this is kind of what you have to

remember to do so again

this is created and assigned in memory

values of zero everywhere because it's

initialized that way so let me kind of

scoot everything else down that I had

just done a minute ago and now we want

to store some data into this and I can

do it the same way I can say Heights and

the first element remember is always

zero in Java and let's say the first boy

has 4.7 feet or the first girl whatever

alright so what I'm gonna now do just to

speed things up I'll copy this and I'll

go paste paste paste paste I've got

additional lines and I'll increment the

index here zero one two three four

and I'll just change the data here so

it's not quite so boring you know six

point five that's a real tall kid you

know two point nine it's a real short

kid and you know 4.0 that's a

middle-of-the-road kid so here I have

the data here of course I can run this

guy I'm still printing out the ages from

earlier but I don't have any errors on

this are right here so now what I can do

is I can go and say

system dot out dot print LM like this I

can say the first whoops let me get some

quotations I can say the first child's

height is put a plus and I can reference

it just like it did the other one

heights and then I need to use the very

first element which is a zero now

whenever I hit run you can see the first

child's height is four point seven let

me go ahead and do a what we'll just go

down here and we'll put a couple of

spaces in its gonna put some new lines

and so that when I run it again I get a

nice line break between the heights that

I'm printing so the first height is four

point seven and I can do the same thing

in system dot out dot print Ln the

second child height is and then here I'm

gonna say Heights and I'm using the very

next element in the array which is a one

and then I have to terminate everything

with a semicolon and then I go and hit

run the second child Heights three point

seven which is the second element of

myarray so you see I'm not storing the

names of the children I'm not storing

any other information I just have a

block of memory assigned to hold these

Heights it's holding double value so

it's got to have enough memory allocated

to store the the double basically

floating point decimal information and I

just go and that can access element one

two three four now for four or five

elements it's not terribly useful to use

an array but if you have any kind of

data base at all you're always going to

be using an array of some kind because

it's just cumbersome otherwise now I can

keep everything associated with this

name which is called Heights I can

anywhere my code if I see this I know

that I'm have an array of very similar

data if I see arrays like this I know I

have an array of very similar data now

again what if when I'm printing this guy

out if I'm printing this statement out I

accidentally instead of a one I

accidentally put in eleven here see this

this array only has five elements so if

I hit run you get the same sort of thing

index array out of bounds exception and

it's telling you the number eleven it's

trying to tell you that you accidentally

put the number eleven in there

if you remember from before it had a

different number here that was what we

were trying to do last time when we went

over when we overflowed so you go back

to parents say well gee is yeah that was

clearly wrong and so the number 11 is

what I really want so so far you've

learned about the concept of an array

you've learned how to declare and sort

of instantiate the array and whenever

you get done with these lines here the

array is created and it's padded with

zeros everywhere you've learned how to

store the relevant data in the arrays

depending on what kind of data the array

supposed to hold and you've learned how

to print out that information with print

statements like this now there is

another kind of way to declare and

instantiate an array that I'm going to

show you right now and in order to do

that what I'm going to do is I'm going

to comment out I don't want to really

delete it but I want to comment out this

stuff here and I want to comment out

this stuff whoops we can go in undo that

this stuff here the reason I'm

commenting it instead of deleting it is

because I want you to be able to still

see what we did earlier when we were

doing this earlier so let me bring this

back up here let me just show you that

when I save it and I run it nothing has

changed other than the fact that the

data is just zeros everywhere because

now we just have the initialization Java

put zeros everywhere we don't store any

value so every value that we print is

just going to be zero notice these are

decimal points with a zero because this

array is holding decimal or double

values okay so here's the deal if you

have a very large array and you want to

use a loop or some other mechanism to

store the values then you just declare

it like this and then you can store the

values either individually by hand or by

a loop or some other method that you can

come up with the store the values but if

you want to put the values if you want

to if you want to store the initial

values into the array as you declare it

you can do that as follows so in order

to do that what I'm going to do is take

the right hand side of my ages array and

they delete it like that the left hand

side stays the same and then I'm gonna

open a curly brace and inside the curly

brace I can just put the initial data

that I want to store in this array 4

comma 5

3 comma 5 comma 7 and then I'll close

the curly brace and hit the semicolon

notice that this data is exactly the

same as the data we initially store it

in there in the same order alright and

let's go in here

save and hit run and see what happens

the - data has 0 still we haven't done

anything with that the age data is

printing out totally correctly notice

that I did not tell Java notice 4 here I

told Java 5 elements for this array I

didn't actually tell Java 5 elements but

it's smart enough to know that I put 5

elements inside of the curly brace so it

has to be a 5 element array so if I

accidentally come down here and for this

guy I'm trying to print out 44 for

instance that's gonna be the 45th

element of that array I'm gonna get an

array exception out of bounds because

the array is we wouldn't save it the

array only has five elements it knows

that because I separated them with

colons with commas like that inside of

the curly brace and I can do exactly the

same thing with this one I can open a

curly brace and I can put the data in

for point seven three point seven six

point five two point nine four point

zero so hopefully that's the same data

put some commas you can put spaces you

can do things like that's fine basically

four point seven three point seven six

point five two point nine and four point

zero let me hit save let me hit run and

now you can see that the proper data is

popping up down here as well so the

bottom line is whenever you already know

what data you need to put in there ahead

of time and it's just a few if it's just

a few elements or just a handful maybe

under ten then you can simply just list

them with commas inside of this curly

brace and that's going to tell Java to

initialize the array and put the data in

there and make it the same number of

elements as I have listed in there

alright but if I have a much larger

array that's gonna be very cumbersome to

do on one line then just declare it the

way that we did originally and then

later in your code you can just store

the values or maybe you can do it with a

loop or something like that I'll show

you a little bit later but in this

lesson we just wanted to introduce the

concept of an array how to declare it

how to store values into it and how to

print the elements out and that's the

most important thing just remember

remember

number array indexing in Java always

starts with element zero that's probably

the most common thing a beginner will do

accidentally put a one here thinking

they're pulling the first element out

you have to put a zero in and pull the

first element out once you get that

squared away and do some practice you'll

be good so on that note go look at my

exercises I have a couple of quick

exercises for you I encourage you to do

those make sure you understand this and

then we'll continue working with arrays

in Java