gpt4 book ai didi

java - 如何将已排序的 Map 转换为 JTable 的二维数组

转载 作者:行者123 更新时间:2023-11-30 08:37:45 25 4
gpt4 key购买 nike

我有一个包含此信息的文本文件:

dog
sheep
cat
horse
cat
tiger
cat
cat
tiger

在另一个Q&A ,我问人们如何计算所有动物的出现次数,我用这种方法做了:

void countAnimals(){
Map m1 = new HashMap();

try (BufferedReader br = new BufferedReader(new FileReader("Animals.txt"))) {
StringBuilder sb = new StringBuilder();
String line = br.readLine();

while (line != null) {
String[] words = line.split(" ");//those are your words
for (int i = 0; i < words.length; i++) {
if (m1.get(words[i]) == null) {
m1.put(words[i], 1);
} else {
int newValue = Integer.valueOf(String.valueOf(m1.get(words[i])));
newValue++;
m1.put(words[i], newValue);
}
}
sb.append(System.lineSeparator());
line = br.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Map<String, String> sorted = new TreeMap<String, String>(m1);
for (Object key : sorted.keySet()) {
System.out.println(key + "\t Votes: " + m1.get(key));
}
}

这是控制台输出的内容:

cat  Votes: 4
dog Votes: 1
horse Votes: 1
sheep Votes: 1
tiger Votes: 2

有人可以帮助我如何将现在打印到控制台的信息放入二维数组中,我将把它放入 JTable 中吗?

更新:这是我添加到我的 countAnimals 方法中的内容:

Map<String, String> sorted = new TreeMap<String, String>(m1);
for (Object key : sorted.keySet()) {
System.out.println(key + "\t Votes: " + m1.get(key));
}
AnimalSummaryModel SOCLOSE = new AnimalSummaryModel(sorted);
SOCLOSE.run();

您提供的 AnimalSummaryModel 类如下所示:

class AnimalSummaryModel extends AbstractTableModel {

private final String[] keys;
private Map<String, String> data;

public AnimalSummaryModel(Map<String, String> data) {
this.data = data;
keys = data.keySet().toArray(new String[data.size()]);
}

public int getColumnCount() {
return 2;
}

public int getRowCount() {
return data.size();
}

public Object getValueAt(int row, int col) {
if (col == 0) {
return keys[row];
} else {
return data.get(keys[row]);
}
}

private void display() {
JFrame f = new JFrame("EnvTableTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(f, this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

public void run() {
display();
}

}

我仍然一事无成。

最佳答案

传递你的 Map<String, String>作为自定义参数 TableModel 并使用该模型构建您的 JTable .显示了一个完整的示例 here .

Map<String, String> sorted = new TreeMap<>();
data.put("cat", "Votes: 4");
data.put("tiger", "Votes: 2");
data.put("horse", "Votes: 1");
data.put("sheep", "Votes: 1");
data.put("dog", "Votes: 1");
JTable table = new JScrollPane(new JTable(new AnimalSummaryModel(sorted)));

private static class AnimalSummaryModel extends AbstractTableModel {

private final Map<String, String> data;
private final String[] keys;

public AnimalSummaryModel(Map<String, String> data) {
this.data = data;
keys = data.keySet().toArray(new String[data.size()]);
}

}

image

关于java - 如何将已排序的 Map 转换为 JTable 的二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36968736/

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