Java - Array of Objects

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
how to work with an array of objects so here I have an employee class which I created in the last tutorial and taught you how to auto generate the code through the clips I just simply remove the Java comments so that you could concentrate more on the code then rather than having to concentrate on anything that we don't really need for this example so here we have got a constructor overloaded default and then bunch of getters and setters now we would like to create another class to establish Association we're going to call this one a demo employee class and we will going to include a public static void main and in this class we will going to learn how to create an array of objects dislike you can create an array of integers you can create an array of doubles you can create an array of objects of any class so far we've only looked at an array of objects of string type in one of the earlier tutorials for example the one that I introduced you earlier today intro to Java arrays where I created an array of names which is actually an array of objects so now in this example we would like to create an array off employee class so employee class array employees equals to new employees size 3 for example so now what exactly the syntax means is I would like to create an array called amps of type employee it's an array because I've used square brackets it's not one object I would like to allocate some extra memory not just one block with multiple blocks of memory and all blocks must be of type employee and I would like to allocate three of them do not get confused between this statement and instantiation because this is one extra thing that you got to do when you are dealing with array of objects you got to declare the array and then you go to instantiate each individual object separately however the good thing is that since all these objects are in a continuous blocks of memories by the increments of element number 0 1 2 3 4 it is very easy to instantiate all of them in the loop for example I could now be generating a loop which will going to run from zero all the way through the length of this array and in this loop I will going to simply call upon the constructor of the class and one by one instantiate each of the element of the array so that's basically what this code is doing first time the counter is 0 so it goes to the first element of em's array instantiate sit then counter becomes 1 second element it gets instantiated counter becomes 2 the third element gets instantiated so you have to declare an array and then you go to instantiate each individual object separately then you can be using these objects to be calling the getters and the setters and to be doing the whole nine yards so if I would like to call the setter upon one of the objects of the array I could simply say I would like to call the answer a the first object to call the set ID and assign it a value 101 then call the same object and then call the set name and give it a name and then call the same object but this time call set salary and assign it a salary value so all of these things can be done individually as if you would do in a regular setup for variables then I can copy/paste these efforts and do it for my second object this is the lengthy way of doing it I'm going to be showing you the improved way of doing that and make those changes and here you have two objects for each one of them call their respective setters and then pretty much put in the values here and then if you would like to call the getters upon both of these objects you can simply right here which will going to be very much like loop that I wrote above so I'll just copy this one because we programmers are a bit lazy so we like copy-paste a lot and then I could write a system dot out dot println statement in which I would like to display all values that pertains to each one of the objects so I would like to call the setter on ID name and salary for opt for first object then the setter on ID name uh trying to get her on ID name and salary for first object and then the Gator on ID name and salary on the second object so in order to do this I was going to simply write it in a loop and I'll going to write my message out here so what I've done over here is I have an array with three elements the first element has an ID of 101 name Amy salary hundred thousand second object or second element of the array has an ID of 102 named Abby and salary hundred and eighty thousand the third element has an ID of 103 named Jason salary two hundred thousand then I quickly wrote this loop that I was talking to you about earlier that I wrote this one long system dot out dot println statement in which I'm running a loop and I'm going from one element to the next however every time I go into loop I'm pulling all three properties for every element then doing a line separator so that you could see the difference between each one of the element after it gets printed and voila here is my code for all three of them separated by the line separators the benefit of using an array of objects is if I were to do it otherwise I would have to write my system dot out dot println statement as many times as I would have the number of objects of the employee class but introducing an array makes your life so much easier that you can actually loop through the process and do things much faster here if you can see this code also needs to be tremendously reduced if I could actually get my inputs from user as opposed to hard coding these values that I can actually streamline this process as well then I can actually run a lube and every time in the loop I accept three values from user populate the three elements of the array go back in the loop and then take the next three inputs for the next element and so and so forth which we're going to try to cover in our next tutorial so stay tuned keep watching and looking forward to seeing you all in the next tutorial thank you for watching this one
Info
Channel: Professor Saad
Views: 27,112
Rating: undefined out of 5
Keywords: Java, Object Array
Id: tp3J3Ep3JTU
Channel Id: undefined
Length: 7min 9sec (429 seconds)
Published: Wed Nov 13 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.