Question 7: "Initialization of an ArrayList in one line"

Java - Top 10 Most Viewed Questions on Stack Overflow Question 7: "Initialization of an ArrayList in one line"
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
$69.99
List Price:  $99.99
You save:  $30
€65.14
List Price:  €93.07
You save:  €27.92
£55.73
List Price:  £79.62
You save:  £23.88
CA$95.61
List Price:  CA$136.60
You save:  CA$40.98
A$106.30
List Price:  A$151.87
You save:  A$45.56
S$94.64
List Price:  S$135.20
You save:  S$40.56
HK$546.91
List Price:  HK$781.33
You save:  HK$234.42
CHF 63.50
List Price:  CHF 90.72
You save:  CHF 27.21
NOK kr764.69
List Price:  NOK kr1,092.46
You save:  NOK kr327.77
DKK kr485.92
List Price:  DKK kr694.20
You save:  DKK kr208.28
NZ$117
List Price:  NZ$167.15
You save:  NZ$50.15
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,661.98
List Price:  ৳10,946.16
You save:  ৳3,284.17
₹5,839.65
List Price:  ₹8,342.71
You save:  ₹2,503.06
RM331.75
List Price:  RM473.95
You save:  RM142.20
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,492.21
List Price:  ₨27,847.21
You save:  ₨8,355
฿2,575.56
List Price:  ฿3,679.53
You save:  ฿1,103.97
₺2,262.43
List Price:  ₺3,232.18
You save:  ₺969.75
B$357.76
List Price:  B$511.10
You save:  B$153.34
R1,296.01
List Price:  R1,851.52
You save:  R555.51
Лв127.38
List Price:  Лв181.98
You save:  Лв54.60
₩95,113.23
List Price:  ₩135,881.87
You save:  ₩40,768.63
₪260.11
List Price:  ₪371.60
You save:  ₪111.49
₱3,999.61
List Price:  ₱5,713.97
You save:  ₱1,714.36
¥10,715.43
List Price:  ¥15,308.41
You save:  ¥4,592.98
MX$1,185.45
List Price:  MX$1,693.57
You save:  MX$508.12
QR254.79
List Price:  QR364.01
You save:  QR109.21
P955.69
List Price:  P1,365.33
You save:  P409.64
KSh9,427.65
List Price:  KSh13,468.65
You save:  KSh4,041
E£3,355.67
List Price:  E£4,794.02
You save:  E£1,438.35
ብር3,989.43
List Price:  ብር5,699.43
You save:  ብር1,710
Kz58,616.62
List Price:  Kz83,741.62
You save:  Kz25,125
CLP$66,326.02
List Price:  CLP$94,755.52
You save:  CLP$28,429.50
CN¥506.51
List Price:  CN¥723.62
You save:  CN¥217.11
RD$4,049.59
List Price:  RD$5,785.38
You save:  RD$1,735.78
DA9,420.19
List Price:  DA13,457.99
You save:  DA4,037.80
FJ$157.70
List Price:  FJ$225.30
You save:  FJ$67.59
Q542.62
List Price:  Q775.21
You save:  Q232.58
GY$14,613.08
List Price:  GY$20,876.73
You save:  GY$6,263.64
ISK kr9,792.30
List Price:  ISK kr13,989.60
You save:  ISK kr4,197.30
DH706.05
List Price:  DH1,008.69
You save:  DH302.63
L1,239.86
List Price:  L1,771.31
You save:  L531.44
ден4,010.92
List Price:  ден5,730.13
You save:  ден1,719.21
MOP$562.15
List Price:  MOP$803.11
You save:  MOP$240.95
N$1,302.54
List Price:  N$1,860.85
You save:  N$558.31
C$2,571.43
List Price:  C$3,673.63
You save:  C$1,102.20
रु9,317.58
List Price:  रु13,311.40
You save:  रु3,993.82
S/262.81
List Price:  S/375.46
You save:  S/112.65
K268.53
List Price:  K383.63
You save:  K115.10
SAR262.51
List Price:  SAR375.03
You save:  SAR112.52
ZK1,879.71
List Price:  ZK2,685.42
You save:  ZK805.70
L324.19
List Price:  L463.14
You save:  L138.95
Kč1,629.65
List Price:  Kč2,328.17
You save:  Kč698.52
Ft25,373.17
List Price:  Ft36,248.95
You save:  Ft10,875.77
SEK kr758.75
List Price:  SEK kr1,083.98
You save:  SEK kr325.22
ARS$61,468.94
List Price:  ARS$87,816.53
You save:  ARS$26,347.59
Bs482.36
List Price:  Bs689.12
You save:  Bs206.75
COP$272,946.91
List Price:  COP$389,940.87
You save:  COP$116,993.96
₡35,623.88
List Price:  ₡50,893.45
You save:  ₡15,269.56
L1,732.95
List Price:  L2,475.75
You save:  L742.80
₲523,151.84
List Price:  ₲747,391.81
You save:  ₲224,239.96
$U2,683.09
List Price:  $U3,833.15
You save:  $U1,150.06
zł281.85
List Price:  zł402.67
You save:  zł120.81
Already have an account? Log In

