Arrays in jAVA
Study Java One and Two Dimentional Arrays.
THEORY
Arrays
array is a group of like-typed
variables that are referred to by a common name. Arrays of any type can be
created and may have one or more dimensions. A specific element in an array is
accessed by its index. Arrays offer a convenient means of grouping related
information.
One Dimensional Arrays
A
one-dimensional array is,
essentially, a list of like-typed variables. To create an array, you first must
create an array variable of the desired type. The general form of a one
dimensional array declaration is
int month_days[];
This declaration establishes the fact that month_days is an array
variable,
no
array actually exists. In fact, the value of month_days is set to null,
which represents an array with no value. To link month_days with an
actual, physical array of integers, you must allocate one using new and
assign it to month_days. new is a special operator that allocates
memory.
The
general form of new as it applies to one-dimensional arrays appears as
follows:
array-var = new type[size];
It is possible to combine the
declaration of the array variable with the allocation of
the array itself, as shown
here:
int month_days[] =
new int[12];
Prorgam 1
program fill an array of 10 elements
by randomly generated Integers,
range (1-100).
import
java.lang.Math;
public class array1
{
public static void main(String args[])
{
int a[] = new int [10];
for (int i=0;i<10;i++)
a[i]=(int)(Math.random()*100);
System.out.println("Values
are");
for (int i=0;i<10;i++)
System.out.println(a[i]);
}
}
Output:
Multidimensional Arrays
Java, multidimensional arrays are actually arrays of arrays. These, as
you might
expect,
look and act like regular multidimensional arrays.
For
example, the following declares a two-dimensional array variable called twoD.
int twoD[][] = new
int[4][5];
possible to initialize multidimensional arrays. To do so, simply enclose
each
dimension’s
initializer within its own set of curly braces. following program creates a
matrix where each element contains the product of the row and column indexes.
Also notice that you can use expressions as well as literal values inside of
array initializers.
// Initialize a two-dimensional
array.
class Matrix {
public static void main(String
args[]) {
double m[][] = {
{ 0*0, 1*0, 2*0, 3*0 },
{ 0*1, 1*1, 2*1, 3*1 },
{ 0*2, 1*2, 2*2, 3*2 },
{ 0*3, 1*3, 2*3, 3*3 }
};
int i, j;
for(i=0; i<4; i++) {
for(j=0; j<4; j++)
System.out.print(m[i][j] +
" ");
System.out.println();
}
}
}
Output:
When
you allocate memory for a multidimensional array, you need only specify the
memory
for the first (leftmost) dimension. You can allocate the remaining dimensions
separately.
when
you allocate dimensions manually, you do not need to allocate the same number
of elements for each dimension. As stated earlier, since multidimensional
arrays are actually arrays of arrays, the length of each array is under your
control. For example, the following program creates a two dimensional array in
which the sizes of the second dimension are unequal.
Prorgam 2
program manually allocate differing size second dimensions.
public class array2 {
public static void main(String args[]) {
int twoD[][] = new int[4][];
twoD[0] = new int[1];
twoD[1] = new int[2];
twoD[2] = new int[3];
twoD[3] = new int[4];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<i+1; j++) {
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<i+1; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
Output:
No comments:
Post a Comment