<< 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 mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
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