简体   繁体   English

线程“AWT-EventQueue-0”中的异常java.lang.IllegalArgumentException:比较方法违反了其总契约

[英]Exception in thread “AWT-EventQueue-0” java.lang.IllegalArgumentException: Comparison method violates its general contract

Disclaimer: I saw the thread about transitivity of comparison functions, but I don't think mine are bad/I dont see mistake in them 免责声明:我看到了关于比较函数传递性的线索,但我认为我的不好/我没有看到它们的错误

So I wrote a simulation of rabbits and wolfs - there is a set amount of wolves and rabbits in the beginning, rabbits run away from wolfs and wolfs chase rabbits, when they meet in the same block rabbit gets killed and simulation ends when there are no rabbits left. 所以我写了一个兔子和狼的模拟 - 开始时有一定数量的狼和兔子,兔子逃离狼和狼追逐兔子,当他们在同一个街区相遇时兔子被杀死,模拟结束时没有兔子离开了。

For small boards, up till about 32x32 all is ok, but for 64+ I get the following error while running. 对于小型电路板,直到大约32x32都可以,但对于64+我运行时会出现以下错误。

Also, here are my 3 comparison functions: 另外,这是我的3个比较函数:

lBlocks are JLabels, hare/wolf/killer are icons lBlocks是JLabels,野兔/狼/杀手是图标

public  synchronized boolean isEmpty(int x, int y){
    if(!(lBlocks[x][y].getIcon() instanceof Icon)) return true;
    return false;
}


public  synchronized boolean isWolf(int x, int y){
    if(!(lBlocks[x][y].getIcon() instanceof Icon)) return false;
    if(lBlocks[x][y].getIcon().equals(hare)||lBlocks[x][y].getText().equals("h")||isEmpty(x,y)) return false;
    return true;
}

public  synchronized boolean isHare(int x, int y){
    if(!(lBlocks[x][y].getIcon() instanceof Icon)) return false;
    if(isEmpty(x,y)||isWolf(x,y)) return false;
    return true;
}

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Comparison method violates its general contract!
    at java.util.TimSort.mergeLo(TimSort.java:747)
    at java.util.TimSort.mergeAt(TimSort.java:483)
    at java.util.TimSort.mergeCollapse(TimSort.java:410)
    at java.util.TimSort.sort(TimSort.java:214)
    at java.util.TimSort.sort(TimSort.java:173)
    at java.util.Arrays.sort(Arrays.java:659)
    at java.util.Collections.sort(Collections.java:217)
    at javax.swing.SortingFocusTraversalPolicy.enumerateAndSortCycle(SortingFocusTraversalPolicy.java:136)
    at javax.swing.SortingFocusTraversalPolicy.getFocusTraversalCycle(SortingFocusTraversalPolicy.java:110)
    at javax.swing.SortingFocusTraversalPolicy.getFirstComponent(SortingFocusTraversalPolicy.java:435)
    at javax.swing.LayoutFocusTraversalPolicy.getFirstComponent(LayoutFocusTraversalPolicy.java:166)
    at javax.swing.SortingFocusTraversalPolicy.getDefaultComponent(SortingFocusTraversalPolicy.java:515)
    at javax.swing.SortingFocusTraversalPolicy.getComponentDownCycle(SortingFocusTraversalPolicy.java:204)
    at javax.swing.SortingFocusTraversalPolicy.getFirstComponent(SortingFocusTraversalPolicy.java:447)
    at javax.swing.LayoutFocusTraversalPolicy.getFirstComponent(LayoutFocusTraversalPolicy.java:166)
    at javax.swing.SortingFocusTraversalPolicy.getDefaultComponent(SortingFocusTraversalPolicy.java:515)
    at java.awt.FocusTraversalPolicy.getInitialComponent(FocusTraversalPolicy.java:169)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:380)
    at java.awt.Component.dispatchEventImpl(Component.java:4731)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.SequencedEvent.dispatch(SequencedEvent.java:128)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

The thing is, it does not do absolutely anything - code is working and simulation is going on, but I want to fix it. 问题是,它没有做任何事情 - 代码正在运行并且模拟正在进行,但我想解决它。 The worst fact is that there is no line where the error is ocurring and I have a lot of .equals (code is about 400 lines altogether) - I don't know where to find my mistake :( 最糟糕的事实是,没有错误发生的行,我有很多.equals(代码大约是400行) - 我不知道在哪里找到我的错误:(

edit: I found out that if both x and y of board are more than 57 this problem occurs, if they are smaller (or atleast one of them) it does not, lol. 编辑:我发现,如果板的x和y都超过57,则会出现这个问题,如果它们较小(或至少其中一个)则不会,lol。

The quick solution appears to be: System.setProperty("java.util.Arrays.useLegacyMergeSort", "true"); 快速解决方案似乎是: System.setProperty("java.util.Arrays.useLegacyMergeSort", "true"); According to this answer . 根据这个答案

暂无
暂无

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

相关问题 线程“ AWT-EventQueue-0”中的java异常java.lang.IllegalArgumentException - java exception in thread “AWT-EventQueue-0” java.lang.IllegalArgumentException 线程“ AWT-EventQueue-0”中的异常java.lang.IllegalArgumentException: - Exception in thread “AWT-EventQueue-0” java.lang.IllegalArgumentException: 为什么我得到这个异常 java.lang.IllegalArgumentException:比较方法违反了它的一般契约 - Why did I get this exception java.lang.IllegalArgumentException: Comparison method violates its general contract Java 7:java.lang.IllegalArgumentException:比较方法违反了其一般合同 - Java 7 : java.lang.IllegalArgumentException: Comparison method violates its general contract java.lang.IllegalArgumentException:比较方法违反了它的一般约定,在 java7 中给出了错误 - java.lang.IllegalArgumentException: Comparison method violates its general contract giving Error in java7 java.lang.IllegalArgumentException:比较方法违反了它的一般约定! 日期 - java.lang.IllegalArgumentException: Comparison method violates its general contract! java.util.Date Java sort抛出java.lang.IllegalArgumentException:比较方法违反了其一般合同 - Java sort throws java.lang.IllegalArgumentException: Comparison method violates its general contract Java 错误:java.lang.IllegalArgumentException:比较方法违反其一般约定 - Java Error: java.lang.IllegalArgumentException: Comparison method violates its general contract 错误:java.lang.IllegalArgumentException:即使使用替代方法,比较方法也违反了其常规协定 - Error: java.lang.IllegalArgumentException: Comparison method violates its general contract even using workaround 排序抛出java.lang.IllegalArgumentException:比较方法违反其常规协定 - Sorting throws java.lang.IllegalArgumentException: Comparison method violates its general contract
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM