Java Fundamentals

Arrays Q&A

Angie Jones

Angie Jones

Java Champion
Java Fundamentals

Check out a free preview of the full Java Fundamentals course

The "Arrays Q&A" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Angie demonstrates writing the printTicket for loop using i and discusses why a do while loop was used instead of a while loop. Student questions regarding how to print on one line and if the terms mutable and immutable are used in Java are also answered in this segment.


Transcript from the "Arrays Q&A" Lesson

>> If you were to have written this for Loop, I'll write it here and just comment it out. But if you were to use this with i, Another thing, so we could have said, the hard coded constant value. But another way, sometimes you won't have those values defined, you have a method that accepts an array, you don't know what this array has in it.

So you can do something like array.length, and that'll give you the number of elements that's in that array, and then i++. And then inside of here I would have just printed out ticket, no, not ticket, it's called array, right? Array i+, and then this little pipe thing. So I would have accessed the element by its index.

So this is another way to do it. I'll just comment it out. Okay? Questions on arrays? Pretty clear? Why did we use the do while instead of a while here?
>> Cuz you wanted it to run at least once.
>> Yeah, we want to run at least once.

So you see, there are valid cases. So when people are like, well, why ever use a do while? I have a little problem with that, this makes logical sense to do that, right? Why would I search to see if the number has been there, if I haven't even generated the number first, all right.

>> What did you do to make the numbers pronounced the same line again?
>> Yeah, instead of print in, it's just print, yeah.
>> I guess there is one, are the terms mutable and immutable used in Java?
>> Yes.
>> Kinda related to the constant?
>> They are, and we actually gonna talk about immutability in this very next section I'm gonna go over, but yes, they do exist in Java.

>> The other part is, right now we're going between 1 and 68, so I think that's what we could also just.
>> Very good, okay, great. So, all right. So, what's your name?
>> Dave.
>> Dave pointed out that this bound here is not inclusive. So when we said next in, it's only gonna do one, if origin is included, is this inclusive?

>> That's inclusive, yeah.
>> Okay, so the first parameter is inclusive, meaning it will be a possibility, but the last one is a cap, meaning it will not. So we could do something like +1 right here. And that way we include max ticket number, so our bound would be 70, right?

>> That feels kinda like it works and it feels like you're just very kludgy in terms of how it's implemented.
>> You could change this to this, [COUGH] 70 right here.
>> And then just have a comment about it just saying inclusive of bounds or something like that.

Yeah, what I would actually do, I wouldn't do a origin. What I would do personally, I would do max number, let it just create it and then I will do +1.
>> Okay, that feels a little bit better.
>> Like that? And the reason I would do that is because I don't want 0 included either.

So that really works on either of the terms. So create a number between 0 and 68, 69 somewhere in between it and add add one to it, right? That way, that's my range.
>> Java Docs.
>> Java Docs, yeah, Java Docs are great. Just for the sake of time, we haven't been writing any on our own.

But we should, and to create one, it's really simple. Right above the method itself you would do, I can't remember if this is slash or backslash. Is that back or is that front? Whatever that is, some sort of slash, [COUGH] with an asterisk. And then hit enter, and that's the closing one.

But in that Java doc, you would then specify like, what does this method do you have a little one line there and then you would enter the parameters it takes, what it returns and a nice little description? So that's being a good developer. All right so, that is a race.

>> How did you access the Java doc?
>> I clicked on, [COUGH] so let's go to a method that this outside of our class, the random may be. So this random.nextInt. I click command, hold command down and then click it, and that takes me into here and you can see how the Java doc is written.

Basically, what is this? Slash two asterisks, and then notice it has the comments, and then it has the parameters. I don't know why it has all that, that's a lot, [LAUGH] this is a book, here's the parameters, so you would do @param. And then you could specify the parameters, a little bit of information about them, exclusive, the upper bound must be positive.

Those are good things to know, that helped us in our code, right? And whatever it'll be returned you let them know that.
>> I bet you the Java doc for var is probably even more convoluted given how much the guessing it's doing other kinds of logic you need to guide that.

>> Up there is a dark.
>> Wouldn't be? So they usually are on methods and var is not let's see.
>> What else we still kinda internal logic for like-
>> Yeah, I mean there's Doc's written on, I just don't know if it's in the form of Java Docs, but there I'm sure there's something on Oracle written on it, let me see.

Yeah, there's nothing here.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now