Now, I hope that you know the encapsulation very well, if you don't then in same blog you can find separate post of Encapsulation, With the help of that I am going to explain the Array.
- First create and declare animal array ( you can change name to anything you like) "Animal[] reptile;"
- Now this animal will hold the 5 references that is " reptile = new Animal[5];".
- Now you can create another reptile objects: " reptile[0] = new Animal(); reptile[1] = new Animal(); reptile[3] = new Animal(); and so on........
- Now we use that methods in our programs with help of encapsulation. e.g. reptile[0].setSize(10); or int count = reptile[0].getSize() and use same in each methods.
_________________________________________________________________________
An array hold fixed values which is called element and each element is accessed by its index. You can understand this from below image.
Now the below program explain an array process in very easy manner.
___________________________________________________________
public class Anything {
public static void main(String[]args){
int[] numb;
numb = new int[5];
numb[0] = 9;
numb[1] = 54;
numb[2] = 99;
numb[3] = 23;
numb[4] = 43;
System.out.println("In index 0 its "+numb[0]);
System.out.println("In index 1 its "+numb[1]);
System.out.println("In index 2 its "+numb[2]);
System.out.println("In index 3 its "+numb[3]);
System.out.println("In index 4 its "+numb[4]);
}
}
_________________________________________
In the above program, we declare int array with length of 5 and assign it to int[] numb that is " numb = new int[5]". And give each element a value.
Now after assign the value, you can call in System.out.println with deserved numb value.
You can copy and paste this code but if you start write this code manually than, it really help you to remember this methods. Try it on your own than tell me the difference !
Thank you for viewing this post.........
No comments:
Post a Comment