I'm a student studying mostly Java.
The more I learn about object oriented programming the more I find myself creating arrays of new objects:
ArrayList<kitten> sackOfKittens = new ArrayList<kitten>();
String[] kittenNames = new String[10];
//fill kittenNames with cute strings like "flufftown" and "Indie Rocker"
for(int i = 0; i < 10; i++) {
sackOfKittens.get(i) = new Kitten(kittenNames[i]);
}
This feels funny to me because now I have all these kittens that I'm clearly going to do something with and there's no direct reference to them, or no variable that I named anyway.
Do I just need to get used to the idea that flufftown
or even kitten1
is gone but sackOfKittens.get(1)
is equivalent?
Writing this out is sort of making more sense of it.
I guess a person just writes methods in the Kitten class to do all the messy and that's that.
Am I missing something?
You need to let go of the idea that you must assign a symbol to every object you create. Symbols (variable names) are defined at compilation time, but you can write programs that create an arbitrary number of objects at runtime, without you knowing what to call them in advance.
Consider public static void main(String[] args)
. How would you handle giving a variable name to every String
in args
? You could try to be smart and lay out a bunch of variables in advance:
public static void main(String[] args) {
String s1 = (args.length > 0) ? args[0] : null;
String s2 = (args.length > 1) ? args[1] : null;
String s3 = (args.length > 2) ? args[2] : null;
String s4 = (args.length > 3) ? args[3] : null;
String s5 = (args.length > 4) ? args[4] : null;
}
But what will you do if you run into more than that? What if you need to support thousands of possible command line arguments? You cannot possibly predict this in advance, of course.
In fact, this is the exact reason why main
takes a String[]
and not multiple String
s. The number of command line arguments cannot be known in advance, so an array is necessary. And the only way you have to refer to them is by their indices in the array.
In the absence of symbols, you can use many methods of lookup on elements in a data structure (such as an array, a List
, or a Map
).
Data structures such as List
s and arrays can be iterated over. Say you want to find the kitten
named "puss"
, you can go through each kitten
in the List
until your find the one you want:
kitten pussTheKitten;
for (kitten k: sackOfKittens) {
if (k.getName().equals("puss")) {
pussTheKitten = k; // found it!
}
}
Alternatively, you can utilize the powerful Map
data structure to make it faster and easier to get objects based on some unique "key" attribute:
Map<String, kitten> mapOfKittens = new HashMap<String, kitten>();
for (kitten k: sackOfKittens) {
mapOfKittens.put(k.getName(), k);
}
kitten pussTheKitten = mapOfKittens.get("puss"); // found it!
I recommend that you learn more about Map
which maps keys to their values. In this case you can use the names as the key. This way you don't have to remember that flufftown
is at offset 1
.
Map<String, Kitten> map = new HashMap<>();
map.put("flufftown", new Kitten(...));
then you can get it out by the same key
Kitten myKitten = map.get("flufftown");