1Answer
Array in Java:
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. You have seen an example of arrays already, in the main method of the "Hello World!" application. This section discusses arrays in greater detail.
// declares an array of integers
int[] myArray;
// allocates memory for 10 integers
myArray = new int[10];
// initialize first element
myArray[0] = 100;
and so on
//Print array value
System.out.println("Element at index 0: "+ myArray[0]);
// declares an array of integers
int[] myArray;
Similarly, you can declare arrays of other types:
byte[] myArrayOfBytes;
short[] myArrayOfShorts;
long[] myArrayOfLongs;
float[] myArrayOfFloats;
double[] myArrayOfDoubles;
boolean[] myArrayOfBooleans;
char[] myArrayOfChars;
String[] myArrayOfStrings;
// create an array of integers
myArray = new int[10];
myArray[0] = 100; // initialize first element
Alternatively, you can use the shortcut syntax to create and initialize an array:
int[] myArray = {
100, 200, 300,
400, 500, 600,
700, 800, 900, 1000
};
multidimensional array
String[][] names = {
{"Mr. ", "Mrs. ", "Ms. "},
{"Smith", "Jones"}
- answered 8 years ago
- Community wiki
Your Answer