Arrays

Complete Step By Step Java for Testers Java Concepts - Data Types
15 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€92.30
List Price:  €129.22
You save:  €36.92
£79.14
List Price:  £110.81
You save:  £31.66
CA$135.92
List Price:  CA$190.30
You save:  CA$54.37
A$153.17
List Price:  A$214.44
You save:  A$61.27
S$134.71
List Price:  S$188.59
You save:  S$53.88
HK$782.26
List Price:  HK$1,095.19
You save:  HK$312.93
CHF 90.56
List Price:  CHF 126.79
You save:  CHF 36.22
NOK kr1,076.34
List Price:  NOK kr1,506.92
You save:  NOK kr430.58
DKK kr688.47
List Price:  DKK kr963.88
You save:  DKK kr275.41
NZ$166.49
List Price:  NZ$233.09
You save:  NZ$66.60
د.إ367.19
List Price:  د.إ514.08
You save:  د.إ146.89
৳11,000.23
List Price:  ৳15,400.77
You save:  ৳4,400.53
₹8,335.05
List Price:  ₹11,669.40
You save:  ₹3,334.35
RM473.25
List Price:  RM662.57
You save:  RM189.32
₦139,656.03
List Price:  ₦195,524.03
You save:  ₦55,868
₨27,872.43
List Price:  ₨39,022.53
You save:  ₨11,150.09
฿3,637.73
List Price:  ฿5,092.97
You save:  ฿1,455.24
₺3,222.80
List Price:  ₺4,512.05
You save:  ₺1,289.25
B$497.67
List Price:  B$696.75
You save:  B$199.08
R1,888.37
List Price:  R2,643.79
You save:  R755.42
Лв180.67
List Price:  Лв252.95
You save:  Лв72.27
₩134,942
List Price:  ₩188,924.20
You save:  ₩53,982.20
₪366.06
List Price:  ₪512.51
You save:  ₪146.44
₱5,625.18
List Price:  ₱7,875.48
You save:  ₱2,250.29
¥15,118.78
List Price:  ¥21,166.90
You save:  ¥6,048.12
MX$1,663.06
List Price:  MX$2,328.35
You save:  MX$665.29
QR365.44
List Price:  QR511.63
You save:  QR146.19
P1,373.02
List Price:  P1,922.28
You save:  P549.26
KSh13,148.68
List Price:  KSh18,408.68
You save:  KSh5,260
E£4,723.53
List Price:  E£6,613.14
You save:  E£1,889.60
ብር5,694.53
List Price:  ብር7,972.57
You save:  ብር2,278.04
Kz83,612.73
List Price:  Kz117,061.17
You save:  Kz33,448.44
CLP$98,024.19
List Price:  CLP$137,237.79
You save:  CLP$39,213.60
CN¥722.49
List Price:  CN¥1,011.52
You save:  CN¥289.02
RD$5,928.03
List Price:  RD$8,299.48
You save:  RD$2,371.45
DA13,469.95
List Price:  DA18,858.47
You save:  DA5,388.52
FJ$225.25
List Price:  FJ$315.36
You save:  FJ$90.11
Q781.84
List Price:  Q1,094.62
You save:  Q312.77
GY$20,987.69
List Price:  GY$29,383.60
You save:  GY$8,395.91
ISK kr13,817.61
List Price:  ISK kr19,345.21
You save:  ISK kr5,527.60
DH1,010.57
List Price:  DH1,414.84
You save:  DH404.26
L1,763.81
List Price:  L2,469.40
You save:  L705.59
ден5,677.01
List Price:  ден7,948.05
You save:  ден2,271.03
MOP$807.66
List Price:  MOP$1,130.76
You save:  MOP$323.09
N$1,895.98
List Price:  N$2,654.45
You save:  N$758.46
C$3,689.21
List Price:  C$5,165.04
You save:  C$1,475.83
रु13,357.88
List Price:  रु18,701.57
You save:  रु5,343.68
S/371.02
List Price:  S/519.45
You save:  S/148.42
K383.60
List Price:  K537.05
You save:  K153.45
SAR375.02
List Price:  SAR525.04
You save:  SAR150.02
ZK2,548.47
List Price:  ZK3,567.96
You save:  ZK1,019.49
L458.83
List Price:  L642.38
You save:  L183.55
Kč2,334.38
List Price:  Kč3,268.22
You save:  Kč933.84
Ft36,425.52
List Price:  Ft50,997.18
You save:  Ft14,571.66
SEK kr1,059.50
List Price:  SEK kr1,483.35
You save:  SEK kr423.84
ARS$85,666.43
List Price:  ARS$119,936.42
You save:  ARS$34,269.99
Bs695.09
List Price:  Bs973.16
You save:  Bs278.06
COP$389,673.99
List Price:  COP$545,559.18
You save:  COP$155,885.18
₡50,509.65
List Price:  ₡70,715.54
You save:  ₡20,205.88
L2,474.33
List Price:  L3,464.17
You save:  L989.83
₲736,638.54
List Price:  ₲1,031,323.43
You save:  ₲294,684.88
$U3,781
List Price:  $U5,293.55
You save:  $U1,512.55
zł398.17
List Price:  zł557.46
You save:  zł159.28
Already have an account? Log In

