All About Arrays

Declaring an array

An array variable is like other variables -- you must declare it, which means you must declare the type of elements that are in an array. All elements must be the same type. Write the element type name, then "[]", then the name of the array variable. The declaration only allocates space associated with a variable name for a reference to an array, but doesn't create the actual array object.

String[] args;   // args is an array of Strings
int[] scores;     // scores is an array of ints
JButton[] bs;   // bs is an array of Jbuttons


Unlike some languages, never put the size of the array in the declaration because an array declaration only tells Java that the variable is an array and the element type. Allocating an array object Create an array using new. This example creates an array of 100 int elements, from a[0] to a[99].

int[] a;           // Declares a to be an array of ints
a = new int[100];  // Allocates an array of 100 ints


Length of an array

Each array has a constant (final) instance variable that has its length. You can find out how many elements an array can hold by writing the array name followed by .length. In the previous example, a.length would be 100. Remember that this is the number of elements in the array, one more than the maximum subscript.

Initial array element values -- zero/null/false

When an array is allocated (with new), all elements are set to an initial value. The initial value is 0 if the element type is numeric (int, float, ...), false for boolean, and null for all object types.

Array variables are references to arrays

When you declare an array variable, Java reserves only enuf memory for a reference (Java's name for an address or pointer) to an array object. When an array object is created with new, a reference is returned, and that reference can then be assigned to a variable. Similarly, you can assign one array variable to another, and it is only the reference that is copied. For example,

int[] a = new int[]{100,99.98};
int[] b;
// "a" points to an array, and "b" doesn't point to anything
b = a; // now b points t the same array as "a"
b[1] = 0; // also changes a[1] because a and b are the same array.
// Both a and b point to same array with value {100,0,98}


Array Initialization

When you declare an array, you can also allocate a pre-initialized array object in the same statement. In this case, do not give the array size because Java counts the number of values to determine the size.

String[] days = {"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"};


FAQ Home

 
Printer friendly version of the FAQ-JAVA-All-About-Arrays page


Sponsored links

Build IT Knowledge with Current & Trusted Content
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.
Check Out IT Certification Preparation Materials
Sign Up With SkillSoft & Get Access to Training Materials for Over 50 Professional Certifications.
SFTP components for .NET
Add complete SSH and SFTP support to your .NET framework application
Virtual File System SDK
Create your own file systems in Windows and .NET applications
PureCM Software Configuration Management
Version control and integrated issue tracking - powerful and easy to use. Get your FREE trial now!

Advertisement



Free Magazine

Free Magazines
eWeek The essential technology information source for builders of e-business.... subscribe now

Newsletter | Submit Content | About | Advertising | Awards | Contact Us | Link to us |
© 1996-2008 Community Networks Ltd All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Synchron Data - .NET development.