Java Arena
Step 3 of 6

Arrays of other types

The elements can be of any type. The array's type is the element type followed by []: double[], String[], boolean[]. Two arrays of the same length can work as a pair, where the same index in each describes the same thing:

String[] dishes = new String[3];
double[] prices = new double[3];
dishes[0] = "Soup";
prices[0] = 4.5;
dishes[1] = "Pasta";
prices[1] = 9.0;
dishes[2] = "Pie";
prices[2] = 3.75;
for (int i = 0; i < dishes.length; i++) {
    System.out.println(dishes[i] + ": " + prices[i]);
}
Output
Soup: 4.5
Pasta: 9.0
Pie: 3.75

All the elements of one array have the same type, and a value of another type doesn't fit:

public class Main {
    public static void main(String[] args) {
        int[] ages = new int[2];
        ages[0] = "twelve";
    }
}
What javac prints
Main.java:4: error: incompatible types: String cannot be converted to int
        ages[0] = "twelve";
                  ^
1 error

One difference from lists: an array of whole numbers is written with the basic type, int[]. A list needs ArrayList<Integer>, but an array has no such rule.

Reading values into an array

When the program is told how many values are coming, it can make an array of exactly that size. Here the first line of input says how many names follow:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int count = Integer.valueOf(scanner.nextLine());
        String[] guests = new String[count];
        for (int i = 0; i < guests.length; i++) {
            guests[i] = scanner.nextLine();
        }
        System.out.println("Guest list:");
        for (int i = 0; i < guests.length; i++) {
            System.out.println((i + 1) + ". " + guests[i]);
        }
    }
}
Input
3
Amira
Bruno
Chen
Output
Guest list:
1. Amira
2. Bruno
3. Chen

The size comes from the input, but once the array exists, it can't grow. When a program can't know the count before it starts reading (for example, when it reads until an empty line), a list is the better choice.

Swapping two elements

To swap two elements, keep one of them aside first. Otherwise its value is gone before it's copied:

String[] podium = new String[3];
podium[0] = "Kim";
podium[1] = "Lea";
podium[2] = "Max";
String kept = podium[0];
podium[0] = podium[1];
podium[1] = kept;
for (String name : podium) {
    System.out.println(name);
}
Output
Lea
Kim
Max

Without kept, the line podium[0] = podium[1]; would write over Kim, and both places would end up holding Lea.

Your task

Challenge 1 of 3

Fill in the blanks. The program reads how many prices follow, then the prices, and prints their total. For the input 3, 2.5, 4.0 and 1.25 it prints:

Total: 7.75

Challenge 2

A small theater numbers its seats from 0. Read the number of seats, then the numbers of the booked seats, one per line, until -1. Keep track of them in a boolean[] where true means booked. Then print every free seat, in order. For the input 5, 1, 3 and -1 the program prints:

Seat 0 is free
Seat 2 is free
Seat 4 is free

Challenge 3

The program reads a count and that many whole numbers into an array, and prints the array with a for-each loop. Add code in between that reverses the array: swap the first element with the last, the second with the second to last, and so on. For the input 5, 1, 2, 3, 4 and 5 the program prints:

5
4
3
2
1