declare

Java Tutorial - 14 - Arrays of Strings



Sharing buttons:

hello welcome to this lesson of

mastering Java lesson number 14 here

we're going to cover the very important

topic where we're going to combine the

concept of arrays that we've talked

about several times with the concept of

a string which we've been talking about

a lot recently so if you remember in the

past we have talked quite a bit about

the concept of an array and an array is

really just a collection of similar

types of data that are logically grouped

together so for instance I might create

an array and that might be of type

integer and I might just call it array 1

let's say and inside of that we learned

that there are several different ways to

declare an array but I can go ahead and

put the values that I want to be stored

inside of that array just by putting

them separated by commas here right

inside of curly braces

now of course to specify this as being

an array that's going to hold multiple

values we need to put our open and close

brackets and so now we have not just a

single variable that's going to hold a

single number but we have in this case

we've got an array that's going to hold

one two three four five values Java

knows to set aside five memory locations

of type integer because I've actually

specified and told Java to go ahead and

do that here and and I've listed the

numbers and so it knows how much space

is required now what we can do is

exactly the same concept with with

strings as well so I can do something

like create a string array right so I

can create an array to hold strings now

just like for the integer I had to tell

at what type of data was gonna be stored

in this array so here I'm typing string

for the same purpose and I'm going to

put string I'm gonna name it sentence

right I'm gonna put my double bracket

there to indicate that this is indeed an

array and inside of here I'm gonna put

an opening curly brace and I can

actually specify the strings that are

going to be inside of each element of

this array so for instance the first

string might be I and then the next

string might be love and then after that

too

travel to far away places so here we

have an array in the name of the array

is called sentence and each element of

this array is a separate string so

element one is this string element two

is the string element three is this

string element four is this string

element five is this string all right

now don't forget though that strings and

arrays are always when you start

counting an array you always start

counting the first element is element

zero so this is element zero of the

array element 1 element two three and

four so if we want to access the

individual elements of the array that

we've named a sentence it's very easy to

do we can say string or system dot out

dot print Ln like this and then simply I

can say sentence bracket 0 this is

exactly the same thing we did when we

were accessing array elements it's the

same thing it's just that element Zr now

contains a string so if we run this

we're just going to get the letter I

printed all right and then if we of

course add to that a space and then add

to that sentence bracket 1 and then

we're going to get we wouldn't hit save

and we wouldn't hit run we're going to

get the first two elements of this array

which is the first two words so we can

continue this whole thing and put

another space here and then put sentence

two and then plus plus sentence 3 so 0 1

2 3 let's see where we're at I love to

travel too far away and then of course

the last thing plus bracket or I should

say blank space sentence bracket 4 and

that's gonna be the last element in that

array and of course there's a error

there and that's because I forgot to put

it plus

there so basically anytime you have

things in the print stay but you have to

have pluses separating all of the

different terms I love to travel to

faraway places so this looks like we've

just done one single print of one single

item but really what's happening inside

of this is I'm accessing the different

elements of the array that I have

created by creating this array called

sentence and the print what's happening

is this guy is referencing the first

string this guy is referencing this

string and so on and I'm just putting

spaces between everything so it's a

little bit more complicated what's

happening behind the scenes then you

might imagine first in the first place

now of course I can change the way this

looks right very simply by just changing

how I've initialized my guy so instead

of faraway places I could say far away

countries if I want to put a period for

instance I'm gonna go and hit save and I

can hit run away run again and so I can

of course see that I've reflected that

change but let's say that you'd you want

to change the last word and the sentence

after I've already initialized like in

other words it's pretty inconvenient to

come back here and change how I've

initialized this guy so instead of doing

that let's go ahead and just change just

that last term so the way I can do that

is accessing that element of the array

sentence bracket for because it's the

fifth element of the array is equal to

and I can just change that from

countries to I could say far away

continents notice that I have had to

enclose that in quotation marks because

I'm trying to store a string into this

element of the array that is illegal

because this array is built to hold

strings so this is just like any other

assignment if I'm going to assign a

different number to an element of an

array that holds integers now what I'm

going to do now is I'm going to take

this very long guy here and I'm gonna

ctrl C to copy that and I'm gonna paste

the same print sentence underneath and

