# 4.4 Arrays and array processing  (Page 2/4)

 Page 2 / 4

## Array accesses

• Indices for arrays must be `int` values that are greater than or equal to 0 and less than the length of the array. Remember that computer scientists always count starting at zero, not one!
• All array accesses are checked at run time: An attempt to use an index that is less than zero or greater than or equal to the length of the array causes an `IndexOutOfBoundsException` to be thrown.
• Array elements can be used on either side of an equals sign:
• `myArray[i] = aValue;`
• `someValue = myArray[j];`
• Accessing elements of an array is fast and the time to access any element is independent of where it is in the array.
• Inserting elements into an array is very slow because all the other elements following the insertion point have to be moved to make room, if that is even possible.

## Array processing using loops

More information on loops can be found at the Java Resources web site page on loops .

The main technique used to process arrays is the for loop . A `for` loop is a way of processing each element of the array in a sequential manner.

Here is a typical `for` loop:

```// Sum the number of elements in an array of ints, myArray. int sum = 0; // initialize the sumfor(int idx=0; idx<myArray.length; idx++) { //start idx @ 0; end idx at length-1; //increment idx every time the loop is processed.sum += myArray[idx]; // add the idx'th element of myArray to the sum}```

There are a number of things going on in the above `for` loop:

• Before the loop starts , the index `idx` is being declared and initialized to zero. `idx` is visible only within the `for` loop body (between the curly braces).
• At the begnning of each loop iteration , the index `idx` is being tested in a "termination condition", in this case, `idx` is compared to the length of the list. If the termination condition evaluates to `false` , the loop will immediately terminate.
• During each loop iteration , the value of the `idx` 's element in the array is being added to the running sum.
• After each loop iteration , the index `idx` is being incremented.

One can traverse an array in any direction one wishes:

```// Sum the number of elements in an array of ints, myArray. int sum = 0; // initialize the sumfor(int idx=myArray.length-1; 0<=idx; idx--) { //start idx @ length-1; end idx at 0; //decrement idx every time the loop is processed.sum += myArray[idx]; // add the idx'th element of myArray to the sum}```

The above loop sums the list just as well as the first example, but it does it from back to front. Note however, that we had to be a little careful on how we initialized the index and how we set up the termination condition.

Here's a little more complicated loop:

```// Find the index of the smallest element in an array of ints, myArray. int minIdx = 0; // initialize the index. Must be declared outside the loop.if(0==myArray.length) throw new NoSuchElementException("Empty array!"); //No good if array //is empty!else { for(minIdx = 0, int j = 1; j<myArray.length; j++) { //start minIdx @ 0, start index @ 1; //end index at length-1; increment// index every time the loop is //processed.if(myArray[minIdx]>myArray[j])minIdx = j; // found new minimum }}```

the art of managing the production, distribution and consumption.
what is economics
okk
damfash
marginal utility is the additional satisfaction one derives from consuming additional unit of a good or service.
Fred
It's the allocation of scarce resources.
Fred
Dishan
marginal utility is the additional satisfaction one derives from consuming additional unit of a good or service.
Fred
I know the definition, but I don't understand its meaning.
Dishan
what is the must definition of economic please?
Nurudeen
demand lfs
Alpha
Economics is derived from the word Oikonomia which means management of household things. Thus, Economics is a study of household things with the constrains of allocating scare resources.
Dishan
what is Open Market Operation
dominating middlemen men activities circumstances
what Equilibrium price
what is gap
mirwais
who is good with the indifference curve
Dexter
What is diseconomic
what are the types of goods
WARIDI
how can price determination be the central problem of micro economics
marginal cost formula
you should differentiate the total cost function in order to get marginal cost function then you can get marginal cost from it
boniphace
Foday
ok
Foday
how can price determination be the central problem if micro economics
simon
formula of cross elasticity of demand
what is ceteris paribus
what is ceteris parabus
Priyanka
Ceteris paribus - Literally, "other things being equal"; usually used in economics to indicate that all variables except the ones specified are assumed not to change.
Abdullah
What is broker
scor
land is natural resources that is made by nature
scor
What is broker
scor
what is land
kafui
What is broker
scor
land is natural resources that is made by nature
scor
whats poppina nigga turn it up for a minute get it
what is this?
Philo
am from nigeria@ pilo
Frank
am from nigeria@ pilo
Frank
so
owusu
what is production possibility frontier
owusu
it's a summary of opportunity cost depicted on a curve.
okhiria
please help me solve this question with the aid of appropriate diagrams explain how each of the following changes will affect the market price and quantity of bread 1. A
ok let me know some of the questions please.
Effah
ok am not wit some if den nw buh by tommorow I shall get Dem
Hi guys can I get Adam Smith's WEALTH OF NATIONS fo sale?
Ukpen
hello I'm Babaisa alhaji Mustapha. I'm studying Economics in the university of Maiduguri
Babaisa
okay
Humaira
my name is faisal Yahaya. i studied economics at Kaduna state university before proceeding to West African union university benin republic for masters
Faisal
Mannan
Wat d meaning of management
disaster management cycle
cooperate social responsibility
igwe
Fedric Wilson Taylor also define management as the act of knowing what to do and seeing that it is done in the best and cheapest way
OLANIYI
in a comparison of the stages of meiosis to the stage of mitosis, which stages are unique to meiosis and which stages have the same event in botg meiosis and mitosis
Researchers demonstrated that the hippocampus functions in memory processing by creating lesions in the hippocampi of rats, which resulted in ________.
The formulation of new memories is sometimes called ________, and the process of bringing up old memories is called ________.
Got questions? Join the online conversation and get instant answers!