Java Array
String[] animals ={"Dog", "Cat", "Fish", "Turtle", "Penguin"}; // Array of animals
ayaan
String[] animals ={"Dog", "Cat", "Fish", "Turtle", "Penguin"}; // Array of animals
int[] anas = new int[3];
age[0] = 1;
age[1] = 3;
age[2] = 6;
String[] array = new String[100];
String[] array = new String[] { "String1", "String2", "String3" }; // Create array
String string3 = array[2]; // Get array
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars[0]);
// Change elements in array
cars[0] = "Opel";
System.out.println(cars[0]);
// Length of array
System.out.println(cars.length);
// Loop through array
for (int i = 0; i < cars.length; i++) {
System.out.println(cars[i]);
}
// 10 Strings in the array named: arr
String[] arr = new String[10];
class Weight
{
. . .
public int average()
{
. . .
}
}
public class WeightTester
{
public static void main ( String[] args )
{
int[] values = { 98, 99, 98, 99, 100, 101, 102, 100, 104, 105,
105, 106, 105, 103, 104, 103, 105, 106, 107, 106,
105, 105, 104, 104, 103, 102, 102, 101, 100, 102};
Weight june = new Weight( values );
int avg = june.average();
System.out.println("average = " + avg );
}
}
class Weight
{
. . .
public int average()
{
. . .
}
}
public class WeightTester
{
public static void main ( String[] args )
{
int[] values = { 98, 99, 98, 99, 100, 101, 102, 100, 104, 105,
105, 106, 105, 103, 104, 103, 105, 106, 107, 106,
105, 105, 104, 104, 103, 102, 102, 101, 100, 102};
Weight june = new Weight( values );
int avg = june.average();
System.out.println("average = " + avg );
}
}