yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Java Lesson 11 | Array Basics


3m read
·Nov 3, 2024

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!

More Articles

View All
Examples recognizing transformations
What we’re going to do in this video is get some practice identifying some transformations. The transformations we’re going to look at are things like rotations, where you are spinning something around a point. We’re going to look at translations, where y…
Nanotechnology: A New Frontier
The world is shrinking. There’s a deep and relatively unexplored world beyond what the human eye can see. The microscopic world is truly alien and truly fascinating. I’m delving further than the microscopic scale; I’m going to explore the potentials of wo…
Why do midterm congressional elections matter? | US government and civics | Khan Academy
[Narrator] Why do midterm congressional elections matter? Congressional elections matter because they are often, and have increasingly been, a referendum on the president. So, it is a kind of real test from real voters doing real voting about whether pe…
The Constitution, Bill of Rights, and Landmark Supreme Court Cases - Course Trailer
The Constitution of the United States is a crucial document to understand if you’re a United States citizen, and frankly for almost anyone on the planet. Obviously, the United States is an influential country, but beyond that, many of the countries out th…
Belonging | Short Film Showcase | National Geographic
Foreign. [Music] This is what I dream of. Sometimes it feels like I’m still there. [Music] One amongst hundreds of others like me, to travel far to be here is without a name. Thank you. We would watch the moon, rain the water darkened, then celebration…
What Kinectimals SHOULD Be Like -- Wackygamer
Um, you turned me on to this. I missed this game. What is this game? Uh, Kinectimals! Kinectimals! For those of you who didn’t see the trailer, you can go check it out. It is a game where you have a virtual pet. There have been these games before, yeah, t…