You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中ArrayList的.get()、.add()方法报找不到符号问题求助

Fixing "Cannot Find Symbol" Errors with ArrayList Methods in Your Code

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 (products as String, prices as double) have the same names as your class-level ArrayList members. When you call products.size() or products.add() inside put(), you're actually trying to call ArrayList methods on a String or double—which don't exist!
    Fix: Rename your parameters to something distinct, like productName and price, or use this.products to explicitly reference the class member.

  • Incorrect Method Call Syntax
    You’ve got lines like products() = products(); and products() = mergePriceProduct();—your class members are variables, not methods, so you don’t use parentheses to access them. It should be this.products = ... instead.

  • Iterator Spelling & Method Errors
    In your get() method:

    • You defined Iterator<String> productIter but tried to use proditer (lowercase 'i') in the while loop—Java is case-sensitive!
    • There’s no nextString() method for Iterator; use next() instead, which returns the element of type String.
  • Scope & Logical Errors

    • In put(), you’re trying to loop over products.size() but if products is 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 mergePriceProduct inside the for loop, so it’s not accessible outside that loop when you try to assign it to this.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.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 19:07:44