Transcript

All right to Stephens question with over 2.8 million views is industrialization an ArrayList in one line? What does this question mean? is how can I create an ArrayList? And after creation, how can I fill this ArrayList with data in only one line of code. And let me tell you that there isn't a short way to do this with the, with the ArrayList class itself. So we need a little hack, and the HEC would be to use a constructor of the ArrayList class that gets collection.

So we have to first we have to create a collection in one line and pass this collection and to the constructor of the ArrayList. And the result is an error list filled with the data out of this collection. So the question, the first question would be, how can I create an error collection in one line and fill it with data? Also, with only one single call and one line? So, the API knows various ways to create collections in one line. But yeah, I show you the most common ones.

The most common one maybe is d second approach I showed you here. So it is to use the arrays class. And the method s list you see there the method what does this method or a call to this message do? You see the parameter is a Barack Perry jmeter So, you can pass as many of elements you like in there. So, you can, you can pass only one or two or three parameter how many you want and the result of the call of the method is list with this parameters inside. So, with this method, you are able to call or to create a list with only one coil in one line and all elements you want and this list you can pass simply to this method.

And as you all know, every list is a collection. So, you can also say that a call of this method, get or create some collection in only one line. The second approach would be To call the the list interface itself, and the methods of this list interface, there are methods off it creates an empty list off, he creates a list with only one parameter or with one element sorry, this method and so on. There are a few methods in the list interface to create a list. But as you can see, you only get a list as resides and list as we all know as an interface. And so, you get and special implementation of the list it isn't normal usual error list or something like that.

But yeah, there there are specialists if you want to say this, so I write it to these methods. Return arrow lists, but especially implementations of the list interface. So let's, let's take a look what they do, especially. So if you are using the list of method to create lists, you get specialists in in form of unmodifiable lists. So the recite would be an implementation of the list interface, but the list is unmodifiable. So you can't, you can't or you aren't allowed to remove elements.

You aren't allowed to add elements or you are and you aren't allowed to edit the elements in this list. There might be use cases in which you have to be sure that your list once you create it, it can't be modified at all at the end. So, yeah, if you if you need section lists and this method or this methods of the list interface or your friends the second one arrays.as list method also creates some special the list and let us take a look at the at the API for this. So, we go to navigate open type and we call the arrays class. And in this we are looking for the methods outline as list and what it creates is an ArrayList but it's not an usual error list. It is a this special kind of ArrayList So, if you mouse over it, you can see it two Java util Arrays dot ArrayList dot ArrayList t. So, yeah, you don't get an usual ArrayList as we all know this ArrayList Java util ArrayList but you get some special implementation and the special implementation is in the class array list in the class array list itself contained.

