gpt4 book ai didi

java - 如何处理 TreeMap?我可以仅使用一个 TreeMap 来存储多个项目的值吗?

转载 作者:行者123 更新时间:2023-12-01 15:17:07 25 4
gpt4 key购买 nike

我正在尝试模拟生产系统。为了简要解释我打算做什么,我将创建一个面板,其中有一些表来保存值(用于几个工作站和作业类型的属性(见下图))。当我运行它时,这些值应该被存储以供进一步处理。

在上一个问题中,有人建议我使用 TreeMaps 来存储这些值,因此我创建了如下内容:

Station[num][type][avg_time][posx][posy][state]
Part[num][type][state]

这是我到目前为止的代码:

L.java

import java.awt.*;
import javax.swing.*;


public class L extends JFrame {

public static final int ww = 1000;
public static final int wh = 600;
public static final int bgw = (ww - 30);
public static final int bgh = (wh - 80);
public static final String wt = "Teste";
Color new_piece = new Color(255,0,0);
Color progress_piece = new Color(255,215,0);
Color ready_piece = new Color(173,255,47);
Container pane = getContentPane();
Dimension appletSize = pane.getSize();
int wHeight = appletSize.height;
int wWidth = appletSize.width;
DrawRectangle rectangle = new DrawRectangle();
public TMap t;

public L() {
setSize(ww,wh);
this.setTitle(wt);
// Sim(int nparts);
JButton startButton = new JButton("Start");
JButton stopButton = new JButton("Stop");

//... Add Listeners
// startButton.addActionListener(new StartAction());
//stopButton.addActionListener(new StopAction());

//... Layout inner panel with two buttons horizontally
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT,10,10));
buttonPanel.add(startButton);
buttonPanel.add(stopButton);
this.setLayout(new BoxLayout(pane,BoxLayout.Y_AXIS));
this.add(rectangle);
this.add(buttonPanel);

//Sim();
t = new TMap();
test();
//pane.add(rectangle);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
}

public void addRectangle(int px, int py, int pw, int ph, Color pc, String state) {
this.rectangle.addRectangle( px, py, pw, ph, pc, state);
}

public void Sim() {
addRectangle(20,20,10,10,Color.green,"new");
/*for (int i=0;i<=nparts;i++) {
addRectangle(200,200,50,Color.green);
}*/
}

public void test() {
// First Station Proprieties
t.put("num",1);
t.put("type",1);
t.put("avg_time",5);
t.put("posx",100);
t.put("posy",20);
t.put("state",0);
// Second Station Proprieties
t.put("num",2);
t.put("type",2);
t.put("avg_time",7);
t.put("posx",200);
t.put("posy",20);
t.put("state",0);
/*System.out.println("Now the tree map Keys: " + t.St.keySet());
System.out.println("Now the tree map contain: " + t.St.values());*/
}

public static void main(String[] args) {

try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}

SwingUtilities.invokeLater(new Runnable() {
public void run() {
L l = new L();
//System.out.println("Entryset: " + t.keySet());
//System.out.println("Entryset: " + t.Station() + "\n");
}
});

}

}

DrawRectangle.java

 import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.*;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

public class DrawRectangle extends JPanel implements ActionListener {

private java.util.List<Rectangle2D> squares;
private java.util.List<Color> colors;
private long seconds = 1;
private int anim_interval = (int) TimeUnit.SECONDS.toMillis(seconds);
private Timer sim_timer;
private Timer idle_timer;
int px = 10, velx = 2;
String state;
Color pc;


public DrawRectangle(){
//this.setBounds(10, 10, 10, 10);
this.setMinimumSize(new Dimension(100,100));
this.setPreferredSize(new Dimension(100,L.bgh));
this.setMinimumSize(new Dimension(500,L.bgh));
setBackground(Color.gray);
setDoubleBuffered(true);
setBorder(BorderFactory.createLineBorder(Color.black, 1));
squares = new ArrayList<Rectangle2D>();
colors = new ArrayList<Color>();
sim_timer = new Timer(anim_interval,this);
sim_timer.start();
}

public void addRectangle(int px, int py, int pw, int ph, Color pc, String state) { // square
squares.add( new Rectangle2D.Double(px, py, pw, ph) ) ;
pc = pc;
//System.out.println(state);
//this.a = a;
//this.startX = startX;
//this.startY = startY;
}

public void actionPerformed(ActionEvent e) {
/*if (px < 0 || px > 990) {
velx = -velx;
}*/
//System.out.println(px);
if (px == 20) {
sim_timer.stop();
state = "idle";
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(DrawRectangle.class.getName()).log(Level.SEVERE, null, ex);
}
sim_timer.start();
state = "going";
}
else if (px == 50) {
sim_timer.stop();
state = "done";
seconds = 2;
}
//if (state != "idle") {
px = px + velx;
repaint();
//}
}

private void idlestate() throws InterruptedException {
Thread.sleep(5000);
state = "idle";
}
private void goingstate() {
state = "going";
}
private void donestate() {
state = "done";
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g1 = (Graphics2D) g.create();
Graphics2D g2 = (Graphics2D) g.create();
//System.out.println("D1");
/*for( Rectangle2D rect : squares ) {
System.out.println(colors);
//g1.setPaint(colors);
g1.fill(rect);
}*/
//for(int i=0;i<squares.size();i++) {
//System.out.println("D2");
//g1.setColor(colors.get(i));
if (state == "going") { g1.setColor(Color.orange); }
else if (state == "idle") { g1.setColor(Color.yellow); }
else if (state == "done") { g1.setColor(Color.green); }
else { g1.setColor(Color.red); }
//g1.fill(squares.get(i));
g1.fillRect(px, 10, 10, 10);
g2.setColor(Color.blue);
g2.fillRect(px,40,10,10);
//g1.dispose();
//}
}
public void setColor(Color newColor) {
pc = newColor;
}

}

TMap.java

import java.util.*;

public class TMap {
public TreeMap <String, Integer> St;
public int num_atrib = 6;

public TMap () {
St = new TreeMap <>();
}
public Set<String> getKeySet() {
return St.keySet();
}

public Integer get(String s) {
return St.get(s);
}

public void put (String s, int i) {
St.put(s,i);
System.out.println("Now the tree map Keys: " + St.keySet());
System.out.println("Now the tree map contain: " + St.values());
}

public TreeMap<String, Integer> Station(String s,int i) {
}
}

这里的 DrawRectangle.java 代码仅供那些可能想要编译代码但与实际问题无关的人使用。 TMap.java 是我创建 map 的地方,并且具有处理数据的方法。一切工作正常,我的问题如下:

很可能,在模拟时,我会有多个站点,因此我需要存储这样的信息:

 Station[num][type][avg_time][posx][posy][state]
Station[1][1][5][100][20][0]
Station[2][2][7][200][20][0]
Station[3][3][4][300][20][0]

事情是,当我将新数据放入树形图中时,它将覆盖以前存储的数据,因此如果我添加信息两次,输出将如下:

 Now the tree map Keys: [avg_time, num, posx, posy, type]
Now the tree map contain: [5, 1, 100, 20, 1]
Now the tree map Keys: [avg_time, num, posx, posy, state, type]
Now the tree map contain: [7, 2, 200, 20, 0, 2]

模拟将限制在 6 个工作站,所以我的问题是,处理这个问题的最佳实践是什么?我唯一能想到的就是创建 6 个 TreeMap,并且只使用需要的那些,但我很确定必须有一种更简单、更有效的方法来存储数据。

预先感谢您的帮助!

最佳答案

我不确定我是否理解您想要实现的目标,但这就是 map 的工作方式。它存储键/值对,确保键是唯一的。

所以当你写:

map.put("abc",100);
map.put("abc",200);

map 在第二行之后只有一个条目,即 key = "abc"和 value = 200。

您可能希望将电台存储在列表中,其中每个电台都是包含相关信息的键/值对的映射:

List<TMap> stations = new ArrayList<TMap> ();

TMap station1 = new TMap();
station1.put("num",1);
station1.put("type",1);
...
stations.add(station1);

TMap station2 = new TMap();
station2.put("num",2);
station2.put("type",2);
...
stations.add(station2);

ps:我不确定我是否理解拥有 TreeMap 类的意义。所以你也可以使用 map 列表:

List<Map<String, Integer>> stations = new ArrayList<Map<String, Integer>> ();
Map<String, Integer> station1 = new TreeMap<String, Integer> ();

关于java - 如何处理 TreeMap?我可以仅使用一个 TreeMap 来存储多个项目的值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11473421/

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