Transcript

Welcome back guys, today we are going to discuss about the arrays. It is again one more integral part of Java it's very important. And till now we have seen about primitive data types and strings, which is the reference data type. So, till now we have seen about the variables which stores one value, right. So even in string when we define a reference type, so it stores Hello. And we go to a variable type, let's say I am declaring my bike.

It stores one value, what arrays are capable of. It can store multiple values. So it's basically a bucket. it's it's a it's a container of objects. We define an array, we define the size of it, and the size is fixed after definition. After that size, we can store multiple values.

The only constraint is the type has to be same. So let's say I'm defining an array as an integer. Then all the elements has to be integer, if I'm defining an array as a string array, and all the elements has to be strings. So array is basically a container, which can hold a fixed count of values of one type. There is a length function arrays that we can use for length. So we'll discuss that when we do that.

So let me just go ahead and create a new class. Name it as an array. Demo. Yeah, that sounds good. Finish. You guys might have noticed that I haven't clicked on the checkbox to create the main method.

So let me do it this way. Let me show you one more shortcut. So when I mean, do control space, it shows me main method. I double click this, and it creates me another problem. So there's one more shortcut and let's get started with defining an edit. I'm gonna define an integer array first.

And this is just n square brackets. That's the significant part. It makes it as an array. If I don't put this, it is just an integer. So I have just created the syntax. My end every one.

This is the definition. So I've just defined the array. This is the reference, again, similar to the strings. I still haven't created the array. This is just the reference that I created. How do I create an array?

So when I say my array, equals new int, and I'll explain what I'm doing. This is the reference that I created by doing new ain't 10. New is the key words that's actually reserving me space in my computer for the elements that I'm going to save, and how much space it's gonna reserve, it's gonna reserve a good enough space that can hold 10 integer values because this is integer type, I'm giving 10 because I want to save 10 values in this array. So it's gonna reserve me some space which is good enough to sort and values of integers. Basically integer is 32 bit so it's gonna store that amount of space in my computer. So this is dedication.

But what are the values? I haven't given any values till all right, how do we give values we can access the areas by the index. index is always zero based. So if I have 10 values in my arrays, the index starts with 00 index is gonna be my first element all the items inside that array are known as elements. The ninth index is going to be my 10th element. I know it's confusing, but you'll get used to it.

So when I put value on my zeroeth index, this is my first value. That's what this my antennae one. Let's do this first, an upper 90. So this is my second index. Let's see I print out. I print out the zero index, which is my first element.

So my anti zero index do the similar thing. So index is my first element. I haven't given any value to my 01 and to my third index, which is third element, which is index 01, and two. So what happens if I print that out? Let's see that my second index with this Yeah, either way, guys, this could be confusing this index and the element things. So zero per second.

And this when I'm doing this, so you see I'm just putting second index but this is the third value, right? So let me run this code for you guys. Zero index hundred first index 92nd index zero from zero came in, I didn't give any value, right? So what Java does is it takes care of the elements that I haven't given any values if it's an integer it initializes everything with zero so that's the default value. So the default values gotten here to zero. Similar way I can just create a string array, string, my string and a one.

