Extending an array multiple times - Help!

Hi everyone,
I am very new to Java (doing my first class now) and I need some help with making an array larger. I have written a program that will display a list of products created in a static array. The program needs to include an "add" button, that will prompt for new product information, and add it to the array.
I've managed to get this coded up to a point where my add button works (albeit only is adding product name at this point...will add the rest later once this works). The problem is, when I attempt to run the add button a second time, it does not grow out the length of the array. The result is that it updates the last record within the array with the new product information, rather than appending a new one. Here is the code I think is relevant (as I don't think you are allowed to entirely post school work):
// Assignment: Inventory Program Part 6
// Author:  Ryan MacDonald
// Class uses both swing and awt components, also using number format for currency, and AWT events
import javax.swing.*;
import java.awt.*;
import java.text.NumberFormat;
import java.awt.event.*;
// InventoryPart4 class
public class InventoryPart6
     // Declare array of products
     static Product_Extender Item[] = new Product_Extender[4];
     // snip
     // snip
     // Main method
     public static void main ( String args[] )
          // Create a new product object
          Item[0] = new Product_Extender( 1, "Pink Markers", "Pentel", 31, 0.61 );
          Item[1] = new Product_Extender( 2, "Blue Pens", "Bic", 57, 0.30 );
          Item[2] = new Product_Extender( 3, "Red Markers", "Zebra", 62, 0.55 );
          Item[3] = new Product_Extender( 4, "Black Pens", "Bic", 73, 0.33 );
     // snip
     // snip
          // Make add button work
               addAddButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                    Product_Extender newItem[] = new Product_Extender[Item.length + 1];
                    System.arraycopy(Item, 0, newItem, 0, Item.length);
                    newItem[newItem.length - 1] = new Product_Extender( newItem.length, productNameText.getText(), "Test New Item", 99, 0.99 );
                    Product_Extender Item[] = new Product_Extender[newItem.length];
                    System.arraycopy(newItem, 0, Item, 0, newItem.length);
                    // Update main text area with our new product information
                    mainText.setBorder(BorderFactory.createTitledBorder("Product Inventory (All Items)"));     // Create a title around the textarea with some text
                    mainText.setText( String.format("Product #" + "\t" + "Name" + "\t" + "Manufacturer" + "\t" + "Quantity" + "\t" + "Price" + "\t" + "Value" + "\t" + "Restocking Fee" + "\n") ); // Create a line of text with the product header information
                    // Loop through each item in the product array and append the information to the main text area for the product number, product name, quantity in stock, price per unit, total value of inventory, and restocking fee for this item
                    for(int i = 0; i < Item.length; i++) {
                    mainText.append(String.format(Item.getProductNumber() + "\t" + Item[i].getProductName() + "\t" + Item[i].getProductManufacturer() + "\t" + Item[i].getProductQuantity() + "\t" + NumberFormat.getCurrencyInstance().format(Item[i].getProductPrice()) + "\t" + NumberFormat.getCurrencyInstance().format(Item[i].getInventoryValue()) + "\t" + NumberFormat.getCurrencyInstance().format(Item[i].getRestockingFee()) + "\n" ) );
                    // Set array location
                    currentLoc = -1;
                    // Close window
                    addFrame.dispose();
     // snip
     // snip
     } // End method main
} // End class InventoryPart6
Basically here is my train of thought (although my logic could be ENTIRELY off, I'm very new here...).  From what I've read, there is no way to actually grow/extend an array.  The only option is to create a new one and populate it with the same data.
1.  Since I can't grow it, I make a new array called "newItem" with Item.lengh + 1 as it's size
2.  I then copy the contents of Item over to newItem
3.  Since I need Item later, I recreate it with a new size of newItem.length
4.  Copy the data from newItem over to Item
5.  Print out the data from Item back to the main text area
What I would like to do is rinse and repeat this process as many times as the user would like.  However as I said, it doesn't seem to work.  Each time it reiterates again, it sets Item.length back to 4, not having it grow each time (deduced this by adding a bunch of prints of variables to my window).
Any help would be appreciated!!!                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

nvrmore100 wrote:
I actually have a lot of other buttons that manipulate the displaying of the array in various ways. I thought about creating a much larger array, but this would break all the functionality I have for displaying next, previous, last, items in the array.Then your functionality is incorrect. You should have a variable that keeps track of how many items are stored in the array. This will always allow you to access the last element. And when this variable reaches the size of the array only then do you increase the size of the array.
I agree, I think this is exactly my issue, unfortunately I do not know where to go from here. Is there a way for me to recreate the Item array entirely, rather than having a local array to the addbutton action method?I already showed you how to fix your problem. Delete your line of code and replace it with mine.

Similar Messages

Maybe you are looking for