Saturday, February 18, 2006

Java - Creating Arrays  [Digg.com This!]

Arrays can be created of any size, but once you have created it, the size cannot change.

An integer array of 20 items can be created as follows:

int[] myintarray = new int[20];

This created items from 0 to 19.

~~~

To access an item as position 7 we can do the following:

myintarray[7]

Or to print it out do:

System.out.println("Number at 7 is: " + myintarray[7]);

To access the 7th number we do the following:

myintarray[6]

~~~

Arrays can be created of any type, either primitive (int, double, String, etc) or classes (Date, Object, etc).

Examples:

double[] doublearray = new double[12];

String[] stringarray = new String[17];

Point[] pointarray = new Point[18000];

Date[] mydates = new Date[100];


They always have the same pattern:

Type[] VarName = new Type[Size];

0 Comments:

Post a Comment

<< Home