Results 1 to 4 of 4
- 03-29-2012, 11:09 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
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 //////////////////////////////////////////////////////////////// }
- 03-29-2012, 11:17 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 03-29-2012, 11:18 AM #3
Re: How to add a merge array from 2 others arrays
Cross posted
http://www.coderanch.com/t/571820/ja...-others-arrays
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-29-2012, 01:44 PM #4
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
Re: How to add a merge array from 2 others arrays
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 okJava 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
-
How do you make an array of arrays?
By mrjaeyun in forum New To JavaReplies: 1Last Post: 11-14-2011, 12:34 AM -
Array of Arrays all unique elements
By dusker in forum New To JavaReplies: 9Last Post: 04-02-2011, 07:21 PM -
store array of arrays in array of arrays
By joost_m in forum New To JavaReplies: 4Last Post: 04-19-2010, 10:32 AM -
Help with arrays and array lists
By ambernicole88 in forum New To JavaReplies: 3Last Post: 12-04-2009, 09:47 PM -
[SOLVED] Creating an Array of Arrays?
By xcallmejudasx in forum Advanced JavaReplies: 5Last Post: 11-04-2008, 06:01 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks