Вы находитесь на странице: 1из 3

Exploring One D array in Java

Array class

How many different type of varaibles can we create in Java?


3 types:
Instance Variable- Declared within the class outside any method or block. Auto
initalized to default value based on data type.
int,byte,short,long =>0
float,double=>0.0
boolean=>false
char=>\u0000
String,Date=>null
Class|Static Variable-Declared within the class outside any method or block and
starts with the STATIC KEYWORD. Auto initalized to default value based on data
type.
int,byte,short,long =>0
float,double=>0.0
boolean=>false
char=>\u0000
String,Date=>null
Local Variable -Declared with a method or even a block and hence needs to
initialized. We cannot use it before initializing it.

Instance variable is getting its memory from instance of a class. which means heap
memory of JVM.
Class variable is getting memory from method area of JVM.
Local Variable gets memory from stack.

A static context can only access another static data|method.

What do you mean by an array?


An array is a data structure which is used to store elements of same data type in
contiguous memory location. The array index starts from zero and it cannot be
negative.

In Java, Array is considered as a reference type and NOT A PDT.

int [] arr;// no memory allocation happens


int[] arr=new int[num];// memory allocation happens
Other way to create an array in Java is array initializer block,

int arr[]={1,2,5,7,5};

How to traverse an array?


a) for loop using arr.length property.
b) for each loop
for(int a:arr)
System.out.println(a);
Restricted to manipulating the contents of the array.
c) We can use java.util.Arrays class to print the contents of the array in a single
line.
System.out.println(Arrays.toString(md3.arr));

This toString() actually is owned by java.lang.Object class.


The java.lang.Object is the supreme class in Java hierarchy.

The classes like Arrays,String,all Wrapper classes,all Collection related class


have OVERRIDDEN this toString() method as per requirement.
The default working of toString() method is to display ClassName@HexaMemAdd
For ex:MainDay3@15db9742 when we write
System.out.println(md3);// it is implicitly calling the toString() method.

The java.lang package is the default package.

Some more methods of Arrays class


a) To sort an array
Arrays.sort(arr);

b) Arrays.binarySearch(arr,num);
If the num is present, it prints the first location (zero based)

1,2,5,5,7
5
2

If the num is not present, it prints its position where it could be present(one
based) with a -ve sign.

1,2,5,5,7
4
-3

c)Arrays.fill() is used to populate an array with some value.

Arrays.fill(md3.arr,4); This code replaces each element by 4.


Arrays.fill(md3.arr,0,2,4); This code replaces first two elements by 4.
The second argument is start index (zero based)
The third argument is end index (one based)
The last argument is the number which will replace.

d) Arrays.copyOf() method creates a new array by copying elements from another


array.

int b[]=Arrays.copyOf(md3.arr,md3.arr.length); //returns an array ref

When we compard the md3.arr and b arrays using .equals() method, it printed true.
This is becoz both have identical contents.

If we compare them using == operator, it prints false. becoz == compares memory


addresses which in this case is not same.

The .equals() method is owned by java.lang.Object class and again overridden by


Arrays,String,all Wrapper classes as well as collection related classes. Hence,
they always check the CONTENTS AND NOT MEMORY REFERENCES.

e)Arrays.equals()

f)Arrays.copyOfRange(md3.arr,2,5);// copies three elements

The second argument is start index(zero based)


The last argument is end index(one based)

WAP to accept an array input from user and print the sum of second largest and
second smallest element in the array.

Sample Input
8
2 5 2 1 2 5 8 4

Sample Output
7

Вам также может понравиться