A rrrrrrrrrrrrrrrrray!

Array's


 · An array is an indexed collection of fixed number of homogeneous data elements.
· The main advantage of arrays is we can represent multiple values with the same name
so that readability of the code will be improved.
· But the main disadvantage of arrays is:
· Fixed in size that is once we created an array there is no chance of increasing or
decreasing the size based on our requirement that is to use arrays concept compulsory
we should know the size in advance which may not possible always.
· We can resolve this problem by using collections

Array initialization: Whenever we are creating an array every element is initialized with default
value automatically.
Example 1:
int[] a=new int[3];
System.out.println(a);//[I@3e25a5
System.out.println(a[0]);//0

Diagram:


Note: Whenever we are trying to print any object reference internally toString() method will be
executed which is implemented by default to return the following.
classname@hexadecimalstringrepresentationofhashcode.
Example 2:
System.out.println(a);//[[I@3e25a5
System.out.println(a[0]);//[I@19821f
System.out.println(a[0][0]);//0






length Vs length():
length:
1) It is the final variable applicable only for arrays.
2) It represents the size of the array.
Example:
int[] x=new int[3];
System.out.println(x.length());//C.E: cannot find symbol
System.out.println(x.length);//3

length() method:
1) It is a final method applicable for String objects.
2) It returns the no of characters present in the String.
Example:
String s="bhaskar";
System.out.println(s.length);//C.E:cannot find symbol
System.out.println(s.length());//7
· In multidimensional arrays length variable represents only base size but not total size.
Example:
int[][] a=new int[6][3];
System.out.println(a.length);//6
System.out.println(a[0].length);//3


SHARE

About df

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment