Page 340 - AP Computer Science A, 7th edition
P. 340
To call the swap method:
int[] list = {1, 2, 3, 4};
swap(list, 0, 3);
System.out.print(“The changed list is: “); for (int num : list)
System.out.print(num + “ ”);
The output shows that the program worked as intended:
The changed list is: 4 2 3 1
Example 5
/∗ ∗ @return array containing NUM_ELEMENTS integers read from the keyboard
∗ Precondition: Array undefined.
∗ Postcondition: Array contains NUM_ELEMENTS integers read from
∗ the keyboard.
∗/
public int[] getIntegers() {
int[] arr = new int[NUM_ELEMENTS]; for (int i = 0; i < arr.length; i++) {
System.out.println(“Enter integer: “);
arr[i] = IO.readInt();
return arr; }
To call this method:
int[] list = getIntegers();
Array Variables in a Class
//read user input
}
Consider a simple Deck class in which a deck of cards is represented by the integers 0 to 51.