Array List

Complete Step By Step Java for Testers Java Advanced - Collections Framework
12 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
€93.84
List Price:  €131.38
You save:  €37.54
£80.95
List Price:  £113.34
You save:  £32.38
CA$137.04
List Price:  CA$191.86
You save:  CA$54.82
A$154.99
List Price:  A$216.99
You save:  A$62
S$136.27
List Price:  S$190.79
You save:  S$54.51
HK$783.50
List Price:  HK$1,096.94
You save:  HK$313.43
CHF 91.19
List Price:  CHF 127.67
You save:  CHF 36.48
NOK kr1,101.37
List Price:  NOK kr1,541.96
You save:  NOK kr440.59
DKK kr700.13
List Price:  DKK kr980.21
You save:  DKK kr280.08
NZ$169.21
List Price:  NZ$236.90
You save:  NZ$67.69
د.إ367.23
List Price:  د.إ514.14
You save:  د.إ146.90
৳10,965.37
List Price:  ৳15,351.96
You save:  ৳4,386.58
₹8,334.23
List Price:  ₹11,668.26
You save:  ₹3,334.02
RM477.95
List Price:  RM669.15
You save:  RM191.20
₦125,683.43
List Price:  ₦175,961.83
You save:  ₦50,278.40
₨27,806
List Price:  ₨38,929.52
You save:  ₨11,123.51
฿3,703.32
List Price:  ฿5,184.80
You save:  ฿1,481.48
₺3,259.92
List Price:  ₺4,564.01
You save:  ₺1,304.09
B$516.59
List Price:  B$723.25
You save:  B$206.66
R1,923.67
List Price:  R2,693.22
You save:  R769.54
Лв183.31
List Price:  Лв256.64
You save:  Лв73.33
₩137,775.97
List Price:  ₩192,891.88
You save:  ₩55,115.90
₪378.18
List Price:  ₪529.46
You save:  ₪151.28
₱5,754.62
List Price:  ₱8,056.70
You save:  ₱2,302.07
¥15,478.45
List Price:  ¥21,670.45
You save:  ¥6,192
MX$1,711.89
List Price:  MX$2,396.72
You save:  MX$684.82
QR364.36
List Price:  QR510.13
You save:  QR145.76
P1,383.88
List Price:  P1,937.49
You save:  P553.61
KSh13,448.65
List Price:  KSh18,828.65
You save:  KSh5,380
E£4,807.48
List Price:  E£6,730.67
You save:  E£1,923.18
ብር5,687.89
List Price:  ብር7,963.28
You save:  ብር2,275.38
Kz83,485.81
List Price:  Kz116,883.48
You save:  Kz33,397.66
CLP$95,253.47
List Price:  CLP$133,358.67
You save:  CLP$38,105.20
CN¥724.60
List Price:  CN¥1,014.47
You save:  CN¥289.87
RD$5,894.01
List Price:  RD$8,251.85
You save:  RD$2,357.84
DA13,445.45
List Price:  DA18,824.17
You save:  DA5,378.72
FJ$227.71
List Price:  FJ$318.80
You save:  FJ$91.09
Q777.29
List Price:  Q1,088.24
You save:  Q310.94
GY$20,903.54
List Price:  GY$29,265.80
You save:  GY$8,362.25
ISK kr14,104.58
List Price:  ISK kr19,746.98
You save:  ISK kr5,642.40
DH1,013.18
List Price:  DH1,418.50
You save:  DH405.31
L1,782.83
List Price:  L2,496.04
You save:  L713.20
ден5,776.11
List Price:  ден8,086.79
You save:  ден2,310.67
MOP$806.39
List Price:  MOP$1,128.98
You save:  MOP$322.58
N$1,907.39
List Price:  N$2,670.43
You save:  N$763.03
C$3,677.32
List Price:  C$5,148.40
You save:  C$1,471.07
रु13,326.11
List Price:  रु18,657.10
You save:  रु5,330.98
S/368.16
List Price:  S/515.44
You save:  S/147.28
K379.71
List Price:  K531.61
You save:  K151.90
SAR375.04
List Price:  SAR525.07
You save:  SAR150.03
ZK2,580.25
List Price:  ZK3,612.45
You save:  ZK1,032.20
L466.96
List Price:  L653.76
You save:  L186.80
Kč2,370.96
List Price:  Kč3,319.44
You save:  Kč948.48
Ft36,978.60
List Price:  Ft51,771.52
You save:  Ft14,792.91
SEK kr1,088.71
List Price:  SEK kr1,524.24
You save:  SEK kr435.53
ARS$87,217.91
List Price:  ARS$122,108.57
You save:  ARS$34,890.65
Bs692.87
List Price:  Bs970.05
You save:  Bs277.17
COP$390,680.46
List Price:  COP$546,968.27
You save:  COP$156,287.81
₡50,009.58
List Price:  ₡70,015.41
You save:  ₡20,005.83
L2,466.51
List Price:  L3,453.22
You save:  L986.70
₲740,348.57
List Price:  ₲1,036,517.61
You save:  ₲296,169.04
$U3,852.35
List Price:  $U5,393.45
You save:  $U1,541.09
zł405.43
List Price:  zł567.62
You save:  zł162.18
Already have an account? Log In

