两个集合的并集,操作数组 Java
Union of two sets,manipulating arrays Java
思路是计算两个集合集合的并集,程序开始时会要求用户输入想要集合的长度,然后提示他分配集合中的元素,最后的思路是计算Union。
我已经到了最后,编译我的程序后只打印了第一组的元素,我真的不知道为什么。
所以我的问题是如何计算集合并集,按照我开始的想法。
我的输出:
Desired array lengths:
3
3
First set elements:
1
2
3
Second set elements:
4
5
6
UNION of our sets:
1.002.003
public class Union{
public static void main(String[] args) {
System.out.println("Desired array lengths: ");
Scanner scan = new Scanner(System.in);
//Infinite loop for reading input,if negative number entered break the loop!
while (true) {
int n1 = scan.nextInt();
int n2 = scan.nextInt();
if (n1 < 0 || n2 < 0)
break;
// Assigning elements to the first set.
double[] s1 = new double[n1];
System.out.println("First set elements: ");
//We enter elements as long as the number of the elements added is less than the length of an array we assigned.
for (int i = 0; i < n1; s1[i++] = scan.nextInt());
if (n1 == 0)
System.out.println();//If we do not enter any element go to new line
//Assigning elements to the second set.
double[] s2 = new double[n2];
System.out.println("Second set elements: ");
for (int i = 0; i < n2; s2[i++] = scan.nextInt());
if (n2 == 0)
System.out.println();//Same as before.
// Calculating union
double[] s3 = new double[n1 + n2];//We reserve memory space for the s3 array with the size equal to both n1 and n2 arrays.
int n3 = 0; // Variable used to save number of elements,after reaching the end of the loop n3 WILL HAVE THE SIZE OF N1.
while (n3 < n1)
s3[n3] = s1[n3++];
for (int j = 0; j < n2; j++) { //HERE WE ARE CHECKING IF THE ELEMENTS FROM N2 SET ARE PRESENT IN THE N1
int i = 0;
while (i < n1 && s2[j] == s1[i])
i++;
if (i == n1)
s3[n3++] = s2[j];
}
double[] pom = new double[n3];
for (int i = 0; i < n3; pom[i] = s3[i++]);
s3 = pom;
pom = null;
System.out.println("UNION of our sets: ");
for (int i = 0; i < n3; System.out.printf("%.2f", s3[i++]))
;
System.out.print("\n\n");
}
}
错误在于你检查set
s2
的哪些元素的代码,你需要输入set
s3
.
基本上你需要检查 set
s2
的元素是否存在于 set
s1
.
所以更改以下代码:
for (int j = 0; j < n2; j++) {
int i = 0;
while (i < n1 && s2[j] == s1[i])
i++;
if (i == n1)
s3[n3++] = s2[j];
}
此代码:
for (int j = 0; j < n2; j++) {
int i = 0;
while (i < n1 && s2[j] != s1[i])
i++;
if (i == n1)
s3[n3++] = s2[j];
}
只有当 set
s1
中的元素 none 与元素 [=24= 匹配时,循环 while (i < n1 && s2[j] != s1[i])
才会以 i = n1
结束] 这就是我们想要在 2 组的 UNION
中的元素。
你可以使用 Java Collections Framework? The Set interface 可以很容易地做到这一点。
Set<Double> union = new HashSet<>();
for (double e : s1)
union.add(e);
for (double e : s2)
union.add(e);
System.out.println(union);
如果您不能使用集合框架而必须自己滚动,那么您可以考虑滚动自己的二叉树或堆。
这是因为以下块:
while (i < n1 && s2[j] == s1[i])
i++;
if (i == n1)
它正在尝试比较具有相似索引的两个数组的元素,例如如果 s1
和 s2
的第一个元素不相等,控制将跳出 while
循环,因此,i
永远不会是 n1
,导致元素在 s2 的索引 j
处被跳过。
关于并集的计算,用Java中的Set
就可以轻松完成,例如:
Set<Double> elements = new LinkedHashSet<>();
for(double number : s1){
elements.add(number);
}
for(double number : s2){
elements.add(number);
}
double[] union = new double[elements.size()];
int i = 0;
Iterator<Double> iterator = elements.iterator();
while(iterator.hasNext()){
union[i++] = iterator.next();
}
思路是计算两个集合集合的并集,程序开始时会要求用户输入想要集合的长度,然后提示他分配集合中的元素,最后的思路是计算Union。 我已经到了最后,编译我的程序后只打印了第一组的元素,我真的不知道为什么。
所以我的问题是如何计算集合并集,按照我开始的想法。
我的输出:
Desired array lengths: 3 3 First set elements: 1 2 3 Second set elements: 4 5 6 UNION of our sets: 1.002.003
public class Union{
public static void main(String[] args) {
System.out.println("Desired array lengths: ");
Scanner scan = new Scanner(System.in);
//Infinite loop for reading input,if negative number entered break the loop!
while (true) {
int n1 = scan.nextInt();
int n2 = scan.nextInt();
if (n1 < 0 || n2 < 0)
break;
// Assigning elements to the first set.
double[] s1 = new double[n1];
System.out.println("First set elements: ");
//We enter elements as long as the number of the elements added is less than the length of an array we assigned.
for (int i = 0; i < n1; s1[i++] = scan.nextInt());
if (n1 == 0)
System.out.println();//If we do not enter any element go to new line
//Assigning elements to the second set.
double[] s2 = new double[n2];
System.out.println("Second set elements: ");
for (int i = 0; i < n2; s2[i++] = scan.nextInt());
if (n2 == 0)
System.out.println();//Same as before.
// Calculating union
double[] s3 = new double[n1 + n2];//We reserve memory space for the s3 array with the size equal to both n1 and n2 arrays.
int n3 = 0; // Variable used to save number of elements,after reaching the end of the loop n3 WILL HAVE THE SIZE OF N1.
while (n3 < n1)
s3[n3] = s1[n3++];
for (int j = 0; j < n2; j++) { //HERE WE ARE CHECKING IF THE ELEMENTS FROM N2 SET ARE PRESENT IN THE N1
int i = 0;
while (i < n1 && s2[j] == s1[i])
i++;
if (i == n1)
s3[n3++] = s2[j];
}
double[] pom = new double[n3];
for (int i = 0; i < n3; pom[i] = s3[i++]);
s3 = pom;
pom = null;
System.out.println("UNION of our sets: ");
for (int i = 0; i < n3; System.out.printf("%.2f", s3[i++]))
;
System.out.print("\n\n");
}
}
错误在于你检查set
s2
的哪些元素的代码,你需要输入set
s3
.
基本上你需要检查 set
s2
的元素是否存在于 set
s1
.
所以更改以下代码:
for (int j = 0; j < n2; j++) {
int i = 0;
while (i < n1 && s2[j] == s1[i])
i++;
if (i == n1)
s3[n3++] = s2[j];
}
此代码:
for (int j = 0; j < n2; j++) {
int i = 0;
while (i < n1 && s2[j] != s1[i])
i++;
if (i == n1)
s3[n3++] = s2[j];
}
只有当 set
s1
中的元素 none 与元素 [=24= 匹配时,循环 while (i < n1 && s2[j] != s1[i])
才会以 i = n1
结束] 这就是我们想要在 2 组的 UNION
中的元素。
你可以使用 Java Collections Framework? The Set interface 可以很容易地做到这一点。
Set<Double> union = new HashSet<>();
for (double e : s1)
union.add(e);
for (double e : s2)
union.add(e);
System.out.println(union);
如果您不能使用集合框架而必须自己滚动,那么您可以考虑滚动自己的二叉树或堆。
这是因为以下块:
while (i < n1 && s2[j] == s1[i])
i++;
if (i == n1)
它正在尝试比较具有相似索引的两个数组的元素,例如如果 s1
和 s2
的第一个元素不相等,控制将跳出 while
循环,因此,i
永远不会是 n1
,导致元素在 s2 的索引 j
处被跳过。
关于并集的计算,用Java中的Set
就可以轻松完成,例如:
Set<Double> elements = new LinkedHashSet<>();
for(double number : s1){
elements.add(number);
}
for(double number : s2){
elements.add(number);
}
double[] union = new double[elements.size()];
int i = 0;
Iterator<Double> iterator = elements.iterator();
while(iterator.hasNext()){
union[i++] = iterator.next();
}