• +1-617-874-1011 (US)
  • +44-117-230-1145 (UK)
Live Chat
Follow Us:

Java Array Homework Help

Java Array:

Array is collection same data type with sequential order. It is used to declare and access collection of variables.

Declare arrays:

First we have to specify the data type and follow by the variable name.

Syntax:

Datatype [] variablename;

Example:

Int [] numers;

Creating array:

Using new keyword we can create an array.

Syntax:

Datatype [] variablename= new datatype [ array size];

Example:

Int [] numbers= new int[10];

Example program:

public class sampleArray
 {
 public static void main(String[] args)
 {
 int[] numbers = {1, 2, 3, 4};
 // Print all the array elements
 for (int i = 0; i < numbers.length; i++)
 {
 System.out.println(numbers[i] + " ");
 }
 // Summing all elements
 int total = 0;
 for (int i = 0; i < numbers.length; i++)
 {
 total += numbers[i];
 }
 System.out.println("Total is " + total);
 // Finding the largest element
 int max = numbers[0];
 for (int i = 1; i < numbers.length; i++) {
 if (numbers[i] > max) max = numbers[i];
 }
 System.out.println("Max is " + max);
 }
}

Output:

1

2

3

4

Total is 10

Max is 4

Java Homework help