8 - Lec - Arrays in Java Part-1
8 - Lec - Arrays in Java Part-1
• Examples:
80-element array with base type char:
char[] symbol = new char[80];
score[3] = keyboard.nextInt();
System.out.println("You entered" + score[3]);
Arrays
• An array is an ordered list of values
The entire array Each value has a numeric index
has a single name
0 1 2 3 4 5 6 7 8 9
scores 79 87 94 82 67 98 87 81 74 91
7-6
Arrays
• A particular value in an array is referenced using the array name
followed by the index in brackets
• For example, the expression
scores[2]
refers to the value 94 (the 3rd value in the array)
• That expression represents a place to store a single integer and can be
used wherever an integer variable can be used
7-7
Arrays • For example, an array element can be assigned a value,
printed, or used in a calculation:
scores[2] = 89;
scores[first] = scores[first] + 2;
mean = (scores[0] + scores[1])/2;
System.out.println ("Top = " + scores[5]);
7-8
• The values held in an array are called array elements
Arrays
• An array stores multiple values of the same type – the
element type
• The element type can be a primitive type or an object
reference
• Therefore, we can create an array of integers, an array
of characters, an array of String objects, an array of
Coin objects, etc.
• In Java, the array itself is an object that must be
instantiated
7-9
Arrays • Another way to depict the scores array:
scores 79
87
94
82
67
98
87
81
74
91
7-10
Declaring Arrays
• The scores array could be declared as follows:
int[] scores = new int[10];
• The type of the variable scores is int[] (an array of integers)
• Note that the array type does not specify its size, but each object of
that type has a specific size
• The reference variable scores is set to a new array object that can
hold 10 integers
• An array is an object, therefore all the values are initialized to
default ones (here 0)
7-11
Declaring Arrays
• Some other examples of array declarations:
7-12
Using Arrays
• for loop can be used when processing array elements
7-13
final int LIMIT = 15, MULTIPLE = 10;
7-15
Bounds Checking
• For example, if the array codes can hold 100 values, it can be indexed using only
the numbers 0 to 99
• If the value of count is 100, then the following reference will cause an exception
to be thrown:
System.out.println (codes[count]);
• It’s common to introduce off-by-one errors when using arrays
problem
7-16
Bounds Checking
• Each array object has a public constant called length that stores the size of the
array
• It is referenced using the array name. e.g.
scores.length
• Note that length holds the number of elements, not the largest index
7-17
ReverseOrder
Scanner scan = new Scanner (System.in);
7-18
Char type
String st = "abcd";
for(int i =0; i < st.length (); i++ ) {
char c = st.charAt (i);
System.out.print(c); Output:
System.out.print(" ");
a 97 0
System.out.print((int) c);
b 98 1
System.out.print(" ");
c 99 2
System.out.println(c - 'a');
}
7-19
Letter Count
// program to count upper case, lower case, and other characters
final int NUMCHARS = 26; // number of alphabets
7-21
Letter Count (contd.)
// Print the results
System.out.println ();
for (int letter=0; letter < upper.length; letter++)
{
System.out.print ( (char) (letter + 'A') );
System.out.print (": " + upper[letter]);
System.out.print ("\t\t" + (char) (letter + 'a') );
System.out.println (": " + lower[letter]);
}
System.out.println ();
System.out.println ("Non-alphabetic characters: " + other);
7-22