Java Reference
In-Depth Information
need to worry about this. Use the diamond whenever possible to save yourself some
typing, as well as to make your code more robust, readable, and concise.
7-6. Working with Dynamic Arrays
Problem
You need a flexible data structure that can store a variable amount of data and that al-
lows for easy insertion and deletion of data.
Solution
Consider using an ArrayList . The following example code is the StockScreen-
er class, which allows you to screen a list of stocks or a single stock based on a specif-
ic screen parameter ( P/E , Yield , and Beta ) and screen value. The class makes use
of an ArrayList for containing stock strings. An example screen might be “Tell me
which of the stocks in this list has a P/E (price-to-earnings ratio) of 15 or less.” Don't
worry if you're not familiar with these stock market terms. Whatever you do, don't use
this class to make your stock investment decisions!
// See StockScreener.java
public class StockScreener {
enum Screen { PE, YIELD, BETA };
public static boolean screen(String stock, Screen
screen, double threshold) {
double screenVal = 0;
boolean pass = false;
switch (screen) {
case PE:
screenVal = Math.random() * 25;
pass = screenVal <= threshold;
break;
case YIELD:
screenVal = Math.random() * 10;
Search WWH ::




Custom Search