Java Reference
In-Depth Information
Moral of this story: don't do this. As you can see, shadowed variable names
can be very confusing. Give your variables unique, memorable names.
Try This Yourself
Since the cake tower doesn't quite work as expected, let's fix it!
Change code/CakeTower/src/caketower/ to use just the one local variable
cakeHeight , not the class-level variable, and pass it in to makeCakes .
Now that you know where variables live and can be used, let's look at a couple
of different ways you can use piles of data, using Java data collections.
Use a Java Array
While variables with individual values are useful and common, sometimes
you need more than that. You need to keep track of all the players in the
system, or one player's inventory items, or a to-do list, or a grocery list, or a
homework list.
Java has you covered. There are several different ways to keep and access
piles of data. We're going to focus on a few: the simple Array , the classier
ArrayList , and the remarkably handy if somewhat alien HashMap (covered in the
next chapter). First up, the Array .
There are actually a few different Array -like collections in Java, including Array ,
Vector , LinkedList , and ArrayList types. They each work differently on the inside,
are stored slightly differently, and perform differently with large or small data
sets, but the idea is the same for all of them.
Search WWH ::

Custom Search