ARRAYS
Declaring
an Array Variable
•
Do not have to create an array while declaring
array variable
– <type> [] variable_name;
– int [] prime;
– int prime[];
•
Both syntaxes are equivalent
•
No memory allocation at this point Defining an
Array
•
Define an array as follows:
– variable_name=new <type>[N];
– primes=new int[10];
•
Declaring and defining in the same statement:
– int[] primes=new int[10];
•
In JAVA, int is of 4 bytes, total space=4*10=40
bytes Array Size through Input
BufferedReader
stdin = new BufferedReader (new InputStreamReader(System.in)); String inData;
int num;
System.out.println("Enter
a Size for Array:"); inData = stdin.readLine();
num = Integer.parseInt( inData ); // convert
inData to int
long[]
primes = new long[num];
System.out.println(“Array
Length=”+primes.length);
….
SAMPLE
RUN:
Enter a
Size for Array:
4
Array
Length=4
Default
Initialization
•
When array is created, array elements are
initialized
– Numeric values (int, double, etc.) to 0
– Boolean values to false
– Char values to ‘\u0000’ (unicode for blank
character)
– Class types to null
Accessing
Array Elements
•
Index of an array is defined as
– Positive int, byte or short values
– Expression that results into these types
•
Any other types used for index will give error
– long, double, etc.
– Incase Expression results in long, then type
cast to int
•
Indexing starts from 0 and ends at N-1 primes[2]=0;
int k =
primes[2]; Validating Indexes
•
JAVA checks whether the index values are valid at
runtime
– If index is negative or greater than the size of
the array then an IndexOutOfBoundException will be thrown
– Program will normally be terminated unless
handled in the try {} catch {}
•
long[] primes = new long[20];
•
primes[25]=33;
•
….
•
Runtime Error:
•
Exception in thread “main”
java.lang.ArrayIndexOutOfBoundsException: 25
•
at MorePrimes.main(MorePrimes.java:6)
Initializing Arrays
•
Initialize and specify size of array while
declaring an array variable int[] primes={2,3,5,7,11,13,17}; //7 elements
•
You can initialize array with an existing array
int[] even={2,4,6,8,10};
int[]
value=even;
– One array but two array variables!
– Both array variables refer to the same array
– Array can be accessed through either
variable name
Array
Length
•
Refer to array length using length
– A data member of array object
– array_variable_name.length
– for(int k=0; k<primes.length;k++)
….
•
Sample Code:
long[]
primes = new long[20]; System.out.println(primes.length);
• Output:
20 Sample Program class MinAlgorithm
{
public
static void main ( String[] args )
{
int[]
array = { -20, 19, 1, 5, -1, 27, 19, 5 } ;
int
min=array[0]; // initialize the current minimum for ( int index=0; index <
array.length; index++ )
if ( array[ index ] < min ) min = array[ index ]
;
System.out.println("The
minimum of this array is: " + min );
}
}
Arrays of Arrays
•
Two-Dimensional arrays
– float[][] temperature=new float[10][365];
– 10 arrays each having 365 elements
– First index: specifies array (row)
– Second Index: specifies element in that
array (column)
– In JAVA float is 4 bytes, total
Size=4*10*365=14,600 bytes
Multidimensional Arrays
•
A farmer has 10 farms of beans each in 5 countries,
and each farm has 30 fields!
•
Three-dimensional array
long[][][]
beans=new long[5][10][30]; //beans[country][farm][fields]
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.