<< Chapter < Page Chapter >> Page >
These learning objects explain arrays in the Java program language. Arrays are a data structure for efficiently accessing a large number of components of the same type.

Learning objects for arrays

Concept An array is a sequence of elements of the same type; the type of the elements can be a primitive types such as int , or a predefined or user-defined class type. To access an element of an array, an index is given; this may be any expression of type int , including an integer literal or a variable.

The source code of these learning objects can be found in array.zip .

LO Topic Java Files (.java) Prerequisites
"Array objects" Array objects Array01A, B
"Array initializers" Array initializers Array02 1
"Passing arrays as parameters" Passing arrays as parameters Array03 2
"Returning an array from a method" Returning an array from a method Array04 2
"Array assignment can create garbage" Array assignment can create garbage Array05 4
"Two-dimensional arrays" Two-dimensional arrays Array06 3
"Arrays of arrays" Arrays of arrays Array07 6
"Ragged Arrays" Ragged arrays Array08 6
"Arrays of objects" Arrays of objects Array09 3

The example used in LO 1 through LO 4 is to fill an array with a sequence of fibonacci numbers (0,1,1,2,3,5,8). The programs forLO 5 through LO 8 concern matrices. The program for LO 9 is explained there.

Array objects

Concept An array is created in three steps: first a variable of an array type is declared; then the array is allocated; finally, theelements of the array are given values. The syntax for accessing an array a is a[i] , and the field a.length gives the length of the array, so that if we modify the program by changing the sizeof the array the rest of the program need not change.

Program: Array01A.java

// Learning Object Array01A //    array objectspublic class Array01A {     public static void main(/*String[] args*/) {         int[] fib;         fib = new int[7];         fib[0] = 0;         fib[1] = 1;         for (int i = 2; i < fib.length; i++)             fib[i] = fib[i-1] + fib[i-2];     }}

The program creates an array in the three steps described above.

  • Initially, the variable fib of type integer array (denoted int[] ) is allocated and contains the null value.
  • new fib[7] creates an array object with its seven fields having the default integer value zero; then the reference to the object is returned and stored in the variable fib .
  • The length field of the array is displayed above the cells for the elements.
  • A for loop is used to assign values to each element of the array.
  • The thin white lines show the constants and expressions that are used as indices into the array.
  • Automatic dereferencing : Although expressions like fib[i-2] seem to indicate that fib is being indexed, fib contains a reference to an array; an implicit operation of dereferencing is carried out to obtain thearray itself from the reference and the index [i-2] is then applied to that array.

Concept It is possible to combine the first two steps in creating an array: declaring the array field and allocating the array object.

Program: Array01B.java

Questions & Answers

what is microbiology
Agebe Reply
What is a cell
Odelana Reply
what is cell
Mohammed
how does Neisseria cause meningitis
Nyibol Reply
what is microbiologist
Muhammad Reply
what is errata
Muhammad
is the branch of biology that deals with the study of microorganisms.
Ntefuni Reply
What is microbiology
Mercy Reply
studies of microbes
Louisiaste
when we takee the specimen which lumbar,spin,
Ziyad Reply
How bacteria create energy to survive?
Muhamad Reply
Bacteria doesn't produce energy they are dependent upon their substrate in case of lack of nutrients they are able to make spores which helps them to sustain in harsh environments
_Adnan
But not all bacteria make spores, l mean Eukaryotic cells have Mitochondria which acts as powerhouse for them, since bacteria don't have it, what is the substitution for it?
Muhamad
they make spores
Louisiaste
what is sporadic nd endemic, epidemic
Aminu Reply
the significance of food webs for disease transmission
Abreham
food webs brings about an infection as an individual depends on number of diseased foods or carriers dully.
Mark
explain assimilatory nitrate reduction
Esinniobiwa Reply
Assimilatory nitrate reduction is a process that occurs in some microorganisms, such as bacteria and archaea, in which nitrate (NO3-) is reduced to nitrite (NO2-), and then further reduced to ammonia (NH3).
Elkana
This process is called assimilatory nitrate reduction because the nitrogen that is produced is incorporated in the cells of microorganisms where it can be used in the synthesis of amino acids and other nitrogen products
Elkana
Examples of thermophilic organisms
Shu Reply
Give Examples of thermophilic organisms
Shu
advantages of normal Flora to the host
Micheal Reply
Prevent foreign microbes to the host
Abubakar
they provide healthier benefits to their hosts
ayesha
They are friends to host only when Host immune system is strong and become enemies when the host immune system is weakened . very bad relationship!
Mark
what is cell
faisal Reply
cell is the smallest unit of life
Fauziya
cell is the smallest unit of life
Akanni
ok
Innocent
cell is the structural and functional unit of life
Hasan
is the fundamental units of Life
Musa
what are emergency diseases
Micheal Reply
There are nothing like emergency disease but there are some common medical emergency which can occur simultaneously like Bleeding,heart attack,Breathing difficulties,severe pain heart stock.Hope you will get my point .Have a nice day ❣️
_Adnan
define infection ,prevention and control
Innocent
I think infection prevention and control is the avoidance of all things we do that gives out break of infections and promotion of health practices that promote life
Lubega
Heyy Lubega hussein where are u from?
_Adnan
en français
Adama
which site have a normal flora
ESTHER Reply
Many sites of the body have it Skin Nasal cavity Oral cavity Gastro intestinal tract
Safaa
skin
Asiina
skin,Oral,Nasal,GIt
Sadik
How can Commensal can Bacteria change into pathogen?
Sadik
How can Commensal Bacteria change into pathogen?
Sadik
all
Tesfaye
by fussion
Asiina
what are the advantages of normal Flora to the host
Micheal
what are the ways of control and prevention of nosocomial infection in the hospital
Micheal
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Learning objects for java (with jeliot). OpenStax CNX. Dec 28, 2009 Download for free at http://cnx.org/content/col10915/1.2
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Learning objects for java (with jeliot)' conversation and receive update notifications?

Ask