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
Is This Literally The Best Investing Strategy that Exists?
We’re now almost halfway through 2023, and while we’ve seen inflation cool in recent times, there’s no doubt we’re still battling with high interest rates, which ultimately put the brakes on the economy and slow business. While this kind of environment is…
This thought experiment will help you understand quantum mechanics - Matteo Fadel
After a long day working on the local particle accelerator, you and your friends head to the arcade to unwind. The lights go out for a second, and when they come back, there before you gleams a foosball table nobody remembers seeing before. Always game, y…
Unlocking the Eyes | Explorer
[Music] What boggles my mind about the eye is everything. But I’m really, really excited by the advances in technology made possible by research, not just into the eye, but into how natural selection caused it to be what it is. The next few decades are go…
April 2019 Q and A
Hi everyone, thanks for tuning in to the April 2019 Q&A. Probably won’t do another one till June; I’m traveling almost completely through May. Although it’s getting easier to do these on the road, camera quality usually isn’t as good, so we’ll see wh…
String theory - Brian Greene
In the year 1919, a virtually unknown German mathematician named Theodor Kaluza suggested a very bold and, in some ways, very bizarre idea. He proposed that our universe might actually have more than the three dimensions that we are all aware of. That is,…
Why Isn't the Stock Market Crashing?
Hey guys, welcome back to the channel! In this video, we’re going to be talking about, we’re going to be trying to answer the question: why isn’t the stock market crashing now? For this video, we are going to look over in America. We’re going to be focusi…