Transcript

Hello, guys, welcome back to the next tutorial. In this one, I'm going to start with the collections framework. And let's start with the simplest and the most use structure data structure of the framework. So we're gonna start with ArrayList. And this is something kind of very widely used when we talk about collections framework. It's kind of based on top up areas.

I'm going to explain you while I'm doing stuff. So let's quickly open the eclipse. And so I have a class is a basic stuff ArrayList demo. That's what I named it. And there's the main method. So So the thing is, it's the basic data structure for the collections framework, and it's built on top of arrays.

So let's talk about it while doing the way we define it as array list. And we need to define the type of elements that we need to put in the ArrayList. So as you know, Java is Java language is sort of strongly typed to the data types. Right, so we need to provide what type of data we need to pour in. So let's say I'm gonna put in string type. So it is and in the angle brackets, we need to put the type.

And then let's name it something. So let's name it cars maybe. And then new new is the keyword to create a new instance. Right, and then relist. And then again, we need to put the type, so string, and then parenthesis, and then semicolon. So it asks us to import few things.

So import a list from Java dot util. So just click this and areas are gone. So now we have the import as well. So this is the simple way that we create an ArrayList. So what we can do is by default, when we create an ArrayList, like this, and we don't specify anything, I'll tell you what I'm talking about. The ArrayList is created for the size 10.

So right now we can store 10 elements in this playlist. So where does this 10 come from? That's the default set by the Java class. And if we know let's say, I know that I'm going to have some 50 elements or hundred elements, then what I can do is I can specify the size here, I can just specify here. So now it's going to create a default ArrayList of the size hundred. So this is always good in case you know, how beggars you're really Scobie.

And if you're not sure, then just leave it empty. So this, this is the thing about ArrayList. And you know what, what happens is, let's say if, if the ArrayList you created by default is created by 10, right? And you end up adding the 11th element. So what happens is ArrayList creates one more structure and then copies all its elements basically relocates everything to match the size of 11 and then create the next one, right? So this is a complete process that's taken care by Java.

And it takes a lot of memory consumption and the process right, so that thing makes it slow. Sometimes if you don't No. And that can just eat up your some memory space. Right? Not not by a whole lot difference, but some. So it's good if you know the size.

But if not, then it's definitely going to increase on its own. It's relocate, and it's gonna happen on its own. And Java takes care of it. So you don't need to worry about it. So let's get rid of the size right now. We don't want to resize by now.

And so let's let's get started. Let's get let's take a look at few things. Maybe few methods that we commonly use an ArrayList. So how do we add items to this list? So I have a list called cars, right? So cars dot this add method, add add.

And I can provide anything any string in it because the type I defined is string type. And then let's say I can add BMW. So this is my first car that I added. And let's say add cars, add more, and maybe more Not at all. So Add more maybe call, Hannah, I don't know, just adding something. So cars add and then maybe Adi.

So we have three cars by now right added. So this add method is just simply to add elements to the car. So how I can, I can show you these things. So I'll just kind of print all these things and I'll show you what different methods are the commonly used methods are in the ArrayList. So we're not going to cover each and every method in a list because that's what we want to restrict this class to. We want to highlight the most commonly used once and this is the kind of intent of this course that we're going to go through the Java concepts and cover them so we can work on Selenium.

It's not we don't want to cover every detail of Java right. So let's let's type few things. So when we have the ArrayList we have a size right because we added three different things right, so we have a size of the arrays. So we can actually find the size of the array. So I'm going to declare a variable called n size. And then cars is my ArrayList.

That size. So this is a method, which gives the size of the ArrayList. So I'm just gonna show it to you guys and print this thing. So you guys know the size of the list is and then plus size. So all said and done, let's run it. And the size of the list is three.

