2D Arrays in Java Tutorial

Sharing buttons:

2d arrays in Java can be kind of hard to

understand especially for beginners so

in this video I'm gonna teach you how to

use 2d arrays in Java and do an example

of how to use 2d arrays for a lottery

ticket hey it's Alex back again helping

you code quicker and easier with program

examples on this channel I make

tutorials just like this all the time so

if you're new here consider subscribing

so you may have heard of 2d arrays and

you may have thought what the heck am I

using these for is this just supposed to

make my life a living hell for the exam

well that's exactly what I thought when

I first started but there's actually a

good reason why you're learning it the

screen you're watching this video on is

a 2d array of pixels probably 1920 by

1080 or something like that and each of

those pixels has its own RGB values

making it light up different colors and

make different patterns on the screen

and having a set of rows and columns of

RGB values like that makes it really

easy for computers to refresh all the

time and show videos like this so

anything with rows and columns can be

stored as a 2d array it's like having a

quick table you can access all the time

so let's do that lottery ticket example

of 2d arrays in Java I did a video

covering normal arrays in Java

just 1d arrays so if you want to check

that out you can click it here on the

link in the card and it's also in the

description so let's go ahead and make

this lottery ticket example so go to

file new Java project we'll just call it

our lottery ticket ticker pick it and it

finish then go under it click source go

to new class and this will set up our

Java file we'll just call it lottery I

cannot spell the date guys

lottery ticket ok and public static void

finish boom we're done ok now we

actually have to code it to make a 2d

array in Java it's really simple first

we think about what values is it gonna

have well our lottery ticket usually has

integers so we'll do integers so we just

type int and then 2 boxes just like that

and we'll name it our lottery card and

we'll set that equal to the values in

our 2d array of rows and columns so

we'll just slide those in some curly

braces and we'll do the first row the

first row has curly braces 2

and we'll just set up our numbers well

have it be 3x3 of just some random

numbers and we might have like 7s going

diagonally we have our first row here

and then at a comma and just for

simplicity sake we'll try that I'll line

it up a little bit at the second row of

numbers just like this okay we have our

second row and then the third row just

some more numbers like that and you just

store it a 2d array now that's one way

to do it you can also make a 2d array in

Java just like this we'll call it our

lottery card too

and we actually just set the dimensions

here so we say it's three rows and three

columns wide and we actually have to put

the keyword new here please excuse the

really loud train outside its State Fair

season and I live on the fairgrounds so

I have to sit in 30 minutes of traffic

just to turn right into my house with

this we have the problem with this way

is that in order to store the values you

have to manually do it like this I would

set the first row and the first column

value to 20 and then I go in here again

and say the first row the second equals

15 and you'd go all the way down and you

to end up with something like this the

numbers inside of these boxes or the

square brackets are the index and index

is always start at 0 so that's why you

see 0 0 is 20 like that and then 0 1

which is actually the second one is 15

and it will always go row-column and one

way I can remember this is array starts

with a row so that might help someone

know so let's prove to ourselves that

this is actually working let's print

some stuff to the screen and we'll print

let's say the first

value so let's try to get this 20 out of

the law degree card 2d array so we would

let's make this a little bigger as you

can see so we get our lottery card which

is the name that is actually this 2d

array we could just refer to it by the

name and then we would go into it by

typing our square brackets go to the

first row and the first column and if we

run this we should get this 20 let's try

that now we get the 20 if we wanted to

print out all of the values here I'm

just gonna add some space here for the

next thing we're gonna do if we want to

print every single value out of a 2d

array we can go through it with

something called a for loop I haven't

gone over this on my channel don't worry

about like how to type this just follow

my lead just like that

Cody nerdy stuff there decoding stuff

don't really worry about what's going on

I'll explain everything

okay here's an example of printing out

multiple values out of your 2d array now

this won't get every value but I kind of

just want to show what the idea is here

so let's save it and run it we see our

little divider we print it out and then

we see 27 and 47 and that's actually

this diagonal right here I'll be going

over for loops in a separate video so

don't worry about this but in a nutshell

we're pretty much running this code

three times and this variable I is

changing we're getting the element 0 0

and then in the second time we're

getting element 1 1 and the third time

we're getting element 2 2 so that's why

it's putting this diagonal 27:47 and

then 2747 ok let's try to get out all of

the elements in this array I'll add

another divider so instead of having IV

the same one this will print diagonal we

want to indexes so we can go all the way

across and it's kind of hard to

understand don't really worry too much

if you don't get the whole thing you'll

get it eventually

because I'm gonna make sure that

everyone on this channel gets the proper

coding knowledge that they deserve

because I know when I was learning how

to code all this was like so foreign to

me I had no idea how like the TAS or the

teachers did it it was just it was

really hard for me so that's my goal on

this channel to try to help you guys out

ok and now here we'll go into our array

and put I and J this piece of code right

here will print out everything on the

first row than the second row then the

third row so if we save it and run it

we'll see that a third little divider

here you got 2015 7 and let's see if

that's right yeah we got 2015 7 and then

8 7 1987 19 and 7 13 14 15

so this is just a little example of a

3x3 lottery card with like the 7s going

agonal II and we won on the jackpot

question of the day do you have any

assignments or projects related to 2d

arrays in Java connect with me in the

comments and engage with this community

of new programs just like you make sure

to subscribe so you see all my videos

the day they come out if this was

helpful smash that like button and share

it if you think it might helps when you

know you could be anywhere in the world

but you're here with me and I appreciate