Okay, now I want to show you one more way to define an array. This is on the top that we discussed is one way. Here we define an array here. We have Given the values, there's one more very, very big. So this is the reference. Now if I do these curly braces and the facility my fuel costs of B and W, something as maybe already, every day Gohan also good maintenance, right?

So, this is the way I've just defined an array. That's it. I can do similar things for integer also. Similar way I can do let me show you first. See, so one, let me just copy these things, just to save some typing and save some time. Other than this, I do my string theory.

My my shame. So let me print these out. See this zero index BMW Audi Honda similar way, one line definition, I don't need to first create a reference then give the size and put the values I can just do it all in one line. If I get rid of this, I don't, I'm not gonna delete anything. So I get rid of this and do this. And then that's when we get 90 and that's it.

So, but we have to take care of this. What happens is because this thing determines the size of the array, right, so this area is of size three, if I do this hundred 90 this areas of study too. Now if I print the index two which is not here, this is zero index one index, index two is not here, it's gonna give me exception, so I have to comment it out if I run it now, so it's gonna do the same thing zero index hundred percent x 90. So this is a shorter way to create an array. Just wanted to cover this. Now, we have multiple inbuilt functions available inside arrays, like we had in string.

I would suggest two guys to go over, play around, get into the practice multiple functions very helpful in arrays. One of them is let's say I can find the length of the array into length one, which is my and array, dot length. All right, and land To my train every document I haven't given equal to sign they're on right. Now I can know that and then which one to see so airy which is true. It shows me integer length is two. That's what I define right string array length.

Three. And that's what I defined, right? So the lens function is very useful in large operations when you want to do for loop a loop around the arrays, till the end of the array, we want to find the length of the array there. That's where you're supposed for looping and all will come in different intervals. So, in this let's focus on arrays. Another function is there's a there's a complete task for Java, so we can use that class.

Let's see import. Java dot util. Stop. Yeah, we haven't discussed a board. In this tutorial. There are other tutorials where we're discussing about the packages.

There are inbuilt packages and build classes in Java that we can import and utilize those functions. So there is very useful functions available inside these classes. One of them Like, sorting function, let's say arrays, dot, I can sort an array. So let's say I'm doing this and sorting the integer array. Okay, so what's happening? Let me so the way we have defined is 190.

And let me do this for you guys. So I'm just gonna put up on over here. Something to you guys, so and i is zero, what I'm gonna do is I'm gonna loop through the indexes of the array index as always a numerical value, as you know, right? I just described. I'm going to start from the zero index, which is my first element and go till I should be less than Len. length of the array.

If you see the length of the array was two right? So I want my eye to be less than length, because my eye is only zero or one, I don't want my eye to be two at all. So that's why I'm not giving here, less than equal. So I wanted always to be less than length, if I'm starting from the zero index. And let me do i plus plus. What I'm doing here is you're going for the follow tutorials, if you don't understand this, but I'm gonna still describe it briefly.

I'm just gonna loop through all the indexes of the array. So let's say there are two elements inside the array. It's going to start from the first one, go to the second one, and come back. I can perform any action inside the loop. So let me just print out all the elements in my array and what index so I'm just printing out the values on the index and this follow I'm just going to give it because I start from the zero index goes to first and print out the values. Similar thing I'm going to do after the sod, just to show the difference to you guys.

So when I run it, it shows 190 This is the display now after that it shows 1900. This is after sorting. So it is dots or actually just sorts the ad in ascending order. Like this. There are multiple function guys, and you can look on them just google out or just do some practice it's going to be very helpful. There is one important function like system, dot carry.

Copy. So this is actually used to copy one array to another. You can practice out this one And there are multiple functions. So guys just do some practice. And it's gonna be very helpful. There are multiple functions available like strings, and it's very powerful, very handy and very useful object was our, so we're going to use arrays quite heavily.

And thanks for attending this session guys. See you in the next session, where we're going to discuss in the future tutorials. We're going to discuss economics and do while loops for loop that I just showed you here. This is just a brief thing. I'll describe it on my next tutorials. Until then, to as much as practice as you can.

All right. Thanks, guys.

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.