document for programming in java

3
SINGLE DIMENSIONAL ARRAYS  This is the simplest of array and is referred to as 1-D array. When an arra y is de cl ar ed, memor y fo r number of el ements of a particular type is assigned to the array during compile time. This allocation is called compile time allocation. This means that even if the array is partially lled, the memory for the entire size will be held by the array. nitialization is the process of assigning a value to each array element when it is rst declared. Declaration of one di mensional arrays Every array declaration has : ! name or identier with which it is referenced. "ize that determines the ma#imum number of elements that the array can store at a time.  The data type determines the type of data that the array will hold.  The individual elements are referred to using subscripts or indices which show the position of the elements in the array. The subscript or location ranges from position $ to n-1 where n is the size of the array. Initialization of one dimensional arrays Initialization is the process of assigning a value to each array element pent when it is rst declared. The individual elements are referred to using subscripts or indices and show the position of the element in the array. The subscripts or location ranges from position $ to n-1 where n is the size of the array. Synta :  T ype identier %& ' (data values separated by commas)*

Upload: angad-sood

Post on 18-Feb-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Document for programming in JAVA

7/23/2019 Document for programming in JAVA

http://slidepdf.com/reader/full/document-for-programming-in-java 1/3

SINGLE DIMENSIONAL ARRAYS

 This is the simplest of array and is referred to as 1-D array. When

an array is declared, memory for number of elements of a

particular type is assigned to the array during compile time. Thisallocation is called compile time allocation. This means that even

if the array is partially lled, the memory for the entire size will be

held by the array.

nitialization is the process of assigning a value to each array

element when it is rst declared.

Declaration of one dimensional arrays

Every array declaration has :

! name or identier with which it is referenced.

"ize that determines the ma#imum number of elements that

the array can store at a time.

 The data type determines the type of data that the array will

hold.

 The individual elements are referred to using subscripts or indices

which show the position of the elements in the array. The

subscript or location ranges from position $ to n-1 where n is the

size of the array.

Initialization of one dimensional arrays

Initialization  is the process of assigning a value to each array

element pent when it is rst declared. The individual elements are

referred to using subscripts or indices and show the position of 

the element in the array. The subscripts or location ranges from

position $ to n-1 where n is the size of the array.

Synta :

 Type identier %& ' (data values separated by commas)*

Page 2: Document for programming in JAVA

7/23/2019 Document for programming in JAVA

http://slidepdf.com/reader/full/document-for-programming-in-java 2/3

Eam!le :

nt !%& ' (1$, +$, $, $, $)

 This means that while declaration itself the memory is allocated

and each array element is given a value. /rom the diagram below

you can see the e0ect of the declaration and initialization

statement.

ore 2#amples

Double Temp %& ' (1., +$.+, 13., 4.+)*

5har 6%& ' (758, 7h8, 7a8,8r8,88,8e8,8s8)*

Accessin" Array Elements

2ach item in an array is called an element, and each element is

accessing by its numerical inde#. The individual elements can be

accessed using the array name and the inde# or subscript within

brac9ets. Ta9e the case of the same array as before.

int !%& ' (1$, +$, $, $, $)*

Synta :

!rray name %inde#&

2ach element can be accessed or initialized using the following

method:

!%$& ' 1$* !%1& ' +$* !%+& ' $* !%& '$* !%& ' $*

Array Len"th # Ece!tions

When dealing with array, the 7number of elements contained

within the array is the array8s $len"th%. This length can be

obtained by using the array name followed by the length. f an

array named Data  contains 1$ values, the code Data& Len"th

will be 1$. The len"th of an array is the number of elements in

the array, which is one more than the lar"est s'(scri!t. The

length is a data member and not a method . "o it m'st not have

(rac)ets*+ at the end.

Page 3: Document for programming in JAVA

7/23/2019 Document for programming in JAVA

http://slidepdf.com/reader/full/document-for-programming-in-java 3/3

f the value of an inde# for an array element is negative, a

decimal, or greater than or e;ual to the length of array <because

the last s'(scri!t is array len"th , -=, an error message will be

Array Inde O't Of .o'nds Ece!tion&

f you see this message, immediately chec9 to see how your array

is being utilized.

/sin" for loo! to access arrays

"ince the array elements lie in contiguous locations stored from

location $ to length-1, it is very convenient to use for loops to

access entire arrays. When the number of elements are large in

number it is not practical to manipulate each element separately.

In!'t0O't!'t data of -1D array

Data can be input into an array using the same "ystem in stream.

 The following program inputs data into an array and then displays

data from two arrays. The string array is initialized with names at

the beginning.

Memory Re!resentation of a -1D array!n array is dened as a named set of elements of similar type

stored in contiguous locations. The memory address of the rst

element is called the (ase address. The other element are

automatically stored in continuous locations depending on the

basic size of each element. The name of the array is the address

of the rst element of the array.

Eam!les:

short 6um %& ' new short %&*

>ase address here is $$$.

/loat ?rice %& ' new @oat %&*

>ase address here is $$$

2ach @oat element is of bytes so the address is incremented by

bytes.s