Java array and programming
example
Q.1 what is an array?
Ø Array is one type of value containing an array representing a group of elements of the same data
type. (homogeneous or similar data type)
Type of array:-
Single dimensional
array= 1D array
Multidimensional array = 2-D
array/ 3-D array
Array declaration creation and initialization?
·
Retrieve elements from an array by for and for-each loop.
·
Arrays are
object
·
Get a class
name for an array
·
What are
matrix and jagged arrays
·
Annonimius array.
·
Array
program (sorting, searching, margins, deleting, etc…)
Syntax:-- int emp_id [] = new int emp_id[100];
·
Array - an array is
an object that holds a fixed member of values of a homogenous of similar
data type
·
Say an
array Is a data stricter where we store
similar element
·
The
length of an array is assigned when the array is created and after creations, its length is fixed.
Example:-
int a [] = new int[10];
It will create an array of length 10 index values we always start from 0(zero).
Feature of an array:-
1.
Java array variables can be declared like
other variables with [] after the data type.
2.
The
variable in the array are ordered
and each has an
index beginning from 0(zero)
3.
In java
arrays object and thus they occupy
memory in “heap array”.
4.
The
direct supper class of an array type of object.
5.
They
are always created
at runtime
6.
The
length of an array can be found by using member length. This is different from C /C++
where we find length using the size of.
7.
The
elements of an array are stored in consecutive memory locations.
Advantages of an array:-
1.
Array is
used to store multiple data items of some type by using only a single name.
2.
We can
access any element randomly by using
index provided by arrays
3.
Arrays can
be used to implement other data structures likeliest, stacks, queues, trees,
graphics. etc...
4.
Primitive
type of wrapper classes
object the connection will not happen so it
fast
The disadvantage of an array:-
Fixed-size:- we need to mentation the size of the array thus they have a fixed size .when the array is created size cannot be changed.
Memory wastage:-
there is
a lot of chances of memory wastage supposes we create
an array of length 100 but only 10 elements are inserted then 90 blocks
are empty and thus
memory wastage
Strongly typed:- array stores only similar data type thus strongly typed.
Reduce performances:- the
elements of an array are stored in a consecutive memory location, thus to declare an element in an array we
need to travel throughout the memory so this will reduce performances.
No methods:- array does not have add or removed method.
0 Comments