Array Merger

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-02-2012, 01:35 PM
Yaki
Array Merger
Hey guys good evening
im new to java and here as well, hope you guys can help me with this:
so the thing is i want to combine 2 int arrays with a size of 5 so the total size would be 10

and put it in a String so that i can see it,
and later on try to compare and clear out the numbers that are common
example:
array1[5] = 1,2,3,4,5
array2=5,6,7,8,9

the numbers that you have inputted are: 1,2,3,4,5,5,6,7,8,9
After execution: 1,2,3,4,5,6,7,8,9

there hope you can help me guys this is what i got so far:
public static void main(String[] args) {
int[] firstArray ={1,2,3,4,5};
int[] secondArray ={6,7,8,9,10};
int first=0;
int second=0;
String combine="";
String una="";
String pang="";

for(int x=0;x<firstArray.length;x++){
una=firstArray[x];
}

}

}
• 09-02-2012, 02:19 PM
JosAH
Re: Array Merger
Have you had a look at the TreeSet<T> class?

kind regards,

Jo
• 09-02-2012, 02:44 PM
Yaki
Re: Array Merger
ohh i dont know what that is... im sorry
• 09-02-2012, 03:07 PM
Norm
Re: Array Merger
What have you tried so far? If you were to do it by hand how would you do it? Describe the steps needed to create a new array and copy the contents of two arrays to it.
• 09-02-2012, 03:10 PM
Yaki
Re: Array Merger
ok ..
ahmm first i have to have 2 sets of array

so
int[] firstArray = {1,2,3,4,5}
int[] secondArray = {5,6,7,8,9}

nxt is i have to combined them and this part is what i cant do in my mind
i thought that
id have to put it in a string but it wont do because im having an error
• 09-02-2012, 03:23 PM
Norm
Re: Array Merger
Quote:

nxt is i have to combined them
What are the steps to combine them?
First create an array large enough to hold both input arrays.
Then what?

Take a piece of paper and draw the three arrays. Draw arrows to the first source array and the target locations for the copying.
Copy from the source to the target locations and advance the arrows to the next location.
When one source is empty change the source arrow to the second array.
• 09-02-2012, 03:25 PM
Yaki
Re: Array Merger
first i have to create an array that will hold both of them,
then i have to compare each number if there are equal numbers
i guess id have to create an array that will save all of those numbers which is a unique number
so that at the end of the program i will be able to show the union of the arrays wthout showing the repeating number
• 09-02-2012, 03:43 PM
Norm
Re: Array Merger
Quote:

compare each number if there are equal numbers
Can you describe that in more detail? Where are the numbers that are being compared?
What will the code do if no equal number are found?
What will it do if equal numbers are found?
• 09-02-2012, 03:47 PM
Yaki
Re: Array Merger
ok sir
so first i have to combined this 2 arrays:
array1 = 1,2,3,4,5
array2 = 5,6,7,8,9
output: 1,2,3,4,5,5,6,7,8,9
after execution: 1,2,3,4,5,6,7,8,9

as you can see i should combined first the two arrays]
after that, i should check for the unique numbers (and on my idea) store them on another array so
that i will be able to show it
i no equal number will be found then thats ok
as long as the code will check it
if there are no equal numbers
then all of it will be the input
• 09-02-2012, 03:55 PM
Norm
Re: Array Merger
Are you going to do the project in two steps:
1) merge the two arrays into a third array
2) remove duplicate numbers from the third array
If not with these two steps, how are you thinking to do it? store them on another array so

