> Online tutorial : arrays a simple example
Showing posts with label arrays a simple example. Show all posts
Showing posts with label arrays a simple example. Show all posts

arrays a simple example

Arrays: A Simple Example
The example:
class Gauss
{
public static void main(String[] args)
{
int[] ia = new int[101];
for (int i = 0; i < ia.length; i++)
ia[i] = i;
int sum = 0;
for (int e : ia)
sum += e;
System.out.println(sum);
}
}

produces the output:


5050

Explanation:


declares a variable is a that has type array of int, that is, int[]. The variable ia is
initialized to reference a newly created array object, created by an array creation
expression . The array creation expression specifies that the array should
have 101 components. The length of the array is available using the field length,
as shown.
The example program fills the array with the integers from 0 to 100, sums
these integers, and prints the result.