When you find yourself wanting to create "more variables" of the same type, you usually want a list of some kind. There are two fundamental kinds of "lists" in Java: Arrays, and List
s.
An array:
String[] people = new String[10]; // That gives you room for 10
people[0] = "female";
people[1] = "male";
// ...
int n = 1;
System.out.println("Person " + n + " is " + people[n]);
A List
:
List<String> people = new LinkedList<String>(); // Expandable
people.add("female");
people.add("male");
// ...
int n = 1;
System.out.println("Person " + n + " is " + people.get(n));
// Note difference -------------------------------^^^^^^^
Using an array is great when you know in advance how many there will be. Using a list is great when you don't know how many there will be.
Note on lists: There's an interface, List
, and then there are multiple different concrete implementations of it with different runtime performance characteristics (LinkedList
, ArrayList
, and so on). These are in java.util
.