Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: UNION OF TWO INTEGER ARRAYS... HELP?

1. ## UNION OF TWO INTEGER ARRAYS... HELP?

```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 :/

2. ## Re: UNION OF TWO INTEGER ARRAYS... HELP?

U can ignore the method Intersection(), it works fine...

3. ## Re: UNION OF TWO INTEGER ARRAYS... HELP?

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

4. ## Re: UNION OF TWO INTEGER ARRAYS... HELP?

```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...

5. ## 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.

6. ## 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)...

7. ## Re: UNION OF TWO INTEGER ARRAYS... HELP?

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?

8. ## 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

9. ## Re: UNION OF TWO INTEGER ARRAYS... HELP?

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.

10. ## 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]

11. ## Re: UNION OF TWO INTEGER ARRAYS... HELP?

everything goes right till the last for loop...

12. ## 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

13. ## 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.

14. ## 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... :/

15. ## Re: UNION OF TWO INTEGER ARRAYS... HELP?

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

16. ## The Following User Says Thank You to Norm For This Useful Post:

Medo Almasry (July 23rd, 2011)