So yeah, the class arrays lock class provides its own error list implementation, if you like to do it to say it this way. And what is special, it is the returned list from the s list method is a Fixed Size list. So, you aren't allowed to remove elements and you aren't allowed to add elements to the returned list. We take a look at an example later on. So, we we could see how it works and special and why solicit fixed size. As you can imagine the the method to to build the bridge between this box every every bar x paramita is in the end is an array.

So you can pass there an array and you get a list a list out of this array and this list and this array is connected with each other. So if you edit the list, you are allowed to edit the list, but not edit with with you you don't have you aren't allowed to add or to remove elements, but you can change you can edit the containing elements and if you change anything any element of the list the array is edited as well. And if you edit element of the array, so the list element is also edited. Yes. So it is you can imagine this method like a bridge between the this array and this list so, they are connected with each other. And if you if you change one element on the one on the one side, the other element on the other side is also changed.

So, when do you need such a method Sometimes you get from from the one API from the one side, you get an array. And on the other side, you have a list or you need a list. And if you want to connect both sides with each other, you could use this. Because if you would copy the array on the one side and copy into a list, and you pass the list to the other side, they aren't connected anymore. So if the array is changing, the list didn't recognize this and vice versa. So if you want to connect them and and life entities stay in touch with each other, you can use this method.

So it's very tricky and very special, this implementation, but you can use it as an asset, simply for create a collection in one line. So you don't need to To use the special connection between the array and the list, you can use it only for for easy purpose to create some collection in one line. And if you did it this way, this way, or this way, you get as an recite a list and a collection collection. And then you can use this per disk constructor. So, let us take a look in this class. Whereas to construct this one, and that's the only constructor you can see with Yeah, where you can pass multiple elements in it.

This not you can only with with an initial capacity on an empty array list and this is the only way to To create an ArrayList by using the constructor and to fill the same error list in one line by passing the collection in the constructor itself. Yeah. And then the ArrayList is the collection is you can see is copied or is filled in to create an ArrayList itself. So this is the way how you can initialize and fill an array with data in only one line. So, let's take a look at an example for this. And in the first line, I create a list with or by by using the off method of the list interface and I am passing two strings to this method and result is I get an unmodifiable List of type string.

And yeah, as I said an unmodifiable list you aren't allowed to add or to remove something, and you aren't allowed to change the elements. So it isn't also it isn't allowed to use the set method, you can at every list, there is the set method here. So you say at position index, you said please the new element, so you aren't allowed to use the set method, the Add or the Remove method. And if you try to do this, you get an Java Lang unsupported operation exception. But you can use it to create a simply a list or collection and you can pass Then the collection and to the ArrayList have a constructor and the resize is a normal ArrayList. So, let let's take a look at this.

So, we first create an unmodifiable list by calling the off method from the list interface posit to the ArrayList constructor and to resize the normal ArrayList we are allowed to add something to remove something and so on. So, yeah, you get a usual error list as recycled. The second approach would be to use the array arrays.as list method. So we are passing also two elements to it. Hello and world and as you just see saw the array as list element parameter is a VAR arc. So it is the converted to an array string array in this case of size two.

But yeah, it isn't doesn't matter how it works, but this would be an array and the array is connected with the recited list. So it is fixed size as I just explained. And fixed size does only mean that you aren't allowed to remove or to add something, but you are allowed to, to to edit the elements or to change the elements. So, I am allowed to set a new element on the on the zero place. So in this case, new Hello, a string it is okay. And if I try to remove an element, I get also the unsupported operation exception as here in the example before so it's Also a special kind of list but I can use it as, as creator of a collection and collection I can pass to the error list as well.

And after it, I am allowed to add and to remove the elements as I usually want to do if I'm using an ArrayList. Yeah, so this would be the way to create and to fill an ArrayList and only one line.

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.