Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> Java >> Snippet

10 Arrays


- Zero - based
- Each element contains same type of data

Create like a variable, but use square brackets

String[] MyStringArray ;
int [] MyIntegerArray ;

Also can put [] after variable :

String MyStringArray[] ;
int MyIntegerArray[] ;

To specify number of elements, use new statement :

int MyArray[] = new int [100]

Can set value at time of creation :

String MyArray[] = { "a" , "b" , "c" }

MyArray[1] + MyArray[2]        // this gives "ab"

Array is an object.  It has properties and methods :
.length       //number of elements in the array (not the maximum element number)

Two dimensional array :

int MyArray [] [] = new int [50[ [50]

There is no way to modify the size of an array after it has been created