【问题标题】:Cannot reference a variable - cannot find symbol无法引用变量 - 找不到符号
【发布时间】:2021-06-27 00:57:38
【问题描述】:

我正在制作我创建的“项目”类型的 ArrayList。该类有一个名为name 的变量来描述该项目。这些项目存储在一个 ArrayList 中。

我不知道如何引用 name 变量,因为它在编译器中产生了一个错误,上面写着 cannot find symbol - variable name. 具体来说,我这样引用它:

for (int i=0; i < theMenu.size(); i++) {
                Item temp = theMenu.get(i);
                if(temp.name == keyword)
                    System.out.println("test");
            }

我也试过这样引用它:

for (int i=0; i < theMenu.size(); i++) {
                if(theMenu.get(i).name == keyword)
                    System.out.println("test");
            }

它会产生同样的错误。请帮忙!以下是相关代码:

import java.util.ArrayList;
import java.util.Scanner;

public class Doms {

    public static class Item {
        public Item(int itemID, String itemName, double itemPrice, double itemSalePrice, String itemSaleBeginDate, String itemSaleEndDate, ArrayList<Integer> itemReviews, ArrayList<String> itemComments) {
            int id = itemID;
            String name = itemName;
            double price = itemPrice;
            double salePrice = itemSalePrice;
            String SaleBeginDate = itemSaleBeginDate;
            String SaleEndDate = itemSaleEndDate;
            ArrayList<Integer> reviews = itemReviews;
            ArrayList<String> comments = itemComments;
        }
    }

    public static void main(String[] args) {

        // A list containing each Item and all of its contents
        ArrayList<Item> items = new ArrayList<Item>();

        // Create Item elements
        ArrayList<Integer> reviews2 = new ArrayList<Integer>();
        ArrayList<String> comments2 = new ArrayList<String>();
        Item item2 = new Item(2, "Sour Cream Donut", 1.29, 1.29, "", "", reviews2, comments2);
        items.add(item2);

        //This part doesn't work
        for (int i=0; i < theMenu.size(); i++) {
            Item temp = theMenu.get(i);
            if(temp.name == keyword)
                System.out.println("test");
        }
    }
}

【问题讨论】:

  • Item 没有任何字段(成员变量)。您的构造函数只是将所有参数分配给 局部变量,一旦构造函数返回,这些参数将不再存在。重新阅读您的 Java 学习指南,了解如何在构造函数中声明 字段 并分配给它们。参见例如The Java™ Tutorials - Declaring Member Variables 的一个类的示例,其中的字段在构造函数中分配。
  • 如果你在比较字符串,你需要使用equals()而不是==。

标签: java variables arraylist types reference


【解决方案1】:

您的代码有很多需要工作的地方,但正如 cmets 中所建议的,主要问题是变量 name 例如在您的 Item 类中不存在,实际上不存在变量在你的Item 班级中。

要解决这个问题,您需要创建可以从其他地方引用的类变量,注意这些变量是如何直接在 Item 类中而不是在 Item(...) 方法中定义的:

public static class Item {
    //Create class variables
    int id;
    String name;
    double price;
    double salePrice;
    String SaleBeginDate;
    String SaleEndDate;
    ArrayList<Integer> reviews;
    ArrayList<String> comments;

    public Item(int itemID, String itemName, double itemPrice, double itemSalePrice, String itemSaleBeginDate, String itemSaleEndDate, ArrayList<Integer> itemReviews, ArrayList<String> itemComments) {
        //Use the values passed into the method and store them as class variables
        id = itemID;
        name = itemName;
        price = itemPrice;
        salePrice = itemSalePrice;
        SaleBeginDate = itemSaleBeginDate;
        SaleEndDate = itemSaleEndDate;
        reviews = itemReviews;
        comments = itemComments;
    }
}

现在您可以创建一个Item 并像正常一样获取值:

//Create an item
Item item1 = new Item(1, "Apple Fritter", 1.49, 1.29, "February 1, 2021", "July 1, 2021", reviews1, comments1);

//Get the public values from the item:
System.out.println("ID: " + item1.id);
System.out.println("Name: " + item1.name);
System.out.println("Price: " + item1.price);

你会得到以下输出:

ID: 1
Name: Apple Fritter
Price: 1.49

请注意,根据您的问题,您需要使用String.equals(...) 方法来比较字符串,您不能使用== 比较字符串。例如,这是将名称字符串与关键字字符串进行比较的正确方法:if(temp.name.equals(keyword))

【讨论】:

    猜你喜欢
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 2016-07-01
    • 2016-03-27
    • 2010-10-12
    • 2012-03-20
    • 1970-01-01
    相关资源
    最近更新 更多