1)使用列表框显示集合元素(升序),输入或产生随机数序列作为集合元素,声明产生/机数序列的成员方法;指定元素范围;删除列表框选中的多个元素。
2)输入多个集合,个数可变,提供集合的并、交、差运算并显示运算结果。
3) 采用整数文件保存集合元素,提供文件读写操作。
https://blog.csdn.net/AwayFuture/article/details/80471214
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.util.TreeSet;
public class SetOperation {
HashMap allSet = new HashMap<>();
Scanner read =
new Scanner(System.in);
class Set{
String name;
TreeSet data = new TreeSet<>();
}
public void creatSet(){
System.out.println("输入集合名和元素的个数");
Set temp = new Set();
temp.name = read.next();
int size = read.nextInt();
System.out.println("输入元素");
for(int i=0;i
int dataTemp = read.nextInt();
temp.data.add(dataTemp);
}
allSet.put(temp.name, temp);
System.out.println(temp.name +temp.data.toString());
System.out.println("集合创建成功");
}
public Set getSet(){
System.out.println("输入集合名");
String name = read.next();
return allSet.get(name);
}
public void showSet(){
Set temp = getSet();
System.out.println(temp.name +temp.data.toString());
System.out.println("集合查询成功");
}
public void showAllSet(){
System.out.println("所有的集合名为。。。");
String name = allSet.keySet().toString();
System.out.println(name);
}
public void operation(){
TreeSet result = new
TreeSet<>();
int operation;
System.out.println("输入你的操作
(1.交
2.并
3.差
4.包含
5 属于 )" );
operation = read.nextInt();
switch (operation) {
case 1: result =jiao();System.out.println(result.toString());
break;
case 2: result
=bing();System.out.println(result.toString());
break;
case 3: result
=cha();System.out.println(result.toString());
break;
case 4: baohan();
break;
case 5:shuyu();
break;
default:System.out.println("wrong input");
break;
}
}
public TreeSet jiao(){
Set a = getSet();
Set b = getSet();
TreeSet data = new TreeSet<>();
TreeSet temp = new TreeSet<>();
temp.addAll(a.data);
while(!temp.isEmpty()){
int x=temp.pollFirst();
if(b.data.contains(x)){
data.add(x);
}
}
return data;
}
public TreeSet bing(){
Set a = getSet();
Set b = getSet();
TreeSet data = new TreeSet<>();
data.addAll(a.data);
data.addAll(b.data);
return data;
}
public TreeSet cha(){
Set a = getSet();
Set b = getSet();
TreeSet data = new TreeSet<>();
TreeSet temp = new TreeSet<>();
temp.addAll(a.data);
while(!temp.isEmpty()){
int x=temp.pollFirst();
if(b.data.contains(x)!=true){
data.add(x);
}
}
return data;
}
public void baohan(){
Set a = getSet();
Set b = getSet();
TreeSet temp = new TreeSet<>();
temp.addAll(a.data);
while(!temp.isEmpty()){
int x=temp.pollFirst();
if (!b.data.contains(x)) {
System.out.println(a.name+"不包含于"+b.name);
return ;
}
}
System.out.println(a.name+"包含于"+b.name);
}
public void shuyu(){
Set a = getSet();
System.out.println("输入元素");
int x=read.nextInt();
if(a.data.contains(x)){
System.out.println(x+"属于"+a.name);
}
else{
System.out.println(x+"不属于"+a.name);
}
}
public static void main(String[] args){
SetOperation operation = new SetOperation();
Scanner read = new Scanner(System.in);
System.out.println("输入你的操作: 1.创建集合
2.查询单个集合 3.查询所有集合 4.集合运算
5.退出系统");
int x;
do {
x=read.nextInt();
switch (x) {
case 1:operation.creatSet();
break;
case 2:operation.showSet();
break;
case 3:operation.showAllSet();
break;
case 4:operation.operation();
default:
break;
}
} while (x!=5);
}
}
实现这些功能的一些具体步骤:
1)使用列表框显示集合元素
1.使用 Java 的 Swing 包或者 JavaFX 包来创建一个 GUI 窗体
2.在窗体中添加一个列表框,用来显示集合元素
3.使用文本框或者按钮来输入或者生成随机数序列,并将这些数添加到列表框中
4.使用按钮来触发删除列表框中选中的多个元素的操作
2)输入多个集合并提供集合运算
1.在窗体中添加多个列表框,用来输入多个集合
2.使用按钮来触发集合的并、交、差运算
3.使用文本框或者另一个列表框来显示运算结果
3)使用整数文件保存集合元素并提供文件读写操作
1.使用文本框来输入文件名
2.使用 Java 的文件输入/输出流 (I/O Stream) 来实现文件读写操作
3.使用按钮来触发文件读写操作
这些步骤是实现这个 GUI 的一个大致框架
步骤:
1)使用Java图形用户界面(GUI)工具创建窗口并添加列表框,输入文本框和按钮。
2)在窗口中创建集合类,并声明存储集合元素的数组。
3)编写产生随机数序列的成员方法,使用Java的Random类生成随机数,并按指定的范围添加到数组中。
4)编写方法将集合元素显示在列表框中,并按升序排序。
5)在按钮的事件处理程序中编写代码删除列表框中选中的多个元素。
6)编写方法输入多个集合,使用用户输入的数据构建集合并添加到数组中。
7)编写集合并、交、差运算的方法,使用Java的HashSet类实现运算。
8)编写文件读写操作方法,使用Java的FileInputStream和FileOutputStream类读写整数文件。
9)在窗口中添加按钮,使用事件处理程序调用集合运算方法并显示结果。
10)运行程序,按要求进行集合运算并保存结果。
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
public class SetOperations extends JFrame implements ActionListener {
// 声明列表框、输入文本框、按钮等组件
private JList<Integer> setList;
private DefaultListModel<Integer> setModel;
private JTextField inputField;
private JButton addButton, deleteButton, randomButton, unionButton, intersectButton, differenceButton, readButton, writeButton;
// 声明存储集合元素的数组
private ArrayList<Integer> setElements;
public SetOperations() {
super("集合运算");
// 初始化数组
setElements = new ArrayList<>();
// 初始化列表框和列表框模型
setModel = new DefaultListModel<>();
setList = new JList<>(setModel);
setList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
// 初始化输入文本框
inputField = new JTextField();
// 初始化按钮
addButton = new JButton("添加元素");
deleteButton = new JButton("删除元素");
randomButton = new JButton("产生随机数序列");
unionButton = new JButton("并集");
intersectButton = new JButton("交集");
differenceButton = new JButton("差集");
readButton = new JButton("读取文件");
writeButton = new JButton("保存文件");
// 设置布局管理器
setLayout(new BorderLayout());
// 添加组件到窗口
add(new JScrollPane(setList), BorderLayout.CENTER);
add(inputField, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel(new GridLayout(2, 4));
buttonPanel.add(addButton);
buttonPanel.add(deleteButton);
buttonPanel.add(randomButton);
buttonPanel.add(unionButton);
buttonPanel.add(intersectButton);
buttonPanel.add(differenceButton);
buttonPanel.add(readButton);
buttonPanel.add(writeButton);
add(buttonPanel, BorderLayout.SOUTH);
// 添加事件监听器
addButton.addActionListener(this);
deleteButton.addActionListener(this);
randomButton.addActionListener(this);
unionButton.addActionListener(this);
intersectButton.addActionListener(this);
differenceButton.addActionListener(this);
readButton.addActionListener(this);
writeButton.addActionListener(this);
}
// 处理按钮点击事件
public void actionPerformed(ActionEvent e) {
// 获取事件源
Object source = e.getSource();
// 根据事件源调用不同的方法
if (source == addButton) {
addElement();
} else if (source == deleteButton) {
deleteElements();
} else if (source == randomButton) {
generateRandomElements();
} else if (source == unionButton) {
setUnion();
} else if (source == intersectButton) {
setIntersection();
} else if (source == differenceButton) {
setDifference();
} else if (source == readButton) {
readFile();
} else if (source == writeButton) {
writeFile();
}
}
// 添加元素方法
private void addElement() {
// 获取输入文本框中的值
String input = inputField.getText().trim();
// 如果输入的是空字符串则不做任何操作
if (input.equals("")) return;
// 将输入的值添加到数组中
setElements.add(Integer.parseInt(input));
// 更新列表框
updateList();
// 清空输入文本框
inputField.setText("");
}
// 删除元素方法
private void deleteElements() {
// 获取列表框中选中的元素的索引
int[] indices = setList.getSelectedIndices();
// 从数组中删除选中的元素
for (int i = indices.length - 1; i >= 0; i--) {
setElements.remove(indices[i]);
}
// 更新列表框
updateList();
}
// 产生随机数序列方法
private void generateRandomElements() {
// 弹出对话框获取范围和数量
String input = JOptionPane.showInputDialog(this, "请输入范围和数量(格式为:范围,数量)", "产生随机数序列", JOptionPane.PLAIN_MESSAGE);
if (input == null || input.equals("")) return;
// 将输入的值解析为范围和数量
String[] inputValues = input.split(",");
int range = Integer.parseInt(inputValues[0]);
int count = Integer.parseInt(inputValues[1]);
// 使用Java的Random类生成随机数并添加到数组中
Random rand = new Random();
for (int i = 0; i < count; i++) {
setElements.add(rand.nextInt(range));
}
// 更新列表框
updateList();
}
// 集合并运算方法
private void setUnion() {
// 弹出对话框获取多个集合
String input = JOptionPane.showInputDialog(this, "请输入多个集合(格式为:集合1,集合2,集合3...)", "集合并运算", JOptionPane.PLAIN_MESSAGE);
if (input == null || input.equals("")) return;
// 将输入的值解析为多个集合
String[] inputSets = input.split(",");
// 使用Java的HashSet类实现并集运算
HashSet<Integer> unionSet = new HashSet<>(setElements);
for (String s : inputSets) {
HashSet<Integer> set = new HashSet<>();
String[] setElements = s.split(" ");
for (String e : setElements) {
set.add(Integer.parseInt(e));
}
unionSet.addAll(set);
}
// 将结果保存到数组中
setElements = new ArrayList<>(unionSet);
// 更新列表框
updateList();
}
// 集合交运算方法
private void setIntersection() {
// 弹出对话框获取多个集合
String input = JOptionPane.showInputDialog(this, "请输入多个集合(格式为:集合1,集合2,集合3...)", "集合交运算", JOptionPane.PLAIN_MESSAGE);
if (input == null || input.equals("")) return;
// 将输入的值解析为多个集合
String[] inputSets = input.split(",");
// 使用Java的HashSet类实现交集运算
HashSet<Integer> intersectSet = new HashSet<>(setElements);
for (String s : inputSets) {
HashSet<Integer> set = new HashSet<>();
String[] setElements = s.split(" ");
for (String e : setElements) {
set.add(Integer.parseInt(e));
}
intersectSet.retainAll(set);
}
// 将结果保存到数组中
setElements = new ArrayList<>(intersectSet);
// 更新列表框
updateList();
}
// 集合差运算方法
private void setDifference() {
// 弹出对话框获取多个集合
String input = JOptionPane.showInputDialog(this, "请输入多个集合(格式为:集合1,集合2,集合3...)", "集合差运算", JOptionPane.PLAIN_MESSAGE);
if (input == null || input.equals("")) return;
// 将输入的值解析为多个集合
String[] inputSets = input.split(",");
// 使用Java的HashSet类实现差集运算
HashSet<Integer> differenceSet = new HashSet<>(setElements);
for (String s : inputSets) {
HashSet<Integer> set = new HashSet<>();
String[] setElements = s.split(" ");
for (String e : setElements) {
set.add(Integer.parseInt(e));
}
differenceSet.removeAll(set);
}
// 将结果保存到数组中
setElements = new ArrayList<>(differenceSet);
// 更新列表框
updateList();
}
// 读取文件方法
private void readFile() {
// 弹出对话框获取文件路径
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showOpenDialog(this);
if (result != JFileChooser.APPROVE_OPTION) return;
File file = fileChooser.getSelectedFile();
// 使用Java的Scanner类读取文件内容
try {
Scanner input = new Scanner(file);
setElements = new ArrayList<>();
while (input.hasNextInt()) {
setElements.add(input.nextInt());
}
// 更新列表框
updateList();
// 关闭文件
input.close();
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(this, "文件不存在", "错误", JOptionPane.ERROR_MESSAGE);
}
}
// 保存文件方法
private void writeFile() {
// 弹出对话框获取文件路径
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showSaveDialog(this);
if (result != JFileChooser.APPROVE_OPTION) return;
File file = fileChooser.getSelectedFile();
// 使用Java的PrintWriter类写入文件内容
try {
PrintWriter output = new PrintWriter(file);
for (int element : setElements) {
output.println(element);
}
// 关闭文件
output.close();
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(this, "文件不存在", "错误", JOptionPane.ERROR_MESSAGE);
}
}
// 更新列表框方法
private void updateList() {
// 清空列表框模型
setModel.clear();
// 将数组中的元素按升序添加到列表框模型
Collections.sort(setElements);
for (int element : setElements) {
setModel.addElement(element);
}
}
public static void main(String[] args) {
SetOperationsFrame frame = new SetOperationsFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}