Java Reference
In-Depth Information
char [] createArray()
21
public static
{
22
// Declare an array of characters and create it
23
char [] chars = new char [ 100 ];
24
25 // Create lowercase letters randomly and assign
26 // them to the array
27 for ( int i = 0 ; i < chars.length; i++)
28 chars[i] = RandomCharacter.getRandomLowerCaseLetter();
29
30
// Return the array
31
return chars;
32 }
33
34
/** Display the array of characters */
void displayArray( char [] chars)
35
{
36 // Display the characters in the array 20 on each line
37 for ( int i = 0 ; i < chars.length; i++) {
38 if ((i + 1 ) % 20 == 0 )
39 System.out.println(chars[i]);
40 else
41 System.out.print(chars[i] + " " );
42 }
43 }
44
45
public static
/** Count the occurrences of each letter */
46
public static
int [] countLetters( char [] chars)
{
47
// Declare and create an array of 26 int
48
int [] counts = new int [ 26 ];
49
50 // For each lowercase letter in the array, count it
51 for ( int i = 0 ; i < chars.length; i++)
52 counts[chars[i] - 'a' ]++;
53
54
increase count
return counts;
55 }
56
57
/** Display counts */
58
public static
void displayCounts( int [] counts)
{
59 for ( int i = 0 ; i < counts.length; i++) {
60 if ((i + 1 ) % 10 == 0 )
61 System.out.println(counts[i] + " " + ( char )(i + 'a' ));
62 else
63 System.out.print(counts[i] + " " + ( char )(i + 'a' ) + " " );
64 }
65 }
66 }
The lowercase letters are:
e y l s r i b k j v j h a b z n w b t v
s c c k r d w a m p w v u n q a m p l o
a z g d e g f i n d x m z o u l o z j v
h w i w n t g x w c d o t x h y v z y z
q e a m f w p g u q t r e n n w f c r f
The occurrences of each letter are:
5 a 3 b 4 c 4 d 4 e 4 f 4 g 3 h 3 i 3 j
2 k 3 l 4 m 6 n 4 o 3 p 3 q 4 r 2 s 4 t
3 u 5 v 8 w 3 x 3 y 6 z
Search WWH ::




Custom Search