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 04-06-2008, 08:59 PM
Member
 
Join Date: Apr 2008
Posts: 2
daigre7 is on a distinguished road
HELP: Trouble with partial filled arrays
For HW, I have to write a deleteRepeats static method for an array of characters that deletes any character previously listed and returns the new array size. The example the problem gives is:

char a[10];
a[0]='a';
a[1]='b';
a[2]='a';
a[3]='c';
int size = 4;
size = deleteRepeats(a, size);

The problem then says after the code is executed, a[0] should be 'a', a[1] should be 'b', a[2] should be 'c' and size is 3. I read the section in my book on partially filled arrays, but I still can't get this method to work properly. Here is my code:

public static int deleteRepeats(char[] a, int size) {

size=a.length;
int numberOfRepeats=0;

for(int i=0; i<size; i++) {
for(int j=1; i+j<size; j++) {
if(a[i]==a[i+j]) {
a[i+j]=a[(i+j)+1];
size--;
numberOfRepeats++;

}
}
}
return size;
}

With that code, when I put in the test array [aabcd], it returns 3, when it should be 4 and it changes the array to [abccd]. I'm sure there's numerous problems with my code. Any help is greatly appreciated.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-07-2008, 03:05 AM
Member
 
Join Date: Apr 2008
Posts: 2
daigre7 is on a distinguished road
BTW, we're learning about ArrayLists now, but we can't use them for this HW. He wants us to learn how to do something like this without it.

I know that to remove an element in an array you have to do something like shift everything with a higher index to the left and then transfer what you want to a new array of size one less than the original, but I have trouble figuring out the code that does that. Just read the part before my code and let me know how you would go about writing such a method.
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
compiling trouble capacitator CLDC and MIDP 4 06-10-2008 11:12 PM
Compile Trouble adelgado0723 New To Java 5 04-21-2008 03:02 AM
trouble with program jimJohnson New To Java 1 04-03-2008 10:29 AM
Having trouble with array ice22 New To Java 3 11-13-2007 04:06 AM
JTree trouble Alantie Vala AWT / Swing 3 08-01-2007 12:12 AM


All times are GMT +3. The time now is 05:05 PM.


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