Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >程序无法在对象的更新信息之后执行compareTo功能

程序无法在对象的更新信息之后执行compareTo功能
EN

Stack Overflow用户
提问于 2022-09-10 09:23:34
回答 1查看 37关注 0票数 1

在更新了产品的一些信息后,我试图更改打印菜单中产品的位置。具体而言,按降序排列“数量”。如果产品数量相同,则列表将按升序"UnitPrice“字段排序。

最初,如果我再添加一个产品,compareTo()函数可以正常工作,但是在使用了产品更新函数之后,compareTo()函数就不能工作了。我在这个项目中使用TreeSet。有人能给我解释一下这个案子吗?非常感谢。

这是我的compareTo()函数

代码语言:javascript
运行
AI代码解释
复制
    @Override
public int compareTo(Product o) {
    if (this.getQuantity() < o.getQuantity()) {
        return 1;
    }
    if (this.getQuantity() == o.getQuantity()) {
        return this.unitPrice-o.unitPrice;
    }
    return -1;
}

这是我的更新函数

代码语言:javascript
运行
AI代码解释
复制
    public void updateInfo() {
    Scanner sc = new Scanner(System.in);
    String id, productName, quantity, unitPrice, numberForAvail;
    while (true) {
        try {
            System.out.print("Input products's id: ");
            id = sc.nextLine().toUpperCase();
            if (id.isEmpty()) {
                this.id = this.id;
            } else {
                if (!id.matches(letter_regEx) || id.length() < 5) {
                    throw new Exception();
                }
                this.id = id;
            }
            break;
        } catch (Exception e) {
            System.out.println("ID must be character and lenght > 5");
        }
    }

    while (true) {
        try {
            System.out.print("Input prodct's name: ");
            productName = sc.nextLine().toUpperCase();
            if (productName.isEmpty()) {
                this.productName = this.productName;
            } else {
                if (!productName.matches(letter_regEx)) {
                    throw new Exception();
                }
                this.productName = productName;
            }
            break;
        } catch (Exception e) {
            System.out.println("Invalid name");
        }
    }
    while (true) {
        try {
            System.out.print("Input Unit Price: ");
            unitPrice = sc.nextLine();
            if (unitPrice.isEmpty()) {
                this.unitPrice = this.unitPrice;
            } else {
                if (!unitPrice.matches("^([1-9][0-9]{0,3}|10000)$")) {
                    throw new Exception();
                }
                this.unitPrice = Integer.parseInt(unitPrice);
            }
            break;
        } catch (Exception e) {
            System.out.println("Unit price must be > 0 and < 10000");
        }
    }
    while (true) {
        try {
            System.out.print("Input quantity: ");
            quantity = sc.nextLine();
            if (quantity.isEmpty()) {
                this.quantity = this.quantity;
            } else {
                if (!quantity.matches("^([1-9][0-9]{0,2}|1000)$")) {
                    throw new Exception();
                }
                this.quantity = Integer.parseInt(quantity);
            }
            break;
        } catch (Exception e) {
            System.out.println("Quantity must be > 0 and < 1000");
        }
    }

    while (true) {
        try {
            System.out.print("Input status [0(not available)/ 1 (available)]: ");
            numberForAvail = sc.nextLine();
            if (numberForAvail.isEmpty()) {
                this.numberForAvail = this.numberForAvail;
            } else {
                if (!numberForAvail.matches("^[01]$")) {
                    throw new Exception();
                }
                this.numberForAvail = Integer.parseInt(numberForAvail);
                break;
            }
        } catch (Exception e) {
            System.out.println("Status must 0 for NOT AVAILABLE and 1 for AVAILABLE");
        }
    }
    if (this.numberForAvail == 0) {
        status = "Not available";
    } else {
        status = "Available";
    }

}

这是我的输出

当我添加一个产品时:

当我把NGUYEN的数量改为125的时候,它不起作用了

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-10 09:44:17

元素根据插入时与元素的比较方式添加到集合中的位置。如果您更改一个元素,使其顺序发生变化,那么像TreeSet这样的数据结构的所有不变量都会被破坏,任何事情都可能发生。在将元素插入依赖于它们的数据结构后,您永远不应该更改元素的这些属性。这意味着,在使用基于顺序的数据结构(如TreeSet )时,不应该更改更改顺序的字段,而在使用基于哈希的结构(如HashSet )时,不应该更改更改哈希代码的字段。

如果确实需要修改值,则需要从TreeSet中删除它,然后对其进行变异,然后将其与新的顺序放在一起。或者,更好的是,拥有一个不可变的类,只需创建一个新实例来替换它。默认情况下,类是不可变的,这首先避免了所有这些杂乱无章的事情。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73673834

