by Ben Davidson
int[] testScores;
double[] dailyTemps;
float[] accountBalances;
new keyword to create the
array.double[] empSalaries; // array declaration
empSalaries = new double[6]; // array creation
int dataLength = 15;
int[] data; // array declaration
data = new int[dataLength]; // array creation
double[] empSalaries = new double[6];
int dataLength = 15;
int[] data = new int[dataLength];
String[] employees = { "Bob", "Larry", "Ben", "Louise" };
double[] sizes = { 13.8, 22.1, 18.6, 18.5, 17.4 };
String[] array = { "Ben", "Emily", "Albert", "Lydia" };
int len = array.length; // len will hold 4
double[] sizes = { 13.8, 22.1, 18.6, 18.5, 17.4 };
// 13.8 is stored at index 0 (the first index).
// 17.4 is stored at index 4 (the last index).
sizes[2] = 15.9; // changes the value at index 2 from 18.6 to 15.9
double result = sizes[3] + sizes[1]; // adds the values at indices 3 and 1.
double[] sizes = { 13.8, 22.1, 18.6, 18.5, 17.4 };
sizes[-1] = 14.1; // Causes an ArrayIndexOutOfBoundsException
sizes[5] = 15.0; // Causes an ArrayIndexOutOfBoundsException
// Printing the contents of an array:
String[] empNames = { "Heath", "Garth", "Chandra", "Susan" };
for (int i = 0; i < empNames.length; i++) {
System.out.println(empNames[i]);
}
// Finding the largest element in an array:
double[] hourlyWage = { 7.75, 8.00, 9.00, 8.50 };
double max = hourlyWage[0];
for (int j = 1; j < hourlyWage.length; j++) {
if (hourlyWage[j] > max) {
max = hourlyWage[j];
}
}
char[][] ticTacToe = { {'x', 'o', 'x'}, {'o', 'x', 'o'}, {'o', 'o', 'x'} };
| Code | Output | |
|---|---|---|
|
|