And we know that we added three elements to the site to the URLs, right. So now let's say how so we already took a look at adding elements, right? Sorry, adding, right? This is size. And then let's take a look at retrieving. So basically get stuff.

So how do we get stuff in assembly, good stuff, by the index. So ArrayList is we have the index, right? So let's say the item on index. One is let's, let's figure it out, right? So what I'm going to do is cars dot get. And if you guys see it shows you get index.

So it asked for the index. So I'm going to provide the index, let's say index one that I'm trying to find. And hit save, and then run the program. So this is the item on index one is Honda, which is this right? So as like areas because it's built on top of arrays, so it's zero index based. So index zero is BMW one is Honda two is Adi.

So the sizes three, but it starts from 01 and two, right? So that's how we took a look at getting this stuff. And let's take a look at how we iterate over the ArrayList. Right So, Teresa so what we can do is we can just type of for loop for int i equals to zero and we can go till less than size. So basically sizes three and we want to go till two, so we can go to less than size i plus plus. And then we can just simply print stuff right now.

So we can say I come on index, and we can say I, and then again we can say this is and then do cars Todd get, right. So, this is gonna print all the elements, right? So let me first trend something so we know that we are trading. So iteration example right? So that's good enough. So now let's run the program.

So it shows me have plus the size the item on the next one is Honda, then efficient example starts and then I don't want index zero BMW, Honda and Audi. So we just successfully traded through the areas that's transport, right. So we have covered adding getting the size, getting one element iterating over all the elements. And, you know, all we can do is we can actually create using one more way. So we can just iterate, like using the for each loop. So we can say for, and we know that type of elements type of items into their list of string.

So we can say string, and one car, and then C inside cars. So this is the element which is going to be representing one item in the ArrayList. And this is our ArrayList. So for string, the type of car in cart, I'm going to create a loop and it's going to say let's print something so item is car, right? So that's gonna do its own stuff, right? So we get the error, let's say, unknown, so we need to add a plus sign.

Yeah. So this is all said and done. So let's, let's type some more print statements to differentiated. Next, for loop example, right, so that should be good enough. And maybe let's put a slash and so I printed a new line. So let's save it and run the program.

So we have the next follow example. And here's the space that I printed. So I printed the space with slash and so we have this space extra here and the item is BMW, Audi. So this is the second way to iterate over the empty list. So now let's take a look at the last one. So we're gonna do is we want to remove the items.

So let's take a look at our remove the items. So we have a cars list. And then there is a method called remote, so we can use that dot remove. And if you guys see it shows you remove I empty index, so we can remove by index. So let's remove the item from the last one. So what I can do is I can just get the size sizes three, and I want to remove the last index, which is two.

So I'm going to do size minus one, so it's gonna remove the item from index two and index two is the last one guys, right because start from 01, and two, so that's going to remove the last item. And if I print again, print the list. So this is gonna do that. So let's do this. I'm gonna again print it. So next, so after removing, we can say that after removing, so if I run it again, so it says this is the one that just ran after removing.

Now it has only two items BMW and Honda so it removed the car item already. So why I removed the third item It is such a small list. It's just three elements right now. Right? What I want to focus is, when we remove from ArrayList. Again, it's, as I mentioned, it's based on arrays.

So it's index based, whenever we remove the last item, it's fine. It's quick and fast. If we remove second, last or last, it's still okay, right? Let's say you have a list of thousand elements, or maybe 2000 10,000. And if you remove the item from the first place, or maybe somewhere in the middle, let's say you have a list of 10,000 items, and you remove the 20th item. So what what's gonna happen is the 20th index is empty.

So a list is gonna relocate everything, it's gonna put 21st element to 20, and then 22nd to 20%. And it's gonna just relocate everything, rearrange everything, and then re that rearranging takes some memory some time. So some processes and that kind of eats up something, some process some memories from an hour. So it's taken care on its own. We don't have to do it. We just need to call that removed.

Sighs Removing the index. But Java takes some time, some memory some processes to do that. So when we want to remove a lot of items from the list, it's not recommended to use an ArrayList. So for that purpose, it's linkless and bigger. It's covered in a different lecture. And that's the difference.

So that's a popular interview question as well. Like what's the difference between a list and a linked list and we're going to cover that. So just remember guys, if you want to remove a lot of things, just prefer not to use any list. So that's pretty much for this tutorial guys. I will again say practice a lot as much as you can and come back up with the questions. Thanks for attending this one guys.

Stay tuned for the next ones. Thanks.

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.