so we'll see how the two things change

and we hit save and hit run and you can

see what happens the first print

statement is made right after the

Declaration of the array after I've

stored everything in here and I get the

first guy then I just modify the

the last string there and then I can see

how that is reflected as I go and and do

that and print it out a second time so

the bottom line is you can create arrays

of strings you can initialize the

different elements in that array

whenever you create it just like you can

do for any array and then you can print

them you can make changes you can access

them using the same notation that we've

always learned for accessing elements of

arrays now the other thing I want to

show you is in this case I've created

this array here and I've put the strings

in there as I have created the array let

me show you how to create the array and

then update the contents of the array

after the initialization so let's create

another string array and this time we'll

just call it array 2 right I'll put the

double bracket which means this is an

array now in this case we put the curly

braces and we've told Java exactly what

we want to go into the array but in this

case let's say we only want three

elements of this array but we don't want

to put the strings in there just yet

let's say we want to create a new array

of type string a new array of type

string and then we have to open our

brackets and tell Java how many memory

locations we want to reserve in this

case we put the number 3 so the way you

read this is you're like okay I want to

create an array that told string

elements the name of the array is going

to be called array 2 the brackets tell

Java it's a one-dimensional array and

instead of putting the actual items into

the array at the outset I'm just going

to reserve the memory by creating a new

string object this follows the same

convention as creating objects of type

string which is an array of 3 elements

so in this case I've just made the

assignment on the right-hand side at the

outset here I'm just setting aside the

memory I'm going to make the assignments

later so let me tighten up this ok and

then over here on the left-hand side

underneath let's go ahead and let's make

some assignments here so I can say array

2 that's the new array that we've

created the 0th element which is the

first basic element in the array I can

make it and set it equal to buttercups

are awesome right and then I can say

array to the next element

I do love to take walks in the park and

then the last one array to element 2 and

I could just make this a simple you know

I love spaghetti which is true I do love

spaghetti okay so that I can have

something like that now what I'll do

here is I'll show you that this compiles

just fine we hit run everything we run

just fine and I can go ahead and put a

print statement system.out.println and

inside of that I can just say array to

bracket 0 that's the element 0 of this

array and when I print that out what I

get is buttercups are awesome and I can

take this guy let me hit copy just to

speed it up copy and paste again and

then put element 1 in this one and

element 2 in that one at that at that

index I should say 0th index 1 index and

2 index so I'm basically looking to

print out everything that I just stored

in that array now let me show you if I

accidentally say array to bracket 3

equals butterflies and I close my

bracket as I properly should here then

let's see what happens I'll hit save and

then I'll hit run I'm going to get an

exception it says array index out of

bounds right here because of course I

only told Java that I wanted three

elements in this array this is the first

element this is the second element this

is the third element this would be the

fourth element I've never I've never

reserved enough memory to do that now if

I actually change this to a four then

it'll compile and run just fine so let's

change that back to a three the way it

was let's take this back the way it was

and let's copy one of these print

statements and put that down and you'll

see the same thing happen if I

accidentally try to print an array

element outside of bounds I'm gonna get

the same kind of thing so that's

basically what I wanted to teach you in

this lesson we've learned about the

concept of arrays they're very very

useful for grouping information that is

in nature we've mostly been focusing on

integer raise double arrays and so on

but now we're combining the concept of

an array with the concept of a string so

here we can create an array that's what

the brackets and the name means of type

stringer to hold strain elements

everything in here must be associated

with a string now you can declare it and

put the stuff in there when at the

outset or you can just declare the array

as an object as you typically would and

then you can make the assignments later

so this is very very useful for things

like storing maybe names and addresses

maybe the 0th element will be the name

the next element would be the address

the next element after that will be the

phone number and so on so you can group

information accordingly there are many

many many uses for arrays you access

them using the indices here the thing

you need to remember is that arrays

always start counting at zero so don't

don't let that screw you up if you're

gonna loop through a loop you gotta

start counting at zero to catch all of

the elements make sure you understand

this then follow me on to the exercise

where you can get some practice for

yourself and make sure you understand

this because arrays and strings are like

peanut butter and jelly you use them all

the time make sure you're comfortable

and then follow me on to the next lesson

and mastering job