Java Foundation With Data Structures Lecture 6: Arrays
Java Foundation With Data Structures Lecture 6: Arrays
Array
Description
What
are
arrays?
In
cases
where
there
is
a
need
to
use
several
variables
of
same
type,
for
storing,
example,
names
or
marks
of
‘n’
students
we
use
a
data
structure
called
arrays.
Arrays
are
basically
collection
of
fixed
number
of
elements
of
a
single
type.
Using
arrays,
saves
us
from
the
time
and
effort
required
to
declare
each
of
the
element
of
array
individually.
The
length
of
an
array
is
established
when
the
array
is
created.
After
creation,
its
length
is
fixed.
For
example:
{1,2,3,4,5}
is
an
array
of
integer.
Similarly,
an
array
can
be
a
collection
of
character,
Boolean,
Double
as
well.
Declaring
Array
Variables
To
use
an
array
in
a
program,
you
must
declare
a
variable
to
refer
the
array,
and
you
must
specify
the
type
(which
once
specified
can’t
be
changed)
of
array
the
variable
can
reference.
Here
is
the
syntax
for
declaring
an
array
variable
−
Syntax
datatype
[]
arrayRefVar;
//
preferred
way.
OR
datatype
arrayRefVar
[];
Example:
int
[]
arr;
OR
int
arr
[];
Creating
Array
Declaring
array
variable
does
not
create
array
(i.e.
no
space
is
reserved
for
array).
Here
is
the
syntax
for
creating
an
array
–
arrayRefVar
=
new
datatype
[array
Size];
Example:
arr=new
int
[20];
Combining
declaration
of
array
variable,
creating
array
and
and
assigning
the
reference
of
the
array
to
the
variable
can
be
combined
in
one
statement,
as
shown
below
–
datatype
[]
arrayRefVar
=
new
datatype
[array
Size];
Example:
int []
arr =new
int
[20];
//
This
statement
will
allocate
contiguous
space
for
20
integers
Array
Indexes
In
order
to
access
different
elements
in
an
array
-‐
all
elements
in
array
are
indexed
and
indexing
starts
from
0.
So
if
there
are
5
elements
in
array
then
first
index
will
be
0
and
last
one
will
be
4.
Similarly,
if
we
have
to
store
n
values
in
an
array,
then
indexes
will
range
from
0
to
n-‐1.
int
[]
arr=
{
1
,
2
,
3
,
4
,
5
};
arr[0]
arr[1]
arr[2]
arr[3]
arr[4]
i. Trying to retrieve an element from an invalid index will give an
ArrayIndexOutOfBondsException.
All
the
elements
of
the
arrays
after
creating
arrays
are
initialized
to
default
values
(if
we
not
initialize
them
while
creation).
Following
is
a
table
showing
default
values
for
various
data_types.
For-‐Each
Loop
This
is
a
special
type
of
loop
to
access
array
elements
of
array.
But
this
loop
can
be
used
only
to
traverse
array,
nothing
can
be
changed
in
array
using
this
loop.
For
Example:
public
class
Solutions
{
public
static
void
main
(String
[]
args)
{
int
[]
arr=
{10,20,30,40,50};
for
(int
i:arr)
{
System.out.print(i+"
");
}
}
}
Output:
1
2
3
4
5
Similarly,
When
we
pass
a
an
array
to
the
increment
function
shown
below
then
the
reference(address)
to
the
array
is
passed
and
not
the
array
itself.
public
class
Solutions
{
public
static
void
increment
(int
[]
arr)
{
for
(int
i=0;i<5;i++)
{
arr[i]++;
}
}
public
static
void
main
(String
[]
args)
{
int
[]
arr=
{1,2,3,4,5};
increment(arr);
for
(int
i=0;i<5;i++)
{
System.out.print(arr[i]+"
");
}
}
}
Output:
2
3
4
5
6
Here
reference
to
the
array
was
passed.
Thus
inside
increment
function
arr
refers
to
the
same
array
which
was
created
in
main.
Hence
the
changes
by
increment
function
are
performed
on
the
same
array
and
they
will
reflect
in
main.
Now,
lets
change
code
for
increment
function
a
little
and
make
arr
point
to
another
array
as
shown
in
example
given
below.
public
class
Solutions
{
public
static
void
increment
(int
[]
arr)
{
int
[]
arr1=
{1,2,3,4,5};
arr=arr1;
for
(int
i=0;i<5;i++)
{
arr[i]++;
}
}
public
static
void
main
(String
[]
args)
{
int
[]
arr=
{1,2,3,4,5};
increment(arr);
for
(int
i=0;i<5;i++)
{
System.out.print(arr[i]+"
");
}
}
}
Output:
1
2
3
4
5
Here
the
changes
done
in
main
didn’t
reflect.
Although
here
as
well
the
reference
to
the
array
was
passed,
but
in
the
first
line
inside
function
we
created
another
arry
of
size
5
and
made
arr
refer
to
that
array(without
affecting
array
created
in
main).
Thus
the
changes
this
time
won’t
reflect.