Java Programming Tutorial - 27 - Introduction to Arrays

Sharing buttons:

what's up YouTube welcome to your 27th

tutorial and this tutorial is on arrays

now what an array is is pretty much a

variable that can store many values in

it so um it's useful when you want to

have a variable that has a bunch of

related information and the thing about

an array is that it can only store

values of the same data type so if you

want to store a string and an integer in

the same array you can't do that so the

easiest way to explain what an array is

is just to build it and show you guys

how it works and what it does so I'm

going to do just that

the syntax for creating an array is

simply first thing if the type in is

what type of array you want to create

and I want to create an integer array to

hold integers so go ahead and press int

the next thing you do is name your array

and I'm going to name mine Bucky so it

doesn't get confused the next thing you

do is put square brackets and this is

right between the P in the backslash on

your keyboard and this is just so the

square brackets tell Java that you're

going to be working with an array if you

did this then it would just be a regular

variable but when you put the square

brackets in Java knows all right you're

about to make an array the next thing

you do is set it equal to new since it's

a new array in again or whatever type of

data you're going to store and square

brackets the only thing different is

inside the second square brackets you

need to put a number in there and this

number indicates how many values or how

many variables you're going to store in

your single array so I'm going to store

ten variables or ten values and if you

don't know exactly what's going on I'll

show you right now so now we created

array and it's named Bucky and I told

you guys that you can store ten values

in this single array name Bucky

interesting all right how do I do that

well since we created a variable or

excuse me an array named Bucky

we have something called indexes for

that array and each index allows us to

store a separate value and those indexes

begin with zero and go all the way to

nine and again your computer is kind of

weird because even though you made ten

elements doesn't start one and then go

to ten and actually start zero to nine

because your computer loves to start

counting at zero for some reason so our

zero with element and Bucky which is our

first element we can set it equal to any

value let's set it equal to 87 so you

don't keep it fused next you go ahead

and make your next index or your next

element of your array and set this equal

to some other value I'll set it in quote

543 and you do this all the way until

you get to the last one which is nine

let me just leave a couple out because I

don't feel like typing them all and now

once you did all of this and you set all

your elements of your array equal to

value you can use those elements in your

array and I'll show you how to use them

let's just go ahead and make a system

out print statement print line might as

well all you can do in order to use this

array instead of giving the variable

name you just give the name of the array

and type whatever index you want to use

so if I used Bucky 1 to print out what I

do is say alright what's the value for

Bucky 1 543 let's go ahead and run this

and look at our run screen 543 so that's

how you can use an array and again an

array is pretty much a single variable

that can hold multiple values depending

on what you typed so for example we made

this array able to hold ten value so it

was able to hold ten things I only made

three right here because I was too lazy

but you can make zero through nine which

will give you ten and again how you use

it just read the name of your array and

then write this little subscript let's

go to index inside and that's how you

reference it just like your reference a

regular variable now if we say in our

write an array wouldn't be really useful

if we need

type every single variable over and over

again well an array comes in handy let

me get rid of this stuff right here one

of the neat things about an array is you

don't have to type ten variables when

making your rate there's another way

that you can do this and let's just go

ahead and get rid of everything let's

tighten this up a little bit so she said

another thing you can do is make

something called an array initializer

and what this does is it initializes all

your arrays without having to type the

array name subscript value array one

subskribe array two sub subscribe gets

hard to even say so here's how you make

an array initializer go ahead and type

what type of array you want Bucky and

again don't forget the square bracket so

it knows we're working with array but

instead of typing new int n what you can

do is just simply add those little curly

bracket things and go ahead and begin

typing numbers in here two four five

seven nine and what this does is instead

of having to tell on your array how many

values will you want to store in it it

automatically counts how many values you

put in and assigns a scut excuse me a

subscript to each one of those so

instead of putting Bucky 0 equals to

Bucky one equals four Bucky two equals

five Bucky three equal seven Bucky four

equals nine it automatically does that

for you in this entire statement so now

whenever we use Bucky and say we wanted

to use two again it would be Bucky zero

Bucky 1 Bucky - so Bucky 2 would be

equal to five so let's see if we got it

right run it and as you can see it

outputs the number five so again this is

a different way that you can create an

array using an array initializer I don't

even know what the first thing is called

I guess it's just called

initializing or creating an array or

something but this is how most people do

it in create an array initializer and

then you don't have to go through the

process of typing book

e 0 equals yada yada yada you don't have

to do that a million times all you have

to do is type it in a nice little list

and it automatically does all the work

for you and again whenever you want to

use one of your values or variables in

your array just write the name of the

array in the number and again the number

is called the subscript or index I'll

usually call it the index or just number

so that is your quick tutorial on arrays

and how to create an array in order also

how to make an array initializer so I

hope you enjoyed it practice this a

little bit in the next couple tutorials

I'll show you how to use arrays and

programs and how they're useful and why

they're useful in applications so thank

you guys for watching don't forget to

check out my next tutorial and I'll see

you then