For the first step, do you have the steps needed to create the merged array?
• 09-02-2012, 03:57 PM
Yaki
Re: Array Merger
well actually i do, ive been doing this for 3 hours but whatever modification i make doesnt make it work
first i thought of doing this:
public static void main(String[] args) {
int[] firstArray ={1,2,3,4,5};
int[] secondArray ={6,7,8,9,10};
int first=0;
int second=0;
String combine="";
String una="";
String pang="";

for(int x=0;x<firstArray.length;x++){
una=firstArray[x];
}
• 09-02-2012, 04:08 PM
Norm
Re: Array Merger
You need to work out the logic BEFORE writing any code. If the first part is to merge the two arrays:
What are the steps the program must do to merge the two arrays into one array. Make a list of the steps.

If the first part is not to merge the two arrays, please explain what you want to do first.
• 09-02-2012, 04:20 PM
Yaki
Re: Array Merger
the first thing i want to do first is to merge the two arrays and thats what i dont know how to do :( can u explain it a little more?
• 09-02-2012, 04:41 PM
Norm
Re: Array Merger
Quote:

first is to merge the two arrays
Take a piece of paper and draw the three arrays. Draw arrows to the first source array and the target array locations for the copying.
Copy from the source to the target locations and advance the arrows to the next locations after each copy.
When one source is empty change the source arrow to the second array and continue copying from the source to the target, advancing each arrow after the copy.
• 09-02-2012, 04:52 PM
Yaki
Re: Array Merger
yeah i have done that but how will apply that on codes ive been revising my codes but im going nowhere
• 09-02-2012, 05:31 PM
Norm
Re: Array Merger
Quote:

how will apply that on codes
Post the list of steps for doing the merge and work on writing the code for each step.
What is the first step in the list, what is the code to do that step?
Continue with all the steps in the list.

Also posted at: http://www.javaprogrammingforums.com...ay-merger.html
• 09-02-2012, 05:46 PM
Yaki
Re: Array Merger
hey i was able to merge it but look at this:
Code:

```import java.util.Scanner; public class Runner {         public static void main(String[] args) {                 int firstArray[] = {1,2,3,4,5};                 int secondArray[] = {6,7,8,9,10};                 int result[]= new int[10];                 int superResult[] = new int [10];                                 for(int x=0;x<firstArray.length;x++ ){                         if(firstArray[x] != firstArray[x+1]){                                 result[x] = firstArray[x];                                 if(x == 5){                                         if(firstArray[x] != firstArray[x]){                                                 result[3] = firstArray[4];                                         }                                 }                                                                                         }                 }                 for(int y=0;y<secondArray.length;y++ ){                         if(secondArray[y] != secondArray[y]){                                 result[y] = secondArray[y+1];                                 if(y == 5){                                         if(secondArray[y] != secondArray[y]){                                                 result[3] = secondArray[4];                                         }                                 }                                                                                         }                 }                 System.out.println("The numbers that you have inputted are: ");                 for(int r=0;r<result.length;r++ ){                         System.out.println(result[r]);                 }         } }```
1,2,3,4,5,6,7,8,9,10
im getting:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at app.runner.Runner.main(Runner.java:14)

and i know its because of this:
if(firstArray[x] != firstArray[x+1]){
and this:
result[y] = secondArray[y+1];

but i dont know any alternative way to
do it..
• 09-02-2012, 05:55 PM
Norm
Re: Array Merger
Quote:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
At line 14 the index goes past the end of the array. The max index for an array is the array length -1;

I thought you were going to do the project in two steps, first merge the two arrays, then remove duplicates from that array.
What does the posted code try to do?
You still have not worked out the logic before trying to write the code.

What are the if tests in the loops that do the copy for?
What are the hardcoded 3 and 4 as indexes for?

Work on the logic BEFORE writing any code. You need two loops, one for each source array.
• 09-02-2012, 06:14 PM
Yaki
Re: Array Merger
i have merged the two arrays, the thing is i cant do the second problem which is to remove the duplicates...
can you explain this to me?
sir im so sorry , im just practicing coz i really want to learn
• 09-02-2012, 06:18 PM
Norm
Re: Array Merger
Do you want to remove the duplicates in the merged array
or do you want to copy the unique elements from the merged array to a new array?
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last