Page 1 of 2 12 LastLast
Results 1 to 20 of 32

Thread: Array Merger

  1. #1
    Yaki is offline Member
    Join Date
    Sep 2012
    Posts
    16
    Rep Power
    0

    Default 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];
    }



    }

    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,367
    Blog Entries
    7
    Rep Power
    20

    Default Re: Array Merger

    Have you had a look at the TreeSet<T> class?

    kind regards,

    Jo
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Yaki is offline Member
    Join Date
    Sep 2012
    Posts
    16
    Rep Power
    0

    Default Re: Array Merger

    ohh i dont know what that is... im sorry

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Yaki is offline Member
    Join Date
    Sep 2012
    Posts
    16
    Rep Power
    0

    Default 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

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Array Merger

    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.
    Last edited by Norm; 09-02-2012 at 02:26 PM.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Yaki is offline Member
    Join Date
    Sep 2012
    Posts
    16
    Rep Power
    0

    Default 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

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Array Merger

    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?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Yaki is offline Member
    Join Date
    Sep 2012
    Posts
    16
    Rep Power
    0

    Default 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

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default 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?
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    Yaki is offline Member
    Join Date
    Sep 2012
    Posts
    16
    Rep Power
    0

    Default 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];
    }

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    Yaki is offline Member
    Join Date
    Sep 2012
    Posts
    16
    Rep Power
    0

    Default 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?

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Array Merger

    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.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    Yaki is offline Member
    Join Date
    Sep 2012
    Posts
    16
    Rep Power
    0

    Default Re: Array Merger

    yeah i have done that but how will apply that on codes ive been revising my codes but im going nowhere

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Array Merger

    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
    Last edited by Norm; 09-02-2012 at 04:35 PM.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    Yaki is offline Member
    Join Date
    Sep 2012
    Posts
    16
    Rep Power
    0

    Default Re: Array Merger

    hey i was able to merge it but look at this:
    Java 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]);
    		}
    
    
    
    	}
    
    }
    instead of getting;
    1,2,3,4,5,6,7,8,9,10
    as my answer .
    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..
    Last edited by Norm; 09-02-2012 at 04:51 PM. Reason: quote tag change to code

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Array Merger

    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.
    Last edited by Norm; 09-02-2012 at 04:59 PM.
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    Yaki is offline Member
    Join Date
    Sep 2012
    Posts
    16
    Rep Power
    0

    Default 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

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default 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?
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 15
    Last Post: 05-14-2012, 12:42 PM
  2. Replies: 0
    Last Post: 05-12-2011, 07:04 PM
  3. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  4. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •