...

View Full Version : How to add a merge array from 2 others arrays



v.victor230364
03-29-2012, 09:16 AM
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 I can add a merge array from 2 others arrays and print it.
This is the code I have written:


//////////////////////////////////////////////////////
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, then called from main to read array a and array b
// but when array a has info the array b is empty, and viceveresa

////////////////////////////////////////////////////////////////

}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum