编写一个Java应用程序,对于给定的一个字符串的集合,格式如:
{aaa bbb ccc}, {bbb ddd},{eee fff},{ggg},{ddd hhh}
要求将其中交集不为空的集合合并,要求合并完成后的集合之间无交集,例如上例应输出:
{aaa bbb ccc ddd hhh},{eee fff}, {ggg}
请将制作好的源文件保存为“t1.java”。(本题60分,要求1占20分,要求2占35分,要求3占5分)
(1)分析问题,描述你解决这个问题的思路、处理流程,以及算法复杂度。
(2)编程实现题目要求的集合合并。
(3)描述可能的改进(改进的方向如效果,算法复杂度,性能等等)。 import java.util.*;public class UnionTest
{
public static final int setLength = 4;
public static void init(Map<String, Integer> map, String[] set, int setNo){
for (String s : set){
Integer no = map.get(s);
if (no == null){
map.put(s, setNo);
} else{
for (String element : set){
map.put(element, no);
}
break;
}
}
}
/**
* @param args
*/
public static void main(String[] args)
{
String[] set1 = new String[]{"aaa", "bbb", "ccc"};
String[] set2 = new String[]{ "bbb", "ddd" };
String[] set3 = new String[]{ "eee", "fff" };
String[] set4 = new String[]{ "ggg" };
String[] set5 = new String[]{ "ddd", "hhh" };
Map<String, Integer> map = new HashMap<String, Integer>();
init(map, set1, 1);
init(map, set2, 2);
init(map, set3, 3);
init(map, set4, 4);
init(map, set5, 5);
Map<Integer, Set<String>> list = new HashMap<Integer, Set<String>>();
for (String s : map.keySet()){
int no = map.get(s);
Set<String> set = list.get(no);
if (set == null) {
set = new HashSet<String>();
list.put(no, set);
}
set.add(s);
}
for (int index : list.keySet()) {
System.out.println("Index" + index);
UnionTest.printSet(list.get(index));
}
}
public static void printSet(Set<String> set){
for (String s : set){
System.out.print(s + " ");
}
System.out.println();
}
}求大家帮帮忙,分析一下执行过程,小弟初学java,很困难,尤其上边if语句,
if (set == null) {
set = new HashSet<String>();
list.put(no, set);
}
set.add(s);
如果set为空,还执行set.add(s);吗?
另外到底怎么把几个集合合并起来的?
{aaa bbb ccc}, {bbb ddd},{eee fff},{ggg},{ddd hhh}
要求将其中交集不为空的集合合并,要求合并完成后的集合之间无交集,例如上例应输出:
{aaa bbb ccc ddd hhh},{eee fff}, {ggg}
请将制作好的源文件保存为“t1.java”。(本题60分,要求1占20分,要求2占35分,要求3占5分)
(1)分析问题,描述你解决这个问题的思路、处理流程,以及算法复杂度。
(2)编程实现题目要求的集合合并。
(3)描述可能的改进(改进的方向如效果,算法复杂度,性能等等)。 import java.util.*;public class UnionTest
{
public static final int setLength = 4;
public static void init(Map<String, Integer> map, String[] set, int setNo){
for (String s : set){
Integer no = map.get(s);
if (no == null){
map.put(s, setNo);
} else{
for (String element : set){
map.put(element, no);
}
break;
}
}
}
/**
* @param args
*/
public static void main(String[] args)
{
String[] set1 = new String[]{"aaa", "bbb", "ccc"};
String[] set2 = new String[]{ "bbb", "ddd" };
String[] set3 = new String[]{ "eee", "fff" };
String[] set4 = new String[]{ "ggg" };
String[] set5 = new String[]{ "ddd", "hhh" };
Map<String, Integer> map = new HashMap<String, Integer>();
init(map, set1, 1);
init(map, set2, 2);
init(map, set3, 3);
init(map, set4, 4);
init(map, set5, 5);
Map<Integer, Set<String>> list = new HashMap<Integer, Set<String>>();
for (String s : map.keySet()){
int no = map.get(s);
Set<String> set = list.get(no);
if (set == null) {
set = new HashSet<String>();
list.put(no, set);
}
set.add(s);
}
for (int index : list.keySet()) {
System.out.println("Index" + index);
UnionTest.printSet(list.get(index));
}
}
public static void printSet(Set<String> set){
for (String s : set){
System.out.print(s + " ");
}
System.out.println();
}
}求大家帮帮忙,分析一下执行过程,小弟初学java,很困难,尤其上边if语句,
if (set == null) {
set = new HashSet<String>();
list.put(no, set);
}
set.add(s);
如果set为空,还执行set.add(s);吗?
另外到底怎么把几个集合合并起来的?
public static void main(String[] args)
{
String[] strArr1 = new String[]{"aaa", "bbb", "ccc"}; //String[] set//这种命名我看着超变扭 ,你那样命名别人还以为是Set类型的呢
String[] strArr2 = new String[]{ "bbb", "ddd" };
String[] strArr3 = new String[]{ "eee", "fff" };
String[] strArr4 = new String[]{ "ggg" };
String[] strArr5 = new String[]{ "ddd", "hhh" };
Map <String, Integer> map = new HashMap <String, Integer>();
init(map, strArr1, 1);//把数组和下标作为一个键值对存到Map中
init(map, strArr2, 2);
init(map, strArr3, 3);
init(map, strArr4, 4);
init(map, strArr5, 5);
Map <Integer, Set <String>> setMap = new HashMap <Integer, Set <String>>();//Map<>list又乱命名,它是map不是list
for (String s : map.keySet()){
int no = map.get(s);
Set <String> set = setMap.get(no);
if (set == null) {//查看setMap里是否有与no相对应的元素,如果没有新建
set = new HashSet <String>();//新建 set,原来的它为空
setMap.put(no, set);//往setMap中存元素
}
set.add(s);//往set里加String s,前面的if语句是保证这个set必顺存在,不存在new
}
for (int index : setMap.keySet()) {
System.out.println("Index" + index);
UnionTest.printSet(setMap.get(index));
}
}
还是执行的.因为if的控制范围只有{}所包含的区域.很明显,set.add()不是{}之内.所以仍然运行.