Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-08-2008, 10:55 PM
Member
 
Join Date: Jun 2008
Posts: 20
frasifrasi is on a distinguished road
Mergin two arrays problem.
In this hw problem, we are told that two arrays are already sorted and we need to merge them.

Here is my code:
Code:
int length = a.length+ b.length; int[] c = new int[length]; for(int i =0; i < a.length; i++) c[i] = a[i]; for(int j = a.length-1; j < length;j++){ for(int k = 0; k < b.length; k++){ c[j] = b[k]; } } java.util.Arrays.sort(c); return c; }
The issues given were:
----------------------------------------------------------------
⇒*****Exception java.lang.ArrayIndexOutOfBoundsException: -1
***************occurred
*****⇒*****result[13] is incorrect
*****⇒*****result[2] is incorrect
*****⇒*****result[3] is incorrect
*****⇒*****result[4] is incorrect
*****⇒*****result[5] is incorrect

Fails When:
*****⇒*****arr1[i] == arr21[i]
*****⇒*****arr1[length-1]
-----------------------------------------------------------

I cannot see what is wrong. can anyone help?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-09-2008, 12:54 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,490
Norm is on a distinguished road
What line in your code is throwing the exception?
Since the value of the index j shown in the error message is -1, the only place I see that can happen is if length of a == 0
for(int j = a.length-1; j < length;j++){

If you want help with your algorithm, write some comments in your code describing what it is you are trying to do and how each of the statements does a step of the process.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-09-2008, 03:50 AM
Eku Eku is offline
Senior Member
 
Join Date: May 2008
Location: Makati, Philippines
Posts: 199
Eku is on a distinguished road
I think there is a problem in your algorithm.

Here is a suggestion.

Code:
int temp =0; for(int i = temp; i < a.length; i++) { c[i] = a[i]; temp = i; } for(int i = temp; i < length;i++){ c[i] = b[i-temp]; }
Try this Algo
__________________
Mind only knows what lies near the heart, it alone sees the depth of the soul.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-09-2008, 02:25 PM
Member
 
Join Date: Jul 2008
Posts: 1
Mythili is on a distinguished road
Hellooooo
The problem is with the for loop.

int length = a.length+ b.length;
int[] c = new int[length];
for(int i =0; i < a.length; i++)
c[i] = a[i];
for(int j = a.length-1; j < length;j++){
for(int k = 0; k < b.length; k++){
c[j] = b[k];
}
}

instead try this...........

int length = a.length+ b.length;
int k=0;
int[] c = new int[length];
for(int i =0; i < a.length; i++)
c[i] = a[i];

k=a.length;

for(int j=0; j < b.length; j++,k++)
{
c[k]=b[j];
}

Last edited by Mythili : 07-09-2008 at 02:29 PM.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-09-2008, 04:29 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,490
Norm is on a distinguished road
"need to merge them."
Seems to me your solution is not in the spirit of the assignment. I would think the assignment would be to create a new array large enough to hold both the other arrays and then to go thru the two arrays moving the contents from them to the output array by testing which one was smaller (assuming ascending sequence). By calling sort YOU are not doing the merge, the sort program is.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Arrays Problem (Advanced Java...Need Help) Zebra New To Java 9 05-02-2008 02:26 AM
Arrays bunbun New To Java 1 04-09-2008 03:24 AM
new to arrays jimJohnson New To Java 1 04-08-2008 03:45 PM
arrays help Warren New To Java 6 11-23-2007 08:23 PM
Problem with Sequential File and Arrays rhivka New To Java 8 07-30-2007 10:03 PM


All times are GMT +3. The time now is 12:30 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org