Programming 1A: (PROG5121)
Programming 1A: (PROG5121)
Programming 1A: (PROG5121)
(PROG5121)
INTRODUCTION TO ARRAYS
• An array is a named list of data items that all have the same data type.
• Each data item is an element of the array.
• To declare an array of double values to hold sales figures for sales people you can write
the following:
double[] salesFigures;
• To create an array of integers to hold student ID numbers, you can write the following:
int[] idNums;
• You can declare and create an array in one statement with the following:
• A subscript is an integer contained within square brackets that specifies one of an array’s
elements.
• In Java, any array’s elements are numbered beginning with 0, so you can legally use any
subscript from 0 through 19 when working with an array that has 20 elements.
INITIALIZING AN ARRAY
scoreArray[sub] += INCREASE;
ARRAYS OF OBJECTS
STARTING_SALARY);
System.out.println (emps[x].getEmpNum() +
""+
emps[x].getSalary());
You can also use the enhanced for loop
alternative to iterate through an array of objects:
for (Employee worker : emps)
System.out.println(worker.getEmpNum() +
""+
worker.getSalary());
SEARCHING AN ARRAY
Write a Java program that reverses the order of the elements in an array. Call your class
containing the main() method ReverseArray.
Your code should produce the following output: