现在正确打印内部但不区别!
public class setPractice {
public static Scanner kbd;
public static final int MAXSIZE = 20;
public static void main(String[] args) {
kbd = new Scanner(System.in);
int sizeA, sizeB, interSize, diffSize;
int[] setA;
int[] setB;
//int[] intersect;
int[] difference = new int[MAXSIZE] ;
//int[] resultSet = new int[MAXSIZE];
System.out.print("How many numbers will be in the 1st set: ");
sizeA = kbd.nextInt();
while (sizeA > MAXSIZE) {
System.out
.print("Error: Set size is too large. Re-enter set size: ");
sizeA = kbd.nextInt();
}
setA = new int[sizeA];
System.out.println("Enter list of integers for 1st set: ");
getData(setA, sizeA);
sort(setA, sizeA);
System.out.println("The ascending order for 1st is:");
print(setA, sizeA);
System.out.println("How many numbers will be in the 2nd set: ");
sizeB = kbd.nextInt();
while (sizeB > MAXSIZE) {
System.out
.print("Error: Set size is too large. Re-enter set size: ");
sizeB = kbd.nextInt();
}
setB = new int[sizeB];
System.out.println("Enter list of integers for 2nd set: ");
getData(setB, sizeB);
sort(setB, sizeB);
System.out.println("The ascending order for the 2nd set is:");
print(setB, sizeB);
int[] resultSet = new int[sizeA +sizeB];
interSize = intersection(setA, setB, sizeA, sizeB, resultSet);
System.out.println("The intersection of the two sets is: ");
for (int x = 0; x < interSize; x++) {
System.out.println(resultSet[x] + " ");
}
diffSize = difference(setA, sizeA, setB, sizeB, resultSet);
System.out.println("The difference of A-B is: ");
for (int x = 0; x < diffSize; x++) {
System.out.print(resultSet[x] + " ");
}
}
public static void getData(int[] set, int size) {
for (int x = 0; x < size; x++) {
int num = kbd.nextInt();
int count = search(set, size, num);
if (count == 0)
set[x] = num;
else
x--;
}
}
public static int search(int[] set, int size, int num) {
int count = 0;
for (int x = 0; x < size; x++) {
if (num == set[x])
count++;
}
return count;
}
public static int difference(int[] setA, int sizeA, int[] setB, int sizeB,
int[] resultSet) {
int count = 0;
boolean flag = true;
for(int i = 0; i<sizeA ; i++){
for(int j = 0; j<sizeB ; j++){
if(setA[i] == setB[j]){
flag =false;
break;
}
if(flag){
resultSet[count++] =setA[i];
}
}
}
return count;
}
public static void sort(int[] nums, int size) {
int temp;
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (nums[j] > nums[j + 1]) {
temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
}
public static void print(int[] nums, int size) {
for (int i = 0; i < nums.length; i++) {
//if (nums[i] != 0) {
System.out.println(nums[i]);
//}
}
}
public static int intersection(int[] setA, int[] setB, int sizeA,
int sizeB, int[] resultSet) {
int count = 0;
for(int i = 0; i <sizeA ; i++){
for(int j=0; j< sizeB ; j++){
if(setA[i] == setB[j]){
resultSet[count++]=setA[i];
break;
}
}
}
return count;
}
}
第一组中将有多少个数字:4
输入第一组的整数列表: 11 2 3 56
1st的升序是: 2 3 11 56
第二组中有多少个数字: 5
输入第二组的整数列表: 56 3 33 98 87
第二组的升序为: 3 33 56 87 98
两组的交集是: 3 56
A-B的区别在于: 0 0 0 0 0
使用的方法是使用这些参数给我们的 - 我们的工作是编写方法 使用给定的参数
答案 0 :(得分:0)
首先建议,不要发明热水:)。将数据移动到集合中并使用现有工具完成工作:commons collections。
答案 1 :(得分:0)
@ user3546001只是注释掉/删除以下条件
if (nums[i] != 0) in your print() function
你会得到你的答案,为什么这段代码不起作用......
如果仍然感到困惑,只需删除排序逻辑,该逻辑对数组进行排序,并在数组中首先放置0秒。
这就是为什么你得到的结果为零交叉....
我希望这会有所帮助...
答案 2 :(得分:0)
我改变了一些事情。但好的新功能是你的代码工作正常。您的数组只是填充了不必要的零
以您的情况为例
setA = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,12}
setB = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,3,6}
所以我改变的是这个
int[] setA; //from int[] setA = new int[MAXSIZE];
int[] setB; //from int[] setB = new int[MAXSIZE];
并在用户输入
后设置数组大小while(sizeA > MAXSIZE)
环
setA = new int[sizeA];
这使你的交叉路口工作,但没有区别。对我而言,它显示0为差异。
编辑: 打印setb后
print(setB, sizeB);
我还将所有其他数组设置为setA和setB
的最大值intersect = new int[Math.max(sizeA, sizeB)];
difference = new int[Math.max(sizeA, sizeB)];
resultSet = new int[Math.max(sizeA, sizeB)];
以及最重要的
int[] intersect; //from int[] intersect = new int[MAXSIZE];
int[] difference;//from int[] difference = new int[MAXSIZE];
int[] resultSet; //from int[] resultSet = new int[MAXSIZE];
自定义最大值方法:
pubic static int Max(int num1, int num2){
if(num1 > num2){
return num1;
}else{
return num2;
}
}
在代码中添加此内容,以便在任何给定时间都可以看到任何数组内容:
public static void printArray(int[] array){
for(int i: array){
System.out.print(i + " ");
}
System.out.println("");
}
最终编辑:
你的差异方法需要相当多的工作,但它已经解决了。凌乱,但它的工作原理。我以不同的方式重做您的程序,但我只是尝试修复您的程序。
public static int difference(int[] setA, int sizeA, int[] setB, int sizeB,
int[] resultSet) {
int count = 0;
boolean flag = true;
for(int i = 0; i<sizeA ; i++){
for(int j = 0; j<sizeB ; j++){
if(setA[i] == setB[j]){
flag =false;
break;
}
}
if(flag){
resultSet[count++] =setA[i];
}
flag = true;
}
for(int i = 0; i<sizeB ; i++){
for(int j = 0; j<count; j++){
if(setB[i] == resultSet[j]){
flag =false;
break;
}
}
for(int j = 0; j<sizeA ; j++){
if(setB[i] == setA[j]){
flag = false;
break;
}
}
if(flag){
resultSet[count++] =setB[i];
}
flag = true;
}
return count;
}
答案 3 :(得分:0)
@ user2408578
由于maxsize为20,因此填充数组末尾并打印出所有20个索引,包括未填充的索引
我做到了,现在我得到了这个
Enter list of integers for 1st set:
2
56
7
1st的升序是:
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
2
7
56