Developing Java Beans // get method for Stocks
Developing Java Beans // get method for Stocks property arraypublic synchronized String[] getStocks() { // we don’t currently deal with the case where the watch list // is empty // allocate an array of strings for the stock names String[] s = new String[getStockCount()]; // copy the elements of the stocks Vector into the string array, // and then return the array stocks.copyInto(s); return s; } // set method for Stocks property arraypublic synchronized void setStocks(String[] s) { // the existing list of stocks is removed in favor of the // new set of stocks // set the size of the stocks vector to match the length // of the new array stocks.setSize(s.length); // copy the values into the stocks vector for (int i = 0; i < s.length; i++) { // use the single stock set method try { setStocks(i, s[i]); } catch (ArrayIndexOutOfBoundsException e) { } } } // get method for single element of Stocks propertypublic synchronized String getStocks(int index) throws ArrayIndexOutOfBoundsException { // make sure the index is in boundsif (index < 0 || index >= getStockCount()) { throw new ArrayIndexOutOfBoundsException(); } // get the stock and return it String s = (String)stocks.elementAt(index); return s; } // set an individual element of the Stocks property arraypublic synchronized void setStocks(int index, String stock) throws ArrayIndexOutOfBoundsException { // make sure the index is in boundsif (index < 0 || index >= getStockCount()) { throw new ArrayIndexOutOfBoundsException(); } page 54