Learn Arrays In Java In English Easy And Simple! NowOnlyJava.


ARRAYS





Learn Arrays In Java In English Easy And Simple! NowOnlyJava.

------------------------------------------------------------------------

Hey what's going on everybody it's your bro hope you're doing well and in this video i'm going to teach you guys how arrays work in java so sit back relax and enjoy the show make sure you like comment and subscribe one like equals one prayer for the youtube algorithm all right welcome back ladies and gentlemen in this video i'm going to explain arrays an array is used to store multiple values in a single variable that is it they're really not that complicated but they can seem kind of intimidating so let's begin with a simple string variable called car and i will assign this a value of camaro what i could do is that i could store multiple values within the single variable by turning this variable into an array and these are the steps to do so next to the data type i'm going to add a set of straight brackets and then with the values i'm going to surround the values with a set of curly braces and that is it and for fun i'll rename this as cars because that makes more sense so it would make more sense to name this something that is plural because it contains more than one value so let's add a few other cars let's say i would like to add a corvette and a tesla and that is it that is an array of cars so in order to access one of these elements arrays have spots kind of like parking spots and they are called elements so let's say i would like to access this first element so i'm going to take the name of my array which is cars add a set of straight brackets and then list the element number so computers always start at zero if i want to access this first element i'm going to write zero and i could reassign this let's say i would like to instead place my mustang within element number zero and let's print whatever is within the first element of our array of cars cars straight brackets zero so this will print my string of mustang if i want to access the next element so this is zero element number zero and the next one is element one then two so on and so forth in my next element of my array of cars we have a corvette and then a tesla so what happens if i attempt to access an element that does not exist so let's put three here well what we'll get is an array index out of bound exception because this array does not have this element element number three it only has elements zero one and two but i could add another element let's say i'm going to add a bmw to element number three so then we no longer get that error because our array has a total of four elements zero one two three one thing that you should know with arrays when you assign values they all have to be the same data type they have to be consistent for example i couldn't add the primitive integer value of 1 2 3 because what this states is that this is a type mismatch cannot convert from int to string so if you have an array of strings for example you can only add strings if this was an array of integers well i could only add integers then to this array so you have to make sure that the data type of the values that you're adding are all consistent with the data type of the array now there is an additional way to create an array and that is by first allocating the amount of elements that we'll need and then storing the values later on in the program so this is an additional way to write an array we type in the data type of the array straight brackets the name of the array we'll call it cars equals new the data type again straight brackets semicolon within the straight brackets we'll assign how many elements we would like within this array let's say we would like three so we can assign a total of three strings to our array of cars and let's do that so later on in this program right here is a good spot we will assign each of the elements of our array of cars so cars at element number zero will equal my camaro and then cars at element one will equal a corvette and cars at element two will equal a tesla and then we can display each element of this array so let's begin with cars at zero this contains the camaro then the corvette and then the tesla so this is an additional way to write an array we can first declare the amount of elements that we would like for this array and then we could assign the values later on in the program before we finish this video i'm going to explain how we can use a for loop to iterate through all of the elements of an array let's say we would like to display all of the elements of this array so let's create a for loop to do that four a set of parenthesis and then a set of curly braces with for loops there are three statements the first is that we need some sort of index or counter so let's say int i equals zero that is the first statement for the second statement this is our condition we'll continue this for loop as long as i is less than our array cars dot length and lastly we will increment our index by one so let's display whatever is within our array of cars at element number i so i is going to begin at zero then after each iteration of this for loop it's going to increment by one so when we run this this will display all of the elements of our array of cars camaro corvette and tesla all right everybody so that's what an array is it's really just used to store multiple values within a single variable if you need to access one of the elements of an array you just list the name of the array and the element number in which you're trying to access. 

Comments

Popular posts from this blog

Percentage Calculator Project In Java Easy and Simple.