Java Lesson 11 | Array Basics
Hey, this is Jake with Mac Heads 101. This is going to be the 11th Java tutorial, and today I'm going to be talking about arrays.
So, what an array is, is it's something that can hold a lot of data of related types, like the same data type. Like, it can hold ints, like a lot of ints, a lot of strings, but you can't be holding like 3 ints and a string and a char with it. It doesn't work like that.
Um, I'm not going to be building a program in this one; I'm just going to be demonstrating what an array is. So to make an array, first you do the data type of the array. So I'm going to make one of ints. So, “int”, and then you name it. But next to it, you put these brackets, and that tells Java that you're going to be working with an array.
And then in here, okay? So this is the type “int”, which is just how you declare something. But these brackets mean that you're going to be working with an array. And in here, you add all the stuff. So, like it's int: 1, 3, 6, 19, 5. Okay? And then that, then your semicolon at the end, and it goes inside those curly braces. So you made an array containing 1, 3, 6, 19, and 5.
So, if I wanted to print out one of these, um, what I would do is I, and then in the brackets, I type which number in order. Now the numbering starts at zero, so 1 is 0, 3 is 1. Um, if I did like 2, for example, that would be 6: 0, 1, 2. So the number in there is the number in the order that um, in the order that you put them in.
So that prints out six because that's 2, and zero starts at zero; that's going to be one. The first element in the array is zero, and the second is one.
So, um, now I'm just going to be doing something where I sum up all the elements in the array. So I'm going to make an int “sum” that's going to be, um, the sum of all of them. And we're going to make a for Loop. So for int x = 0, x is less than “i.length”, which is the length of the array. So, “.length” means the length of the array.
So this is 1, 2, 3, 4, 5. So that's what it's going to mean, so it's going to be, you know, have elements in the array. Um, and then of course, x++. Now, sum equals sum plus i, and in here, we're going to put x. So this is how we um, loop through the elements in the array.
And I didn't yet initialize sum, which is the problem. Sum equals zero, so it's going to be zero. It's going to start at zero plus um, the first one. So it's going to start at zero, and then x is going to be zero. So it's going to say, all right, sum, which is 1 plus 0, equal 1. Then this sum's going to equal one, and that's going to um, go to one, you know, i[1], which is 3, i[2], and it's going to add them all; it's going to sum them all up.
So, that's demonstrated a little more. And I forgot to print it out! Then after this ends, sum. So it did all that, but it didn't show it; I forgot that. All right, I'm all over the place right now.
So, yeah, 34—that's all them added together. So that is an introduction to arrays. Just, uh, remember these little things when you're creating an array, um, and the little number in there is which one in the sequence you're going to be referring to, and it starts at zero, which is going to be confusing, but you're just going to have to get used to it. And “.length” is the length of the array.
So, yeah, see you next time!