Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default How to add a merge array from 2 others arrays

    Hi, Please I need your help.
    I am asking for advice and help on:
    (I already try many times but always one of the arrays became empty)

    How to add a merge array from 2 others arrays and print it.
    This is the code I have written:


    Java Code:
    //////////////////////////////////////////////////////
    public class OrderedApp
       {
       public static void main(String[] args)
          {
          int maxSize = 25;             // array size
          OrdArray arr;                  // reference to array
          arr = new OrdArray(maxSize);   // create the array
    
          int maxSize1 = 25;                  // arr1ay size
          OrdArray arr1;                    // reference to arr1ay
          arr1 = new OrdArray(maxSize1);    // create the arr1ay
     
          arr.insert(77);     // inserts in array 1          
          arr.insert(13);
          arr.insert(13);
          arr.insert(33);
        		  
          arr1.insertb(47);   // inserts in array 2           
          arr1.insertb(89);
          arr1.insertb(69);
          arr1.insertb(32);
          arr1.insertb(99);
    
          System.out.print("array 1 \n");
          arr.display();                 // display items again
          System.out.print("===================================================\n");
    
          System.out.print("array 2 \n");      
          arr1.display();                 // display items 
          System.out.print("===================================================\n");
    
     
    
          }
       }
    ////////////////////////////////////////////////////////////////////
    
    // inside this class add a third array which merge arr and arr1
    // and print it
    //
    
    class OrdArray
       {
       private static long[] a;          // ref to array a
       private static int nElemsa;       // number of data items
    
       private long[] b;                // ref to array b
       private int nElemsb;             // number of data items
    
       //-----------------------------------------------------------
       public OrdArray(int max)          // constructor
          {
          a = new long[max];             // create array
          nElemsa = 0;
    
          b = new long[max];             // create array
          nElemsb = 0;
          }
    
       //-----------------------------------------------------------
       public int size()
          { return nElemsa; }
    
       //-----------------------------------------------------------
       public void insert(long value)     // put element into array
          {
          int j;
          for(j=0; j<nElemsa; j++)        // find where it goes
             if(a[j] > value)             // (linear search)
                break;
          for(int k=nElemsa; k>j; k--)    // move bigger ones up
             a[k] = a[k-1];
          a[j] = value;                   // insert it
          nElemsa++;                      // increment size
          }  // end insert()
       //-----------------------------------------------------------
    
       public void insertb(long value)     // put element into array
          {
          int j;
          for(j=0; j<nElemsb; j++)         // find where it goes
             if(b[j] > value)              // (linear search)
                break;
          for(int k=nElemsb; k>j; k--)     // move bigger ones up
             b[k] = b[k-1];
          b[j] = value;                    // insert it
          nElemsb++;                       // increment size
          }  // end insert()
    
       //-----------------------------------------------------------
       public static void display()             // displays array contents
          {
    	
          for(int j=0; j<nElemsa; j++)    	   // for each element,
          {
             System.out.print(a[j] + " ");     // display it
    
          }
             System.out.println(""); 
    
          }
       //-----------------------------------------------------------
        
    // here I added a merge method called from main to read array a and array b
    // but when array a has info the array b is empty, and viceveresa
    
    ////////////////////////////////////////////////////////////////
    
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default Re: How to add a merge array from 2 others arrays

    What do you see?
    What do you expect to see?
    Any exceptions, including where they are thrown from?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,304
    Rep Power
    20

    Default Re: How to add a merge array from 2 others arrays

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Re: How to add a merge array from 2 others arrays

    Quote Originally Posted by Tolls View Post
    What do you see?
    What do you expect to see?
    Any exceptions, including where they are thrown from?
    I already have spent 30 hours trying everything I found on internet and still not working.
    and this is the result that the program should get
    array 1 after sorting
    13 33 77
    ================================================== =
    array 2 after sorting
    32 47 69 89 99
    ================================================== =
    Merged Array is:
    13 32 33 47 69 77 89 99
    ================================================== =
    following is the code that seems to be ok
    Java Code:
       public int[] merge(int[]... arr)   
     {   
           int arrSize = 0;   
           for (int[] array : arr)    
       {   
            arrSize += array.length;   
       }   
           int[] result = new int[arrSize];   
           int j = 0;    
         for (int[] array : arr)   
     {    
           for (int s : array)   
     {     
          result[j++] = s;   
                              
     }                  
     }                  
     return result;      
       }   
      
    // from main i used
      OrdArray arrC;                  // reference to array   
         arrC = new OrdArray(maxSize);   // create the array   
    int M[] = (int[]) arrC.merge(arr, arr1);  
    
      OrdArray arrC;                  // reference to array
         arrC = new OrdArray(maxSize);   // create the array
    int M[] = (int[]) arrC.merge(arr, arr1);

Similar Threads

  1. How do you make an array of arrays?
    By mrjaeyun in forum New To Java
    Replies: 1
    Last Post: 11-14-2011, 12:34 AM
  2. Array of Arrays all unique elements
    By dusker in forum New To Java
    Replies: 9
    Last Post: 04-02-2011, 07:21 PM
  3. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 10:32 AM
  4. Help with arrays and array lists
    By ambernicole88 in forum New To Java
    Replies: 3
    Last Post: 12-04-2009, 09:47 PM
  5. [SOLVED] Creating an Array of Arrays?
    By xcallmejudasx in forum Advanced Java
    Replies: 5
    Last Post: 11-04-2008, 06:01 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
  •