gpt4 book ai didi

java - 我被“比较方法违反了它的一般契约(Contract)!”击中。创建/添加超过一定数量的对象时发生异常

转载 作者:行者123 更新时间:2023-12-02 13:04:57 27 4
gpt4 key购买 nike

我在大学学习Java,这是我的任务。任务是创建一个由颜色方块组成的x x y网格,每个网格在单独的线程中运行,并且每k ms要么将其颜色更改为随机的颜色,要么对其邻居的颜色求平均。

现在,如果我创建一个30 x 30的网格,则一切运行正常。但是,如果我以40 x 40尝试,则在应用程序启动时和关闭应用程序时都会出现以下异常。除此之外,它似乎可以正常运行,尽管网格似乎在窗口中的位置略有错误(比预期的要窄)。

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 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:729)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
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:702)
at java.awt.EventQueue$4.run(EventQueue.java:700)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
at java.awt.SequencedEvent.dispatch(SequencedEvent.java:128)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
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:702)
at java.awt.EventQueue$4.run(EventQueue.java:700)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
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)


这是我的代码:
主要课程Symulacja:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.util.Random;

// main class
@SuppressWarnings("serial")
public class Symulacja extends JPanel {
public Symulacja(int x, int y, int k, double p) {
super();
Field.delay = k;
Field.p = p;
Field.random = new Random();

setLayout(new GridLayout(y, x));

//creating Field objects and adding them to the panel
Field fv[][] = new Field[y][];
for (int i = 0; i<y; i++){
fv[i] = new Field[x];
for (int j = 0; j<x; j++){
fv[i][j] = new Field();
add(fv[i][j]);
}
}

//setting the neighbours in all Field objects
for (int i = 0; i<y; i++){
for (int j = 0 ; j<x; j++){
Field fv2[] = {fv[(i+1)%y][j], fv[(i+y-1)%y][j], fv[i][(j+1)%x], fv[i][(j+x-1)%x]};
fv[i][j].setArr(fv2);
}
}

//starting the threads
for (int i = 0; i<y; i++){
for (int j = 0 ; j<x; j++){
new Thread(fv[i][j]).start();
}
}

}

public static void main(String[] args) {
Symulacja s = new Symulacja(30,30,100,0); //works
//Symulacja s = new Symulacja(40,40,100,0); //throws exception
JFrame f = new JFrame();
f.setLayout(new BorderLayout());
f.add(s, BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setSize(400, 400);
}

}


Field类(单个网格元素):

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;

@SuppressWarnings("serial")
class Field extends Canvas implements Runnable
{
Field()
{
color = nextInt();
randFlag = false;

//zmiana koloru na losowy po kliknięciu myszą
addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
randFlag = true;
}
});
}


void setArr(Field[] a)
{
fieldArr = a;
}


public void run()
{
while (true){
double rand = nextDouble();
int newcolor;
if (rand <= p || randFlag){ // losowy kolor lub kliknięcie myszą
newcolor = nextInt();
randFlag = false;
}
else{ // uśredniony kolor
newcolor = 0;
for (Field f: fieldArr)
newcolor+=f.getColor()/4;
}

if (newcolor!=getColor()){
setColor(newcolor);
repaint();
}

try {
Thread.sleep((long)((nextDouble() + 0.5)*delay));
} catch (InterruptedException e) {
return;
}
}
}


public void paint(Graphics g)
{
g.setColor(new Color(getColor()));
g.fillRect(0, 0, getWidth(), getHeight());
g.dispose();
}


synchronized public int getColor()
{
return color;
}


synchronized private void setColor(int color)
{
this.color = color;
}


private int nextInt()
{
synchronized (random) {
return random.nextInt();
}
}


private double nextDouble()
{
synchronized (random) {
return random.nextDouble();
}
}

static public int delay;
static public double p;

Field fieldArr[];
private int color;

boolean randFlag;
static Random random;
}

最佳答案

这是一个比较器错误,可能在这里:

if (rand <= p || randFlag)


其中 rand从未初始化为新的随机数。试试看

 rand = new Random();


致电 nextDouble().之前

编辑。

注意使用nextDouble()。它会给您一个介于0,0和1,0之间的值。

关于java - 我被“比较方法违反了它的一般契约(Contract)!”击中。创建/添加超过一定数量的对象时发生异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16466471/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com