Java Reference
In-Depth Information
LISTING 8.4
//********************************************************************
// Primes.java Author: Lewis/Loftus
//
// Demonstrates the use of an initializer list for an array.
//********************************************************************
public class Primes
{
//-----------------------------------------------------------------
// Stores some prime numbers in an array and prints them.
//-----------------------------------------------------------------
public static void main (String[] args)
{
int [] primeNums = {2, 3, 5, 7, 11, 13, 17, 19};
System.out.println ("Array length: " + primeNums.length);
System.out.println ("The first few prime numbers are:");
for ( int prime : primeNums)
System.out.print (prime + " ");
}
}
OUTPUT
Array length: 8
The first few prime numbers are:
2 3 5 7 11 13 17 19
SELF-REVIEW QUESTIONS (see answers in Appendix N)
SR 8.4
What is an array's element type?
SR 8.5
Describe the process of creating an array. When is memory allocated
for the array?
SR 8.6
Write an array declaration to represent the ages of all 100 children
attending a summer camp.
SR 8.7
Write an array declaration to represent the counts of how many times
each face appeared when a standard six-sided die is rolled.
 
Search WWH ::




Custom Search