Question 2: "How do I declare and initialize an array in Java?"

Java - Top 10 Most Viewed Questions on Stack Overflow Question 2: "How do I declare and initialize an array in Java?"
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.39
List Price:  €93.41
You save:  €28.02
£55.92
List Price:  £79.90
You save:  £23.97
CA$96.01
List Price:  CA$137.16
You save:  CA$41.15
A$107.15
List Price:  A$153.08
You save:  A$45.93
S$95.13
List Price:  S$135.90
You save:  S$40.77
HK$547.14
List Price:  HK$781.66
You save:  HK$234.52
CHF 63.86
List Price:  CHF 91.23
You save:  CHF 27.37
NOK kr775.40
List Price:  NOK kr1,107.76
You save:  NOK kr332.36
DKK kr487.78
List Price:  DKK kr696.86
You save:  DKK kr209.07
NZ$118.01
List Price:  NZ$168.60
You save:  NZ$50.58
د.إ257.06
List Price:  د.إ367.24
You save:  د.إ110.18
৳7,680.49
List Price:  ৳10,972.60
You save:  ৳3,292.11
₹5,842.03
List Price:  ₹8,346.11
You save:  ₹2,504.08
RM332.86
List Price:  RM475.54
You save:  RM142.67
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,491.96
List Price:  ₨27,846.85
You save:  ₨8,354.89
฿2,586.09
List Price:  ฿3,694.58
You save:  ฿1,108.48
₺2,265.39
List Price:  ₺3,236.41
You save:  ₺971.02
B$363.53
List Price:  B$519.35
You save:  B$155.82
R1,302.64
List Price:  R1,861
You save:  R558.35
Лв127.90
List Price:  Лв182.73
You save:  Лв54.82
₩96,270.48
List Price:  ₩137,535.16
You save:  ₩41,264.67
₪262.29
List Price:  ₪374.71
You save:  ₪112.42
₱4,033.94
List Price:  ₱5,763.02
You save:  ₱1,729.07
¥10,867.12
List Price:  ¥15,525.12
You save:  ¥4,658
MX$1,187.12
List Price:  MX$1,695.96
You save:  MX$508.84
QR254.93
List Price:  QR364.20
You save:  QR109.27
P994.08
List Price:  P1,420.18
You save:  P426.09
KSh9,360.69
List Price:  KSh13,372.99
You save:  KSh4,012.30
E£3,358.63
List Price:  E£4,798.26
You save:  E£1,439.62
ብር4,003.77
List Price:  ብር5,719.92
You save:  ብር1,716.15
Kz58,546.63
List Price:  Kz83,641.63
You save:  Kz25,095
CLP$67,216.99
List Price:  CLP$96,028.39
You save:  CLP$28,811.40
CN¥506.70
List Price:  CN¥723.89
You save:  CN¥217.19
RD$4,073.53
List Price:  RD$5,819.58
You save:  RD$1,746.04
DA9,418.34
List Price:  DA13,455.35
You save:  DA4,037.01
FJ$158.31
List Price:  FJ$226.17
You save:  FJ$67.86
Q543.96
List Price:  Q777.12
You save:  Q233.16
GY$14,650.29
List Price:  GY$20,929.88
You save:  GY$6,279.59
ISK kr9,815.39
List Price:  ISK kr14,022.59
You save:  ISK kr4,207.20
DH707.71
List Price:  DH1,011.06
You save:  DH303.35
L1,237.78
List Price:  L1,768.33
You save:  L530.55
ден4,025.24
List Price:  ден5,750.59
You save:  ден1,725.35
MOP$563.96
List Price:  MOP$805.69
You save:  MOP$241.73
N$1,304.33
List Price:  N$1,863.42
You save:  N$559.08
C$2,570.38
List Price:  C$3,672.13
You save:  C$1,101.75
रु9,397.27
List Price:  रु13,425.24
You save:  रु4,027.97
S/263.43
List Price:  S/376.35
You save:  S/112.91
K270.11
List Price:  K385.89
You save:  K115.77
SAR262.49
List Price:  SAR375.01
You save:  SAR112.51
ZK1,873.89
List Price:  ZK2,677.10
You save:  ZK803.21
L325.37
List Price:  L464.84
You save:  L139.46
Kč1,643.47
List Price:  Kč2,347.91
You save:  Kč704.44
Ft25,458.03
List Price:  Ft36,370.18
You save:  Ft10,912.14
SEK kr764.90
List Price:  SEK kr1,092.76
You save:  SEK kr327.86
ARS$61,327.27
List Price:  ARS$87,614.14
You save:  ARS$26,286.87
Bs483.57
List Price:  Bs690.85
You save:  Bs207.27
COP$273,218.78
List Price:  COP$390,329.27
You save:  COP$117,110.49
₡35,710.66
List Price:  ₡51,017.42
You save:  ₡15,306.75
L1,733.65
List Price:  L2,476.75
You save:  L743.09
₲524,442.73
List Price:  ₲749,236.02
You save:  ₲224,793.28
$U2,683.09
List Price:  $U3,833.15
You save:  $U1,150.06
zł283.24
List Price:  zł404.64
You save:  zł121.40
Already have an account? Log In

Transcript

The second most viewed question on Stack Overflow with over 4.2 million use is how do I declare and initialize an array in Java, you might know what an array is an array, it's a special data structure to store different objects or different primitives in it. And each array has a fixed size. And in each array, you can address each element directly. So for example, if you have an array with the size five, you can say, Please give me the second element, the third or the fifth. And you can get it or you can edit them as you want and you can address each element of it. That is how the data structure array looks like.

So if you want to create array, you have three different approaches. The first one is that you create an empty array with a defined size. So if you know, if you don't know the content yet of the array, but you know, you want an array of size five, you can use the first approach. Sometimes you have content, and you want to create an array out of this content. So you can use the second one. So it's the creation of an array by defining its content.

