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]);
}
Soup: 4.5
Pasta: 9.0
Pie: 3.75All 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";
}
}
Main.java:4: error: incompatible types: String cannot be converted to int
ages[0] = "twelve";
^
1 errorOne 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]);
}
}
}
3
Amira
Bruno
ChenGuest list:
1. Amira
2. Bruno
3. ChenThe 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);
}
Lea
Kim
MaxWithout kept, the line podium[0] = podium[1]; would write over Kim, and both places would end up holding Lea.