如何将Java中使用ArrayList的ProductManager类改为使用Array?
Replace ArrayList with Array in ProductManager
Got it, let's break down a simple, workable way to swap out your ArrayList<Product> for a regular Product[] array in your ProductManager class. The key thing to remember is that arrays have a fixed size, so we'll need to manually track the number of actual elements (since the array's length will be its total capacity, not the current count of products).
Here's the step-by-step modified code with explanations:
Modified ProductManager Class
public class ProductManager { // Replace List<Product> with a Product array, plus a size tracker private Product[] productArray; private int currentSize; public ProductManager() { // Initialize the array with an initial capacity (e.g., 10 - adjust as needed) this.productArray = new Product[10]; this.currentSize = 0; } public int addProduct(Product p) { // First check if the array is full - if so, resize it if (currentSize == productArray.length) { // Double the array capacity (simple, common resizing strategy) Product[] newArray = new Product[productArray.length * 2]; System.arraycopy(productArray, 0, newArray, 0, productArray.length); productArray = newArray; } // Add the new product to the next available position productArray[currentSize] = p; currentSize++; return count(); } public int count() { // Return the actual number of products, not the array's total capacity return currentSize; } public Product getProduct(int index) { // Validate the index against currentSize (not array length!) if (index < 0 || index >= currentSize) { return null; } return productArray[index]; } public boolean removeProduct(int id) { int indexToRemove = -1; // Find the index of the product with the given ID for (int i = 0; i < currentSize; i++) { if (productArray[i].getId() == id) { indexToRemove = i; break; } } if (indexToRemove != -1) { // Shift all elements after the removed index left by one for (int i = indexToRemove; i < currentSize - 1; i++) { productArray[i] = productArray[i + 1]; } // Clear the last element (optional but good practice) productArray[currentSize - 1] = null; currentSize--; return true; } return false; } }
Key Changes Explained
- Member Variables: We replaced
listOfProductwithproductArray(the fixed-size array) andcurrentSizeto track how many products are actually stored (since the array's length is its total capacity, not the active element count). - Constructor: Initialize the array with a starting capacity (10 here, but you can pick a number that makes sense for your use case).
- addProduct: Checks if the array is full, and if so, resizes it by creating a new array (double the size) and copying existing elements over. Then adds the new product to the next empty slot and increments
currentSize. - count: Simply returns
currentSizeinstead of relying on the list'ssize()method. - getProduct: Validates the index against
currentSize(not the array's length) to avoid accessing empty slots beyond the actual products. - removeProduct: After finding the index to remove, shifts all elements after that index left by one to fill the gap, then decrements
currentSizeand clears the now-unused last slot.
This approach keeps things simple while handling all the core functionality your original class had—no fancy libraries or complex logic required.
内容的提问来源于stack exchange,提问作者Jack Duvall
相关产品推荐
相关产品推荐

