Arrays

7 minutes
Share the link to this page
Copied
  Completed
An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. You have seen an example of arrays already, in the main method of the "Hello World."

Transcript

Hello, in this Java video we are going to cover arrays. So array is for like a data type is more of a data structure that allows you to store data information. And it's off a particular data type that we've already covered, or even a custom data type that you may create later on. So an array allows you to store multiple values, and easily allows you to group them together. So if for example, maybe you have a list of usernames, and instead of having a separate variable for all of them want very hard to especially when you have, you don't know how you're going to have when you're changing stuff. And two, it could just be compound manageable.

Using an array allows you to store common data, you can store whatever you want, really, it's just it's best to store data that is, you know, has a common functionality. So first of all, to create it, you specify the date, tap on the quick There's two ways of creating an array are on the correlate or, and you can even put a square bracket here, like so. Or what you can do is put the square brackets here after the data type. Some people prefer this method ad if you actually look at a lot of Java code online on websites on StackOverflow and programming blogs, you will come across it like this. So you see like this is the same here. I saw a prefer here simply because I come back, I come from more of a C c++ background, and that's where you would put it so I'm going to put it there, but you can put it in the other location.

What I like to put a comment in so Just so you, okay, so this just says we've got an array, so let's actually use it in some meaningful way. So we can assign some values to it. So you press equals, you put curly braces here, now, and in here, you put in all of the values that you want. So 123456, like so we've created an array of size. Six, the size is determined by how many values we pass in. And we can access the value by doing system.

Dot out dot print, ln, like so. And we could do a r, so maybe the rate per square bracket, now you specify, so he's saying read because we can't print it out yet. We need to specify which index we want to print out or which value at which index. So if I were to put index zero So this is the first index to remember computer starts at zero. So if we run this, it prints out one, we do index one with two, you know the second value in the array, it prints out two. And if we were to put five, it'll be the final value because it's 01234 and five.

So if we run it, there, we get the value of six. You can also create it like this, too. And once that second Ah, so to him. I'm sorry, does know from c++ background, okay. So if we have a lot of values, then it can be sort of, I would say cumbersome To be able to, you know, print all of the math, so what we can do is use a for loop. But before we do that, you can actually get the length of the array.

So System dot out dot print, ln. And if we do i o dot length, they don't in square brackets here, cuz we'll gain property of the array, we run it, it prints out six, because the length is six, because there's one value to value to my indexes, but the number of values in the array, and they all have to be the same data type. So you can't have an array data from integers and doubles and strings, or some kind of some Boolean. You just can't do that. But there are some languages that are a bit more loose with it, but most frankly, aren't. Okay, so the for loop I'm going to show you so Before and because they also, if you're unsure about loops and what we're doing here, feel free to go back and check out the loop videos, and more specifically the for loop and you'll be all queued up for Intel equal zero or less than to make it dynamic array dot num.

So it's a less than the length. The reason is not less than or equal to remember, computers start at zero until the array, so we want it to be from zero to, in this case zero to five. If the length is six feet stopped one before, you'll be zero. So that's all of the elements for some colon, do i plus plus or just a simple increment. And now if we were to system dot out, again, you can use these values any way you want, you can assign them to a variable them into a method which we'll cover later on in this series, you could still in the database, it does not matter if you do AR pi. So because this is constantly changing every time the loop runs, or every pass in the loop will print out every value.

So if I were to comment this out, I think they printed out all the values and beliefs dynamically for seven, I think nine two, doesn't have to be, you know, combine for before 343, print less less, I think See, still print out all the value. True, we're doing it relative to the length and not it's not a specific length. And on top of that, we're printing it using the for loop as well. Obviously, you could do more of it. I think you could maybe add all of the values of a record. And during that next test, try adding up all the values in the array and printing that I have another extra tough, try using a loop to be able to go through the array and figure out which is the largest value.

And there is one last thing I want to cover, which is the for each loop, which I'm not actually going to cover in this video on how to do that in the next part in this series, which is just going to be really useful to be able to simplify this center. So thanks for watching. I look forward to seeing you next video. If you have any questions, feel free to reach out and I'll see you 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.