/**
*
* An array is a container object that holds a fixed number of values of a single type.
* The length of an array is established when the array is created. After creation, its length is fixed.
* Each item in an array is called an element, and each element is accessed by its numerical index.
*
* The following program creates can array of integers, puts some values in it, and prints each value to standard output.
*
* integer: a whole number, such as 3 or 4, but not 3.5
*
* You can declare an array with the following line of code:
* int[] anArray; // declares an array of integers
* Like declarations for variables of other types, an array declaration has two components:
* 1. the array's type
* 2. the array's name.
* An array's type is written as type[], where type is the data type of the contained elements;
* the square brackets are special symbols indicating that this variable holds an array.
* The size of the array is not part of its type(which is why the brackets are empty).
*
* As with variables of other types, the declaration does not actually create an array --
* it simply tells the compiler that this variable will hold an array of the specified type.
*
* One way to create an array is with the new operator. The next statement program allocate an array with enough memory for ten integer elements and assigns the array to the anArray variable.
* anArray = new int[10]; // create an array of integers.
*
* Alternatively, you can use the shortcut syntax to create and initialize an array:
* int[] anArray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
*
*/
public class Learning {
public static void main(String[] args) {
// declare an array of integers
/**
* the declaration does not actually create an array, it simply tells the compiler that this variable will hold an array of the specified type.
*/
int[] intArray = new int[10];
// create an array of integers,
/**
* the keyword new allocates the array with enough memory for ten integer elements and assigns the array to the intArray variable.
* if this statement were missing, the compiler would print an error like the following, and compilation would fail:
* Variable intArray may not have been initialized.
*/
intArray = new int[10];
// assign a value to each array element and print
/**
* you can use the built-in length property to determine the size of an array.
*/
for (int i = 0; i < intArray.length; i++) {
intArray[i] = i; // assigns values to each element of the array.
System.out.print(intArray[i]); // each array element is accessed by its numerical index.
if (i == intArray.length - 1)
break;
System.out.print(" * ");
}
System.out.println("/n");
/**
* In the Java programming language, a multidimensional array is simply an array whose components are themselves arrays.
* This is unlike arrays in C. A consequence of this is that the rows are allowed to vary in length.
*/
// length of subarrays unspecified
int[][] aMatrix = new int[4][];
// populate matrix
for (int i = 0; i < aMatrix.length; i++) {
// create subarray
aMatrix[i] = new int[5];
for (int j = 0; j < aMatrix[i].length; j++) {
aMatrix[i][j] = i + j;
}
}
// print matrix
for (int i = 0; i < aMatrix.length; i++) {
for (int j = 0; j < aMatrix[i].length; j++) {
System.out.print(aMatrix[i][j] + " ");
}
System.out.println();
}
System.out.println("");
/**
* The System class has an arraycopy method that you can use to efficiently copy data from one array into another.
public static void arraycopy(
Object src, // specify the array to copy from
int srcPos,// specify the starting position in the source array
Object dest, // specify the arry to copy to
int destPos,// specify the starting position in the destination array
int length// specify the number of array elements to copy
)
*/
// The following program declares an array of char elements, spelling the word "arraycopy". It uses arraycopy to copy
// a subsequence of array components into a second array.
char[] copyFrom = {'a', 'r', 'r', 'a', 'y', 'c', 'o', 'p', 'y'};
char[] copyTo = new char[4];
System.arraycopy(copyFrom, 5, copyTo, 0, copyTo.length);
System.out.println(new String(copyTo));
}
}