简体   繁体   English

为什么会出现“分配的左侧必须是变量”?

[英]Why do I get “The left-hand side of an assignment must be a variable”?

I'm making a "dog register" where you can save information about dogs and bid on dogs up for auction etc. I am a beginner and was recently told that array list should be private, this information got me in some trouble. 我正在做一个“狗登记”,您可以在其中保存有关狗的信息以及对狗进行拍卖等竞标。我是一个初学者,最近被告知阵列列表应该是私有的,此信息使我有些麻烦。

I got a class named Auction where you can store bids, I made a get method in this class to use in the main class. 我有一个名为Auction的类,您可以在其中存储出价,我在该类中创建了一个get方法供主类使用。 It works on every line except one in my method where you can delete a user. 它适用于除我可以删除用户的方法中的每一行以外的所有行。

private void deleteUser()
{
    System.out.println("Enter the name of the user: ");
    String name = input.nextLine().toLowerCase().trim();

    User deleteUser = getUser(name);
    while (name.isEmpty()) {
        System.out.println("Error. Enter the name of the user: ");
        name = input.nextLine().toLowerCase().trim();

        deleteUser = getUser(name);
    }

    if (deleteUser == null) {
        userException();
        return;
    }

    for (Auction a : auctionRegister) {
        ArrayList<Bid> bids = new ArrayList<>();
        for (Bid b : a.getBidList()) {
            if (b.getUser() != deleteUser) {
                bids.add(b);
            }
        }
        a.getBidList() = bids;
    }

    if (deleteUser.getDogList() != null) {

        for (Dog dog : deleteUser.getDogList()) {
            dogRegister.remove(dog);
        }
    }

    userRegister.remove(deleteUser);
    System.out.println("User " + deleteUser.getUserName() + " is removed");
}

I get the error message on this line("The left-hand side of an assignment must be a variable"): 我在这行上收到错误消息(“分配的左侧必须是变量”):

a.getBidList() = bids;

Everything worked when i had the array list public so my question is, how do i solve this? 当我将数组列表公开时,一切正常,所以我的问题是,如何解决这个问题?

Java has 2 varieties of 'buckets' in which to store information. Java有两种存储信息的“存储桶”。 One variety is a variable , the other is a constant . 一种是变量 ,另一种是常数 Both varieties let you look in the bucket to see the contents. 两种品种都可以让您在存储桶中查看内容。 A variable also allows you to you replace the current contents of the bucket. 变量还允许您替换存储桶中的当前内容。

Your syntax , a.setBidList() is a method call, which is constant. 您的语法a.setBidList()是一个方法调用,它是恒定的。 You can read from it but you can't assign a new value to it. 您可以读取它,但不能为其分配新值。 Since you have it on the left side of an assignment statement, you are asking Java to do something it can't do. 由于您将其放在赋值语句的左侧,因此您正在要求Java做它不能做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM