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

how to create a software using Android phone
Wiseman Reply
how
basra
what is the difference between C and C++.
Yan Reply
what is software
Sami Reply
software is a instructions like programs
Shambhu
what is the difference between C and C++.
Yan
yes, how?
Hayder
what is software engineering
Ahmad
software engineering is a the branch of computer science deals with the design,development, testing and maintenance of software applications.
Hayder
who is best bw software engineering and cyber security
Ahmad
Both software engineering and cybersecurity offer exciting career prospects, but your choice ultimately depends on your interests and skills. If you enjoy problem-solving, programming, and designing software syste
Hayder
what's software processes
Ntege Reply
I haven't started reading yet. by device (hardware) or for improving design Lol? Here. Requirement, Design, Implementation, Verification, Maintenance.
Vernon
I can give you a more valid answer by 5:00 By the way gm.
Vernon
it is all about designing,developing, testing, implementing and maintaining of software systems.
Ehenew
hello assalamualaikum
Sami
My name M Sami I m 2nd year student
Sami
what is the specific IDE for flutter programs?
Mwami Reply
jegudgdtgd my Name my Name is M and I have been talking about iey my papa john's university of washington post I tagged I will be in
Mwaqas Reply
yes
usman
how disign photo
atul Reply
hlo
Navya
hi
Michael
yes
Subhan
Show the necessary steps with description in resource monitoring process (CPU,memory,disk and network)
samuel Reply
What is software engineering
Tafadzwa Reply
Software engineering is a branch of computer science directed to writing programs to develop Softwares that can drive or enable the functionality of some hardwares like phone , automobile and others
kelvin
if any requirement engineer is gathering requirements from client and after getting he/she Analyze them this process is called
Alqa Reply
The following text is encoded in base 64. Ik5ldmVyIHRydXN0IGEgY29tcHV0ZXIgeW91IGNhbid0IHRocm93IG91dCBhIHdpbmRvdyIgLSBTdGV2ZSBXb3puaWFr Decode it, and paste the decoded text here
Julian Reply
what to do you mean
Vincent
hello
ALI
how are you ?
ALI
What is the command to list the contents of a directory in Unix and Unix-like operating systems
George Reply
how can i make my own software free of cost
Faizan Reply
like how
usman
hi
Hayder
The name of the author of our software engineering book is Ian Sommerville.
Doha Reply
what is software
Sampson Reply
the set of intruction given to the computer to perform a task
Noor
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