declare nums = table of binary-long (1 2 3)
declare names as string occurs 5
*> Can also do:
declare names-again as string occurs any
set size of names to 5
set names(1) to "David" *> first element indexed as 1
*> ...but can also use zero based subscripting:
set names[0] to "David" *> first element indexed as 0
*>set names(6) to "Bobby" *> throws System.IndexOutOfRangeException
*> COBOL does not have direct resizing syntax but achieves similar
*> results using 'reference modification' syntax:
declare names2 as string occurs 7
set names2[0:size of names] to names
*> Resizing to a smaller size is even simpler:
set names2 to names[0:3]
declare twoD as float-short occurs any, any.
declare rows as binary-long = 3
declare cols as binary-long = 10
set size of twoD to rows, cols
declare jagged = table of (table of binary-long(1 2)
table of binary-long(3 4 5)
table of binary-long(6 7 8 9))
*> Can also do:
declare jagged2 as binary-long occurs any, occurs any
set size of jagged2 to 3
set size of jagged2(1) to 5
set jagged2(1 5) to 5
|
public class Arrays
{
public static void main(String args[])
{
int nums[] = { 1, 2, 3 };
String names[] = new String[5];
names[0] = "David";
// names[5] = "Bobby"; // throws ArrayIndexOutOfBoundsException
// Can't resize arrays in Java
String names2[];
// Copy elements from an array
names2 = java.util.Arrays.copyOfRange(names, 0, 3);
float twoD[][];
int rows = 3;
int cols = 10;
twoD = new float[rows][cols];
int[][] jagged = { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } };
int[][] jagged2 = new int[3][];
jagged[0] = new int[5];
jagged[0][4] = 5;
}
}
|