复制
相关文章
React报错之无法在未挂载的组件上执行React状态更新
原文链接:https://bobbyhadz.com/blog/react-cant-perform-react-state-update-on-unmounted-component[1]
chuckQu
2022/08/19
2.7K0
iOS开发中在指定的某些线程执行完之后去执行其他线程
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010105969/article/details/79139208
用户1451823
2018/09/13
1.2K0
iOS开发中在指定的某些线程执行完之后去执行其他线程
【漫画】finally到底是在return之前执行还是return之后执行?
比如说上面所示的代码,在try语句里面 i / 0 的话会抛出来异常,这样的话程序就在i / 0这里由于抛出了异常,所以程序不会继续往下去执行try包含的语句了。首先进入到catch语句里面,由于finally语句一定会执行,接下来就会执行finally中的语句,所以就得到了上面的执行结果。
乔戈里
2019/01/23
7320
Oracle——无法在查询中执行 DML 操作
create or replace function test_f(id varchar2) return varchar2 is Result varchar2(100); begin insert into sfcs_temp_17109 (sn)values(id);
_一级菜鸟
2019/09/10
4.5K0
Oracle——无法在查询中执行 DML 操作
vue/uniapp 如何让页面的 onLoad 在 onLaunch 之后执行[通俗易懂]
app.vue里的 onLaunch 中如果有异步方法(比如:登录),返回结果可能会在页面的 onLoad 之后,但 onLoad 中的方法需要登录回调的结果。
全栈程序员站长
2022/07/30
3.2K0
compareTo()方法
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/158435.html原文链接:https://javaforall.cn
全栈程序员站长
2022/09/14
3550
关于compareTo使用的几种情况
String source = "A"; String tager = "B"; int i = source.compareTo(tager); System.out.println(i); //输入为 -1 String source = "B"; String tager = "B"; int i = source.compareTo(tager); System.out.println(i); //输出为 0 String source =
信铁寒胜
2020/07/31
5090
ReactDOM.render在react中执行之后发生了什么?
通常是如下图使用,在提供的 container 里渲染一个 React 元素,并返回对该组件的引用(或者针对无状态组件返回 null)。本文主要是将ReactDOM.render的执行流程在后续文章中会对创建更新的细节进行分析,文中的源代码部分为了方便阅读将__DEV__部分的代码移除掉了。
flyzz177
2022/12/08
7590
Java 堆栈信息对象 StackTraceElement,获取当前线程的执行方法
java.langStackTraceElement类保存了Java中线程中的方法栈信息:
青山师
2023/05/05
6670
Java finally 语句到底是在 return 之前还是之后执行?
今天跟大家分享下Java finally语句的知识。finally隐藏了这样的细节?
互扯程序
2019/11/01
1.2K0
python程序执行时间_用于在Python中查找程序执行时间的程序
The execution time of a program is defined as the time spent by the system to execute the task. As we all know any program takes some execution time but we don't know how much. So, don't worry, in this tutorial we will learn it by using the datetime module and also we will see the execution time for finding the factorial of a large number. A large number will be provided by the user and we have to calculate the factorial of a number, also we have to find the execution time of the factorial program. Before going to write the Python program, we will try to understand the algorithm.
用户7886150
2021/01/28
2.1K0
Java finally语句到底是在return之前还是之后执行?
网上有很多人探讨Java中异常捕获机制try...catch...finally块中的finally语句是不是一定会被执行?很多人都说不是,当然他们的回答是正确的,经过我试验,至少有两种情况下finally语句是不会被执行的:
bear_fish
2018/09/20
1.3K0
小程序在父组件执行子组件方法,可适用于下拉刷新上拉加载之后执行子组件方法
当父组件引用了子组件的时候,会遇到父组件执行子组件的方法,比如下拉刷新上拉加载等事件只有在页面中才能检测到,但是获取数据的方法在子组件,这时就可以执行子组件方法。
蓓蕾心晴
2018/08/15
1.1K0
Java finally语句到底是在return之前还是之后执行?
网上有很多人探讨Java中异常捕获机制try…catch…finally块中的finally语句是不是一定会被执行?
挨踢小子部落阁
2019/08/05
8490
glut库更新旧程序无法完成编译问题描述
zhangrelay@LAPTOP-5REQ7K1L:~$ cd cppcode/Bouncing-Ball-Animation-master/src/
zhangrelay
2022/08/10
1.1K0
glut库更新旧程序无法完成编译问题描述
ReactDOM.render在react源码中执行之后发生了什么?
通常是如下图使用,在提供的 container 里渲染一个 React 元素,并返回对该组件的引用(或者针对无状态组件返回 null)。本文主要是将ReactDOM.render的执行流程在后续文章中会对创建更新的细节进行分析,文中的源代码部分为了方便阅读将__DEV__部分的代码移除掉了。
flyzz177
2022/09/30
5960
Java finally语句到底是在return之前还是之后执行?
网上有很多人探讨Java中异常捕获机制try…catch…finally块中的finally语句是不是一定会被执行?很多人都说不是,当然他们的回答是正确的,经过我试验,至少有两种情况下finally语句是不会被执行的:
乔戈里
2019/10/28
8080
ReactDOM.render在react源码中执行之后发生了什么?
通常是如下图使用,在提供的 container 里渲染一个 React 元素,并返回对该组件的引用(或者针对无状态组件返回 null)。本文主要是将ReactDOM.render的执行流程在后续文章中会对创建更新的细节进行分析,文中的源代码部分为了方便阅读将__DEV__部分的代码移除掉了。
flyzz177
2023/01/06
5920
Class 对象在执行引擎中的初始化过程
装载 装载是指 Java 虚拟机查找 .class 文件并生成字节流,然后根据字节流创建 java.lang.Class 对象的过程。
老马的编程之旅
2022/06/22
1.1K0
Class 对象在执行引擎中的初始化过程
Java中的compareTo方法详解
在Java编程中,有时候我们需要对对象进行比较和排序。为了实现这一目标,Java提供了一个非常有用的接口叫做Comparable,以及一个重要的方法compareTo。本文将详细解释什么是Comparable接口,以及如何使用compareTo方法来比较对象。
修己xj
2023/09/13
4700

相似问题

在表单验证之后执行功能

14

重写compareTo()方法的功能

35

在SIGTRAP之后,无法单步执行gdb中的程序

19

在CREATE INDEX之后更新统计信息?

05

在使用电子邮件执行signIn之后,无法使用firebase获取更新的配置文件信息

20
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档