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
Warren Buffett: "Rule #1: Never lose money. Rule #2: Never forget rule #1."
Warren Buffett: The first rule of investment is: Don’t lose. And the second rule of investment is: Don’t forget the first rule. And that’s all the rules there are. I mean, if you buy things for far below what they’re worth, and you buy a group of them, yo…
COMIC-CON 2010: Halo: Reach Exclusive HD Footage - Forge World Beyond the Canyon, LE Xbox and more.
Hey everybody, Jeff Rman and Adam Mlin here from Wacky Gamer. We’re here at the 2010 Comic-Con. We’re going to be posting a bunch of footage next week on Wacky Gamer Comedy, so make sure to subscribe. But for now, check out this awesome footage from the …
Canyon Catharsis | Badlands, Texas
I’ve been through this canyon over 750 times. This is a place where you can hear the voice of God bouncing off these walls in the wind. There’s no roar, no freeways, no trial. Silence. For years, I was a river guide here with Tony. I remember when Tony f…
Product and Media Are New Leverage
The most interesting and the most important form of leverage is this idea of products that have no marginal cost of replication. This is the new form of leverage. This was only invented in the last few hundred years. It got started with the printing press…
Lecture 17 - How to Design Hardware Products (Hosain Rahman)
Very exciting! And thank you, Sam, uh, for having me. Sam and I have known each other for a long time because we were fellow Sequoia companies, and we met in the early days of when he was on his, uh, company journey. So it’s cool! So what he asked me to t…
Backcountry Basics: Navigating With or Without Technology | Get Out: A Guide to Adventure
My name is Hillary O’Neal and I am a professional ski mountaineer and adventurer. Today, we are going to talk about backcountry navigation. There are a lot of uses in many different environments that would require some level of skill in backcountry naviga…