What is an array ? Write about one dimensional array?
https://www.computersprofessor.com/2017/01/what-is-array-write-about-one.html
Array:
|
An array is a
group of contagious (or) continuous related data items that share a common
name. a particular value in an array is indicated by writing a number called
index number (or) subscript in brackets([ ]) of the array name.
For example
salary[10]; represents the salary of the 10th employee while the
complete set of values is referred to as an array, the individual values are
called elements.
One dimensional arrays:
A list of
items can be given one variable name using only one subscript and such a
variable is called a single subscripted variable(or) an one dimensional
array.
For example if
we want to represent a set of five numbers say (9, 10, 11, 12, 13) while an
array variable number then we may create the variable number as follows.
int number [
]=new int[5];
The values to
the array elements can be assigned as follows.
number [0]=35;
number[1]=40;
In java subscript
starts with the value o these elements may be used in programs just like any
other java variable. For example the following are valid statements.
A
number=number[0]+10;
Number[4]=number[0]+number[2];
The subscript
of an array can be integer constant, integer variables like I, or expressions
that yield integers.
Creating an array:
Like any other
variable, arrays must be declared and created in the computer memory before
they are used.
Creation of an
array involves three steps
1. Declaring
the array
2. Creating
memory locations
3. Putting
values into the memory locations
Declaration of
arrays:
Arrays in java may be declared in two
forms:
Examples:
int number [
];
int [ ]
counter;
remember, we
do not enter the size of the arrays in
the declaration.
Creation of
arrays:
After
declaring an array, we need to create it in the memory. Java allows us to create
arrays using new operator only 1 as shown below
Ex:
number=new
int[5];
Create
necessary memory locations for the array number and designate as int. now the
variable number refers to an array of 5 integers.
It is also
possible to combine the two steps declaration & creation into one as
follows:
int number [ ]
=new int[5];
initialization of arrays:
the final step
is to put values into the a may created. This process is known as
initialization. This is done using the array subscript as shown below.
Ex:
number[0]=35;
number[1]=40;
Note that java
creates arrays starting with the subscript of 0 and ends with a value oneless
that the size specified.
We can also
initialize arrays automatically in the same way as the ordinary variables when
they are declared as shown below.
int number[
]={35, 40, 20, 57, 19};
it is possible
to assign an array objects to another. Example
int a[ ]={1,
2, 3};
int b[ ] ;
b=a;
are valid in
java.
Array length:
In java, all
arrays store the allocated size in a variable named length. We can obtain the
length of the array a using a length.
Ex:
int
asize=a.length;
Ex: Program for Sorting of numbers
class
NumberSorting
{
public static
void main(String args[ ] )
{
int number [ ]
={55, 40, 80, 65, 71};
int
n=number.length;
System.out.println(“given
list:”);
for (int
i=0;i
|
{
System.out.println(“
“+number[i]);
}
System.out.println(“ \ n ”);
for (int i=0;
i < n ; i++)
{
for(int
j=i+1; j < n ; j++)
{
if(number [ i
] < number [ j
])
{
int
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
}
System.out,println(“sorted
list”);
for (int i=0;
i < n ; i++)
{
System.out.println(“
“ +number [ i ]);
{
System.out.println(
“ “);
}
}