<< Chapter < Page Chapter >> Page >

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 }}

Questions & Answers

Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
Renato
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
?
Kyle
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
Adin
why?
Adin
what school?
Kyle
biomolecules are e building blocks of every organics and inorganic materials.
Joe
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
research.net
kanaga
sciencedirect big data base
Ernesto
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
Bharti
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
Daniel
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
Maciej
characteristics of micro business
Abigail
for teaching engĺish at school how nano technology help us
Anassong
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
NANO
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
s.
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
Tarell
what is the actual application of fullerenes nowadays?
Damian
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
Tarell
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
Virgil
is Bucky paper clear?
CYNTHIA
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
NANO
so some one know about replacing silicon atom with phosphorous in semiconductors device?
s. Reply
Yeah, it is a pain to say the least. You basically have to heat the substarte up to around 1000 degrees celcius then pass phosphene gas over top of it, which is explosive and toxic by the way, under very low pressure.
Harper
Do you know which machine is used to that process?
s.
how to fabricate graphene ink ?
SUYASH Reply
for screen printed electrodes ?
SUYASH
What is lattice structure?
s. Reply
of graphene you mean?
Ebrahim
or in general
Ebrahim
in general
s.
Graphene has a hexagonal structure
tahir
On having this app for quite a bit time, Haven't realised there's a chat room in it.
Cied
what is biological synthesis of nanoparticles
Sanket Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!





Source:  OpenStax, Principles of object-oriented programming. OpenStax CNX. May 10, 2013 Download for free at http://legacy.cnx.org/content/col10213/1.37
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Principles of object-oriented programming' conversation and receive update notifications?

Ask