Java array of objects ๐Ÿฑ

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
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 we can create an array of objects in java so sit back relax and enjoy the show you too can become a hero and save our channel by punching the like button throw a comment down below and smash the subscribe button alright guys and gals in this video i'm going to be explaining how we can create an array of objects now here's a few examples of arrays that we've worked on in the past so the standard formula or steps to creating an array is to type in the data type that we're working with a name for this array equals new the data type again and then a size if you don't plan on assigning values right away from the beginning when you declare this so we have an array of integers an array of characters and an array of strings so what if we wanted to create an array of objects for example i have a class called food we're going to be storing some food objects within an array so following these simple steps let's begin creating an array of objects so we begin by typing in the name of the data type that we are working with the name of the data type that we are working with is food for this example so we're going to type in food followed by a set of straight braces and then we need a name for this array so we could call this foods but i don't think that would make too much sense so let's pretend that we're placing food in a refrigerator equals new the data type again which is food and then a size if we do not plan on assigning values right away so let's say that this refrigerator can hold three food items so we now have an array that can hold some food objects for us now let's create some food objects so let's do this after we create our array called refrigerator so let's create three foods to store within this refrigerator so we type in the name of the class which is food let's call this food one equals new food and let's set up the constructor of our food class so it's the same name as the class and let's say we want to set up a parameter for a name string name this dot name equals name and then we need to declare this outside the constructor string name so when we create a food we need to pass in a name so for food one let's say that this is pizza it will create a food 2 item and a food 3 item let's say food 2 will be a hamburger we'll pass that in as an argument and food three will be a hot dog so we now have three food items now we are going to store these food items in our array of food objects our refrigerator as we call it so in order to do that we type in the name of the array followed by an index so this always begins at zero because computers always start with zero so our refrigerator at index zero let's store food item number one which is our pizza and for index one we'll store food two and index two will store food three and for practice let's attempt to access one of these elements of our array of food objects called refrigerator and display it to the console window so we can do that with a print line statement and let's attempt to access whatever food object is at index zero so refrigerator index zero and let's see what happens now remember that this returns the array of where this object is in your computer's memory so this is the address of where food 1 is located in memory what i'm trying to do is access the name variable of our food object so i'm going to tack on dot name to the end of this because we have a name variable for all of our food objects so this will print the name of whatever food is in element number zero of our refrigerator which is pizza and we can do the same process for the other elements too so i'm going to print element number one as well as element number two so if we were to take a look at all of the elements within our refrigerator we have pizza a hamburger and a hot dog now there's another way of writing this too instead of declaring our array of food objects called refrigerator and setting a memory size this time we're going to declare our array of food objects and assign all of these values right away so after we create our food objects let's declare and assign these values to our array of food objects so we type in the name of the data type followed by a name refrigerator equals and then within curly braces all of the values that we would like to add we would like to add food 1 food 2 and food 3. and we do not need these lines anymore so we can take these out and this will do the exact same thing but we changed the order of these steps around so that's the basics of arrays of objects in java if you'd like a copy of all this code i will post all of this in the comments down below but yeah that's the basics of arrays of objects in java hey you yeah i'm talking to you if you learn something new then you can help me help you in three easy steps by smashing that like button drop a comment down below and subscribe if you'd like to become a fellow bro [Music] you
Info
Channel: Bro Code
Views: 16,123
Rating: undefined out of 5
Keywords: java array of objects, java array of objects example, java, array, objects
Id: nhIB2S6NiFA
Channel Id: undefined
Length: 6min 15sec (375 seconds)
Published: Tue Sep 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.