And the third approach is to create an array out of a stream from the stream API from Java eight, and each stream has a method to array and in this case, you would convert the stream into an array. In this case, each stream is only one dimensional. So the resizing arrays also one dimensional But the two different approaches can also create a multi dimensional arrays. So, you might know that arrays can have 234 dimensions. So you are free by creating them with two approaches. And yeah, we will see in next an example, that the size if you want if you are using the first approach to size is defined and fixed in each dimension.

But if you are using the creation over its content, the size or the elements, the size of the elements in each dimension may differ. But don't worry, we take a look at an example. So in this case, and line two and line three, we are using the first of pro approach so we don't have any content yet. We are Only know the size of the array. In this case, we want to create an array with size five. So we can write the data type and two brackets, variable name equals sign new data type brackets and then the size of the array.

And what we get here isn't that empty at all I have to be honest, because as you might know, only the primitive types aren't or couldn't, can't be empty at all. So if you have an int variable it It always has a default value. default value of zero. So what we get by writing the second line is an array of size Five, and every element. So the five elements within this array are defined with the default value zero from the end. But if you are using objects like this integer, and the array is empty at all.

So what you have here as we get here is an array with size five and five times nine, as content. So, yeah, in this case, you might say that to get an empty array, but the creation is the same, you know the size and you don't know yet the content and you create an array out of a special size. If you have the photo second approach, if you know the content, and do you want to create an array, I'd off the content. So you can use The creation type, and I'm for the content, in this case, three numbers 123. And you can write it like this, and the resize as an interplay of size three. So yeah, you, you don't have to write the size of it, you only have the content.

And Java computes the size and so on and creates an array and a pre initialized, so wait, so arrays, so you have 4123 as content of the ray. In line five is the same approach used. So the second one, but it's a bit shorter with the notation but it's the same as in line four. So line four and five is the same one, and five is a bit shorter. All right. So let's Take a look at multi dimensional arrays.

So, if you have in our case in our example, we have two dimensions. And if you want to use the first approach with a defined size, you can write it like this and with object like integers like this, and if you have a content and you want to to create the content an array out of the content, you can also do it with multi dimensional arrays, like you see in line nine. But I set the size of elements in a dimension may differ if you use the second approach. In the first approach, the size of each dimension is always the same. So as you can see, the first approach the second dimension the size of each element is to and the size of the frame. The dimension of each element is also two.

And if you use the second approach, the size may differ. Let's take a look at the example in line 10. There we see this, we have in first dimension, we have three elements, one, two, and three. And, but the size of the three elements differ. So in the first in the first case, we have to size one, in the second case two, and this one is size three. So as you can see, it's more flexible to to write it this way, and then to use the first approach, because the first approach, if you would write, yeah, you have you have to write three three.

So you have three elements in the first dimension and the element has a size three In the second dimension, but yeah, you would have more elements than you want. And then you need because you want here, only one side was size one here, size two, and so on. So yeah, they the two approaches differ. And that's the difference between. So the second approach is more flexible than the first one. Also take notice of the different connotations of arrays.

So you might, either the usual way to define races that you write data type and after the data type two brackets, and then the variable name and so on. But you can also only write the data type then the variable name and then the brackets. Yeah, so it's also it's both fine for your Java is both okay, but I would recommend you notation in line 15. And it's the usual notation. Because if you take a quick look on the code in line 15, you'll see okay, it's an int array in line 16. You might think if you take only a quick look at the expression, you might think it's only an int, because you don't recognize brackets too fast.

So, and then you think, Oh, it's an int. And you are wondering why you have an array. So yeah, it's a line 15 it's usually one and I would recommend you to use 15 as well for you. If you want to edit the arrays, as I said, in the beginning, you can pick each element in the array and edit it or you can change it and so on. And as an example, How to protests how to iterate over an array you can use for each while for loop in this case or for each for example, but as you can see, I am using for loop with variable E for the first dimension and the second loop embedded loop with the variable name j for the second, second dimension. So, I pick each each element of the first dimension into array, I picked the I zero at the beginning so it picks the first element out of the array and from this the length, so, and then after it I can set a new a new value x for example for it.

So, in this case we start with number zero And we set the first run would be an array for 00 is the value zero. After that we increment variable number by 10. And after it would be 01 10, and so on. So you can iterate over the whole enter through the whole array. And you can edit it, for example, or you can change the values. What do you want to do with it, and as you can see, you can, you can write brackets at the February of this in the brackets.

If you want to read an array, it's the same you overwrite you you iterate through it. And then you can use a system out print line. For example. In this case, I use string format and If you write this percentage D and D is it's a number, an integer number. So 1233 parameters, and this is the first one, the second is the third one. And so and you can then reading the whole array, if you do so, you get this as result.

Yes, the third approach was to create an array out often string stream, sorry. And for this proach this example is here. So what do we do here, we create an in stream from one to five, five minutes extra surf. And I call the to array method or on the stream, and I get an int array out of it. And I can iterate over it as I did before and get as resized 1234 And to, to demonstrate that the size of elements may differ in one dimension in the same dimension. If you are using the second approach I have made this example it's interplay five underscore two.

And this one was this. So we have, as I said, in the first dimension three elements, and the second dimension has different size. The first one first does the second element has the size to the third element has a size three. And if we iterate through this away, array, we can see the size of each element and the second dimension with it. And we get as resize. One, two and three.

So yeah, as I said, and then you can see that the The length or the size of the dimension, and the elements differ. So yeah, it's more flexible than the other one. Yeah, this is the third. This is how you create and initialize an array. And I would say let's take a look at the third question.

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.