UNION OF TWO INTEGER ARRAYS... HELP?
Code :
public class array{
public static void main(String args []){
int list1[] = {12, 32, 14, 35, 89, 16, 120};
int list2[] = {9, 12, 8, 17, 120, 35, 36};
Union(list1, list2);
Intersection(list1, list2);
}
public static void Union(int list1[], int list2[]){
int unique[] = new int[15];
int counter = 1;
System.out.println("FOR UNION : ");
for(int i = 0; i < list1.length; i++){
unique[i] = list1[i];
counter++;
}
for(int j = 0; j < list2.length; j++){
unique[counter] = list2[j];
counter++;
}
int x = 0;
while(x < (unique.length-1)){
int tmp = unique[x];
int z = x + 1;
while(z < unique.length){
if(tmp == (unique[z])){
unique[z] = 0;
break;
}
z++;
}
x++;
}
for(int y = 0; y < unique.length; y++){
System.out.print("\t" + unique[y] + "\t");
}
\*int union[] = new int[20];
for(int y = 0; y < unique.length; y++){
if((unique[y]) == 0){
union[y] = unique[y+1];
}else{
union[y] = unique[y];
}
System.out.print("\t" + union[y] + "\t");
}*/
}
}
Ok in shorts if u run this...
It gives u 4 zeros at the wrong positions and I don't understand why...
if u unhide the last portion of the code it doesn't really replace the zeros with the following element...
AM TIRED AND WIRED AND HOPELESS SO THIS IS REALLY MY LAST RESORT :/
Re: UNION OF TWO INTEGER ARRAYS... HELP?
U can ignore the method Intersection(), it works fine...
Re: UNION OF TWO INTEGER ARRAYS... HELP?
Quote:
It gives u 4 zeros at the wrong positions and I don't understand why..
Are you saying the output from the program is wrong?
Can you post here what the output looks like and show us what you want it to look like.
With comments as necessary to help us understand the problem
Re: UNION OF TWO INTEGER ARRAYS... HELP?
Code :
public class array{
public static void main(String args []){
int list1[] = {12, 32, 14, 35, 89, 16, 120};
int list2[] = {9, 12, 8, 17, 120, 35, 36};
Union(list1, list2);
Intersection(list1, list2);
}
public static void Union(int list1[], int list2[]){
int unique[] = new int[15];
int counter = 1;
System.out.println("FOR UNION : ");
for(int i = 0; i < list1.length; i++){
unique[i] = list1[i];
counter++;
}
for(int j = 0; j < list2.length; j++){
unique[counter] = list2[j];
counter++;
}
int x = 0;
while(x < (unique.length-1)){
int tmp = unique[x];
int z = x + 1;
while(z < unique.length){
if(tmp == (unique[z])){
unique[z] = 0;
break;
}
z++;
}
x++;
}
for(int y = 0; y < unique.length; y++){
System.out.print("\t" + unique[y] + "\t");
}
int union[] = new int[20];
for(int y = 0; y < unique.length; y++){
if((unique[y]) == 0){
union[y] = unique[y+1];
}else{
union[y] = unique[y];
}
System.out.print("\t" + union[y] + "\t");
}
System.out.println("");
}
}
This is what it is supposed to give out:
For Union:
12 32 14 35 89 16 120 9 8 17 36
This is what it gives out:
FOR UNION :
12 32 14 35 89 16 120 0 9 0 8 17 0 0 36 12 32 14 35 89 16 120 9 9 8 8 17 0 36 36
P.S. I appreciate your help again...
Re: UNION OF TWO INTEGER ARRAYS... HELP?
Try debugging your code by printing out the contents of the arrays as you work with them.
An easy way to do that is the Arrays toString method:
System.out.println("B unique=" + java.util.Arrays.toString(unique));
You'll see some of your errors when you look at the print out.
Re: UNION OF TWO INTEGER ARRAYS... HELP?
I already checked the first 2 for loops and the print the right data... (which add both arrays into one new array)
but starting int x = 0;
and the two while loops inside each other which are meant to convert one of two identical elements into 0 so I can remove it
then print it (using 4th for loop)...
Re: UNION OF TWO INTEGER ARRAYS... HELP?
Quote:
print the right data... (add both arrays into one new array
That's strange. When I printed out the combined arrays, I saw bad data.
Are you certain that the concatenation works?
Re: UNION OF TWO INTEGER ARRAYS... HELP?
ok u were right I should've started the counter with 0 instead of one...
and i changed the length of both arrays union and unique to 14...
but now it gives me
FOR UNION :
12 32 14 35 89 16 120 9 8 8 17 0 36 36
Re: UNION OF TWO INTEGER ARRAYS... HELP?
Quote:
changed the length of both arrays union and unique to 14...
It would be better if you used the sum of lengths of the two arrays instead of hardcoding 14.
Re: UNION OF TWO INTEGER ARRAYS... HELP?
The altering at the end is not much of an issue to me as much as getting the right output...
everything goes right till the last for loop...
I'm trying to copy the whole array into a new one without the 0s so I can't use .copy()...
I applied the for loop
[CODE]
int union[] = new int[15]; // 15 or 14 not sure;
for(int y = 0; y < unique.length; y++){
if((unique[y]) == 0){
union[y] = unique[y+1];
}else{
union[y] = unique[y];
}
System.out.print("\t" + union[y] + "\t");
}
[\CODE]
Re: UNION OF TWO INTEGER ARRAYS... HELP?
Quote:
everything goes right till the last for loop...
Please explain.
Re: UNION OF TWO INTEGER ARRAYS... HELP?
The last for loop is supposed to convert unique[]
12 32 14 35 89 16 120 9 0 8 17 0 0 36
to union[]
12 32 14 35 89 16 120 9 8 17 36
Re: UNION OF TWO INTEGER ARRAYS... HELP?
If you are working with arrays and you want to have the final array be exactly the correct length,
you will need to make two passes over the array that has the entries to be removed:
One to count the elements to be copied which you will use to create the final array size.
And then you will need a second pass to copy the elements.
You can get the size of the final array by counting the number of elements that were zero'd and using that to compute the final array size.
Re: UNION OF TWO INTEGER ARRAYS... HELP?
Outside the bounds of array length discussion...
Is there anyway to create an array an put in it the values of another array without the zeros...
P.S. I know am getting on ur nerves but my exam is next Thursday and arrays are killing me... :/
Re: UNION OF TWO INTEGER ARRAYS... HELP?
Quote:
Is there anyway to create an array an put in it the values of another array without the zeros...
The default value of an element in an int array is zero. If you don't change an element's value, it will be zero. A zero is a valid int array value.
When copying an array's contents, your logic can skip over the zero's in the source array.
arrays are a basic/simple structure, unlike a class where you can have a method that will: Remove all the elements with values = xxx
Re: UNION OF TWO INTEGER ARRAYS... HELP?
Thanks for your help NORM :D
U are a great MAN :D
Re: UNION OF TWO INTEGER ARRAYS... HELP?
Thanks for the kind words.
Good luck on the exam.