Java中ArrayList的.get()、.add()方法报找不到符号问题求助
Hey there, let's break down the issues in your code that's causing those frustrating 'cannot find symbol' errors, plus some other bugs that'll trip you up once you fix those:
Key Issues & Fixes
Variable Name Collisions (Most Critical)
Your method parameters (productsasString,pricesasdouble) have the same names as your class-levelArrayListmembers. When you callproducts.size()orproducts.add()insideput(), you're actually trying to call ArrayList methods on a String or double—which don't exist!
Fix: Rename your parameters to something distinct, likeproductNameandprice, or usethis.productsto explicitly reference the class member.Incorrect Method Call Syntax
You’ve got lines likeproducts() = products();andproducts() = mergePriceProduct();—your class members are variables, not methods, so you don’t use parentheses to access them. It should bethis.products = ...instead.Iterator Spelling & Method Errors
In yourget()method:- You defined
Iterator<String> productIterbut tried to useproditer(lowercase 'i') in the while loop—Java is case-sensitive! - There’s no
nextString()method for Iterator; usenext()instead, which returns the element of type String.
- You defined
Scope & Logical Errors
- In
put(), you’re trying to loop overproducts.size()but ifproductsis a String parameter,size()is for the String's length—not what you want. Your logic here seems off; it looks like you want to add a single product-price pair, not loop over a collection. - You defined
mergePriceProductinside the for loop, so it’s not accessible outside that loop when you try to assign it tothis.products. - The
ArrayList<String> mergePriceProduct = new ArrayList<String>(products.get(i) + prices.get(i));line is wrong—there’s no ArrayList constructor that takes a String like that. You probably want to create a new entry and add it to the list, not initialize the list with that value.
- In
Corrected Code Example
Here’s a revised version of your code with these fixes applied, focusing on core functionality of adding product-price pairs and retrieving them:
import java.util.ArrayList; import java.util.Iterator; public class ProductPrices { // Class-level ArrayList members private ArrayList<String> products = new ArrayList<>(); private ArrayList<Double> prices = new ArrayList<>(); // Constructor (if you want to initialize with initial values) public ProductPrices(String initialProduct, double initialPrice) { this.products.add(initialProduct); this.prices.add(initialPrice); } // Empty constructor for flexibility public ProductPrices() {} // Method to add a single product-price pair public void put(String productName, double price) { products.add(productName); prices.add(price); } // Method to list all products and their prices public void listAllProducts() { Iterator<String> productIter = products.iterator(); Iterator<Double> priceIter = prices.iterator(); while (productIter.hasNext() && priceIter.hasNext()) { System.out.println(productIter.next() + ": $" + priceIter.next()); } } // Method to get price by product name public Double getPrice(String productName) { int index = products.indexOf(productName); if (index != -1) { return prices.get(index); } return null; // Return null if product not found } }
Quick Notes for Java Beginners
- Always be mindful of variable scope and case sensitivity—Java cares a lot about both!
- When you have class members and method parameters with the same name, use
this.to refer to the class member. - Double-check method names for classes like ArrayList and Iterator—you can confirm correct method names directly in your IDE or via offline Java docs.
内容的提问来源于stack exchange,提问作者Gee

