Arrays

Learn the Basic Java Concepts Loops, arrays and methods
14 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.44
List Price:  €93.49
You save:  €28.05
£56.04
List Price:  £80.06
You save:  £24.02
CA$95.74
List Price:  CA$136.79
You save:  CA$41.04
A$107.11
List Price:  A$153.03
You save:  A$45.91
S$95.36
List Price:  S$136.24
You save:  S$40.87
HK$547.90
List Price:  HK$782.75
You save:  HK$234.85
CHF 64.03
List Price:  CHF 91.47
You save:  CHF 27.44
NOK kr772.60
List Price:  NOK kr1,103.76
You save:  NOK kr331.16
DKK kr487.88
List Price:  DKK kr697.01
You save:  DKK kr209.12
NZ$117.84
List Price:  NZ$168.36
You save:  NZ$50.51
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,700.43
List Price:  ৳11,001.09
You save:  ৳3,300.65
₹5,837.33
List Price:  ₹8,339.41
You save:  ₹2,502.07
RM333.81
List Price:  RM476.90
You save:  RM143.08
₦92,489.99
List Price:  ₦132,134.22
You save:  ₦39,644.23
₨19,474.37
List Price:  ₨27,821.72
You save:  ₨8,347.35
฿2,588.61
List Price:  ฿3,698.17
You save:  ฿1,109.56
₺2,275.23
List Price:  ₺3,250.47
You save:  ₺975.23
B$358.08
List Price:  B$511.57
You save:  B$153.48
R1,312.98
List Price:  R1,875.77
You save:  R562.78
Лв127.91
List Price:  Лв182.73
You save:  Лв54.82
₩96,452.39
List Price:  ₩137,795.03
You save:  ₩41,342.64
₪267.73
List Price:  ₪382.49
You save:  ₪114.75
₱4,034.64
List Price:  ₱5,764.02
You save:  ₱1,729.38
¥11,065.06
List Price:  ¥15,807.91
You save:  ¥4,742.85
MX$1,201.03
List Price:  MX$1,715.83
You save:  MX$514.80
QR255.05
List Price:  QR364.38
You save:  QR109.32
P965.79
List Price:  P1,379.76
You save:  P413.97
KSh9,296.24
List Price:  KSh13,280.92
You save:  KSh3,984.67
E£3,349.82
List Price:  E£4,785.67
You save:  E£1,435.84
ብር3,985.10
List Price:  ብር5,693.24
You save:  ብር1,708.14
Kz58,375.85
List Price:  Kz83,397.65
You save:  Kz25,021.80
CLP$66,375.71
List Price:  CLP$94,826.51
You save:  CLP$28,450.80
CN¥507.11
List Price:  CN¥724.48
You save:  CN¥217.36
RD$4,113.13
List Price:  RD$5,876.15
You save:  RD$1,763.02
DA9,392.53
List Price:  DA13,418.47
You save:  DA4,025.94
FJ$158.35
List Price:  FJ$226.22
You save:  FJ$67.87
Q544.06
List Price:  Q777.27
You save:  Q233.20
GY$14,678.83
List Price:  GY$20,970.66
You save:  GY$6,291.82
ISK kr9,837.79
List Price:  ISK kr14,054.59
You save:  ISK kr4,216.80
DH708.72
List Price:  DH1,012.51
You save:  DH303.78
L1,246.75
List Price:  L1,781.16
You save:  L534.40
ден4,025.16
List Price:  ден5,750.48
You save:  ден1,725.31
MOP$565.76
List Price:  MOP$808.27
You save:  MOP$242.50
N$1,340.22
List Price:  N$1,914.68
You save:  N$574.46
C$2,582.07
List Price:  C$3,688.83
You save:  C$1,106.76
रु9,353.99
List Price:  रु13,363.42
You save:  रु4,009.42
S/263.57
List Price:  S/376.55
You save:  S/112.97
K270.82
List Price:  K386.91
You save:  K116.08
SAR262.50
List Price:  SAR375.01
You save:  SAR112.51
ZK1,859.31
List Price:  ZK2,656.27
You save:  ZK796.96
L325.94
List Price:  L465.65
You save:  L139.71
Kč1,644.39
List Price:  Kč2,349.23
You save:  Kč704.84
Ft25,720.62
List Price:  Ft36,745.32
You save:  Ft11,024.70
SEK kr762.14
List Price:  SEK kr1,088.81
You save:  SEK kr326.67
ARS$61,174.36
List Price:  ARS$87,395.69
You save:  ARS$26,221.33
Bs486.59
List Price:  Bs695.16
You save:  Bs208.56
COP$277,528.94
List Price:  COP$396,486.91
You save:  COP$118,957.97
₡35,172.98
List Price:  ₡50,249.28
You save:  ₡15,076.29
L1,732.56
List Price:  L2,475.19
You save:  L742.63
₲520,857.74
List Price:  ₲744,114.38
You save:  ₲223,256.64
$U2,703.34
List Price:  $U3,862.08
You save:  $U1,158.74
zł282.38
List Price:  zł403.41
You save:  zł121.03
Already have an account? Log In

