Results 1 to 6 of 6

Thread: merging arrays

  1. #1
    ehca87 is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default merging arrays

    hi,
    i have a problem with merging 2 arrays into a third and display this in a ascending order. this is what i've done yet:
    Java Code:
    public class ArrayMerge {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    
    		int anzWerte = 10;
    		int[] feld1 = new int[anzWerte];
    		for (int i = 0; i < feld1.length; i++) {
    			feld1[i] = 3 * i;
    			System.out.print(feld1[i] + " ");
    		}
    
    		System.out.print("\n");
    		int anzWerte2 = 10;
    		int[] feld2 = new int[anzWerte2];
    		for (int j = 0; j < feld2.length; j++) {
    			feld2[j] = 1 * j;
    			System.out.print(feld2[j] + " ");
    		
    		}
    		System.out.print("\n");
    		int[] mergeArray = new int[feld1.length + feld2.length];
    		int k = 0;
    		int l = 0;
    		for (int i = 0; i < mergeArray.length; i++) {
    			if (feld1[k] <= feld2[l]) {
    				mergeArray[i] = feld1[k];
    				if (k < feld1.length - 1) {
    					k++;
    				} else {
    					if (i < mergeArray.length - 1) {
    						mergeArray[i] = feld1[k];
    					} else {
    						mergeArray[i] = feld2[l];
    					}
    
    				}
    			} else {
    				mergeArray[i] = feld2[l];
    				if (l < feld2.length - 1) {
    					l++;
    				} else {
    					if (i < mergeArray.length - 1) {
    						mergeArray[i] = feld1[k];
    					} else {
    						mergeArray[i] = feld2[l];
    					}
    				}
    			}
    			System.out.print(mergeArray[i]+ " ");
    
    		}
    	}
    }
    console-->
    0 3 6 9 12 15 18 21 24 27
    0 1 2 3 4 5 6 7 8 9
    0 0 1 2 3 3 4 5 6 6 7 8 9 12 12 12 12 12 12 9


    does anyone know what is wrong? what do i have to do that the numbers 12,15,18,21,24,27 are displayed in the third line?
    i use java for about a week now and don't have much knowledge about it.
    sorry for my english:) and thanks for help
    Last edited by ehca87; 11-12-2009 at 02:55 PM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Is this homework with some special restrictions?
    Why not just add all the values into an ArrayList and then sort the resultant list?

  3. #3
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Does it have to be that complicated, or will this do?
    Java Code:
    int[] mergeArray  = new int[feld1.length+ feld2.length];
    System.arraycopy(feld1, 0, mergeArray , 0, feld1.length);
    System.arraycopy(feld2, 0, mergeArray , feld1.length, feld2.length);
    Arrays.sort(mergeArray );
    for (int i : mergeArray ) {
    	System.out.println(i);
    }
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  4. #4
    ehca87 is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default

    it has to be that complicated, but thanks for idea!

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    In that case then it helps to lay out all the rules of what's allowed and what's not and write down some steps first and before starting the code.
    You can still break down the steps into two parts.
    Merge - Simply copies values from all arrays into a fourth array.
    Sort - Sorts the fourth array in descending order.

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Quote Originally Posted by ehca87 View Post
    it has to be that complicated, but thanks for idea!
    Ok, your problems start as soon as you've reached the end of one array, i.e. feld1.
    You could check whether youve reached the end of feld1 and insert from feld2, next you check whether you've reached end of feld2 and insert from feld1. If both is not the case you check feld1[k] <= feld2[j] and insert accordingly.
    Use if- else ifs.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. Replies: 0
    Last Post: 11-03-2009, 12:36 PM
  2. Arrays
    By swim_fan08 in forum New To Java
    Replies: 7
    Last Post: 04-11-2009, 04:02 PM
  3. Merging XML
    By KEVINR in forum XML
    Replies: 5
    Last Post: 01-21-2009, 09:09 PM
  4. merging selected table cells using javascript
    By Renjini in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 06-17-2008, 02:04 AM
  5. Merging Ideas
    By CompleteBeginner in forum New To Java
    Replies: 1
    Last Post: 05-19-2008, 03:15 PM

Posting Permissions

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