Transcript

Hello there, and welcome back to this job development course. So, last time we went over for loops, and you had some homework to do, your job was to write a program that would get a variable from the console, and then print the factorial of that variable. So here is essentially what you should have done. So first of all, I create my scanner to get the value from the console. Then I do int a equals scanner dot next integer, then I create a for loop, which creates an integer i is equal to a minus one. So if we are for is going to be equal to three, then I tell the loop to keep on executing while I is more than zero.

Then I said every time we loop, we're going to subtract one from my, then I just assign A to A times I was going to be first A times three, then a times two and then a times one which is what happened. tutorial is all about. Now, I can actually just do times equals that actually just makes the code a little bit less. So if you remember, this is an assignment operator that we went over, it does exactly the same thing. Alright, and then I just print a after loop is executed. So we can test this out, we can have five factorial, that's gonna be 124 factorial, it's gonna be 24.

And they're gonna have 10 factor, that's going to be 3,628,800. So there we go. It's pretty simple application. Nevertheless, it does take some mathematical thinking and logical thinking as well. But it actually is a very good program to understand how exactly you could work with data and how you could write programs in general. So yeah, anyway, without further ado, let's get into this lesson.

All right. So today we're going to be going over arrays. So let's create our array class. New class, let's call it arrays. And main as always, and finish. Alright, there we go.

Delete the comment as always as well. All right, so what is an array? An array is essentially just list. So how do we create an array? Well, we need a data type, as always, so let's say that we have an array of integers. So int, and then we open and close, square brackets, open close.

And then we need to give our array a name. So let's just call our array A and then we're going to assign it some value. So in this case, let's say in our array, new integer array, and now we need to input some number into these square brackets. So this number will be v length of our array. So let's use input 10. And there we go.

As you can see, there are no more errors. Now, let's assign a value to an array. So we can do a open brackets, zero. And remember, arrays, start counting from zero. So the first element in an array will be zero, not one, a zero is equal to two. Okay, there we go.

And now we can do system dot out, dot print ln AE zero. So this is how we get a value from an array. We can assign a value to an array by doing a zero equals two, or we can get a value from an array by just doing a zero. So let's go ahead and run this. And as you go, and there you go, you can see it prints to fix that. We'll get that since that's what we assigned to it.

All right. So, that is generally how we work with the race. Now, there are a couple things to understand. So first of all arrays can be of any type. So we have an integer we can have it be as long as well. It can be a short by string, boy Boolean, character anything.

An array can be of any type, since it can be of complex type like strings. We can also have it be of any complex type so like for example scanner All right, there we go. So, as you can see, we're just creating a new scanner array with 10 values, then assigning the first value to new scanner. Whoops new scanner putting system to As the place where whoever can get it from, and then printing the scanner that we put into a zero dot next int. So now if we run this, we get three. There we go.

All right. Now let's take a look at some possible ways we can manipulate arrays. So we can have an integer array A, it's gonna be equal to new int array with 10 values. So first of all, we're going to create a method which will fill the array with values from one to nine. There we go. All right, there we go.

So there's a method for filling the array with the values from one to nine. And now we're going to have a separate method for printing the array. Or I'm saying no, it's not. It's not a method. This is a loop. No, a method is a completely different thing.

This is a loop. So we're gonna have an array for a loop. for filling the array, and then a loop for printing the array. All right, there we go. All right, there we go. So now if we run this code, we get the values from zero to nine.

Since it starts from zero then just inserts into this, for example, the zero value of the array is zero, the first value and enters one, second two, and so on. So we could also if really want to do we can do math dot random, times 100, and then cast that to integer. All right, there we go. And so now we'll just get a random around values as you can see, alright. So that is how we use method to use loops to work with arrays. And you can see now how the for loop is starting to end, you're starting to understand how the for loop sort of interconnects with arrays and why we need the for loop for arrays, all right.

So, another message that we can, which we can fill arrays is actually without doing new end, we just do two curly brackets and then just the numbers that we actually want to insert. There we go. And now if we print the array, we get the numbers that we inserted, there we go. So when you do this, when you do it like this, it actually will just put set the length of the array as as many numbers as you introduced in this case, their length will be 1-234-567-8910 years the length will be 10. All right. Okay, so that is essentially how we work with arrays.

Now we can also have an array of arrays. So this works a little bit differently. So we can have an integer, and then an array of arrays. And then this we would assign as an array. And then inside we're going to put two arrays, each of which will have three values. There we go.

So as you can see here, this is just an array of arrays. So there really isn't, isn't anything difficult here. This is just a multi dimensional array. So we can get a value from the array by doing system dot out, dot print ln a zero, which will return this array here. And then from that array, we're going to get one which will return the second value or two. So now this will print to Alex If we do one one, this will get this array here.

And this value here, let's change this to 557. Just to prove the point here. So yeah, so it's gonna get this array and then this value, second value. All right, let's take a look. And there we go. 567 as expected.

So we can also use for loops in this array, although with here we're going to have to use a multi dimensional for loop as well. So we just do for int i equals zero, i is less than a dot length. I plus plus. And then for int i one is equal to zero, i is less than a, I dot length. And then I one plus plus, no, sorry, it's gonna be a one is listening. I think there we go.

And now System dot out dot print ln. Ay ay ay one. Alright, there we go. So now if we run this, we get 1233561. So this is, this is sort of the actual race that we, that we created here. So that's how we print a multi dimensional array.

So what do we do here for? Well, first of all, we declare a for loop with an i variable, which is equal to zero, then while i is less than a dot length, so it's going to be two. In this case, there's since there are two arrays, we're going to we're going to execute the loop, then we have another loop inside, that's gonna have an i one variable, which I want is going to be the less than a dot length. So for example, if I zero is going to get this array, and then this length was going to be three and then we just add one to it one every single time we loop and now it prints eight, I was going to get the array under I so for example, is saying at 050 and this one is as if I as one and then I want so just the value inside that array and result we get this printed array.

All right. And so that is it. How we work with a race. All right, so one last thing we actually should go over with arrays is that they don't really work when we print them. So if you want to print an array, we don't, we would have to iterate over the array and then print every single value. And iterating essentially is going to every single value and then printing it manually.

We can't do System dot out dot print ln limited, we can, it's just wrong A. So this will actually just give us this, this is just an N, the ID sort of of the array. So if you want to print the array, we would have to go over every single value of the array and then print that as well. Okay, and one final thing that we're just going to go over just this isn't actually really doesn't have much to do with arrays, if we wanted to print a multi dimensional array, but have each sort of value be on separate lines. So each array be on separate lines that first prints 123 and then on the next line prints, three 561 one We would have to do a system out System dot out dot print here and then plus, comma. And then here, we would have to do System dot out dot print ln.

There we go. Just an empty print line. And now if you run this, as you can see it printed correctly on separating lines 123335671 because System dot out dot print will print and then not break the line. So it just, it'll just print on the same line. And then when if if we print again, it'll print on that same line, but then print ln goes to the next line. Alright, so Okay, so now in this lesson, we've actually done quite a bit.

So we've learned the very basics of arrays. We've learned how we can use for loops with arrays, and we've also gone over multi dimensional arrays. Now, multi dimensional arrays aren't necessarily just two dimensional arrays. Multiple arrays can be infinitely dimensional. So I think Well, I mean, obviously, it's not infinitely many dimensional because every computer has its limitations, but just being we can have, you know, four dimensional arrays. So we can in here we can have an array.

And another array, I think array array. Right, there we go. So yeah, there can be infinitely infinite amount of arrays, you know, an endless amount of arrays. So yeah, it doesn't necessarily have to be just two dimensional. But most of the time, we're going to be using one dimensional or two dimensional arrays, either way. All right.

So anyway, let's find some homework. All right. So for homework, your job is to take an array of values which say that we have an array A and then just assign it some values, you know, just some value to really matter all that much. And your job is to print the average of those values. So for whatever our You get your job is to just print the average and it should be dynamic. So if I add more values, it should be able to automatically update the average without, you know, changing any of the code.

So anyway, it's pretty simple homework. You will be using for loops for this. So just keep that in mind. And yeah, without further ado, then I wish you luck and I'll see you next time soon.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.