Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 3.00 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-10-2004, 08:41 PM   PM User | #1
cwl157
New Coder

 
Join Date: Jun 2003
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
cwl157 is an unknown quantity at this point
adding a char to a char array in java

i need to write a method that takes a char array, an int, and a char value as parameters and add the char value after the bucket number represented by the int in the char array, moving everything else down one spot. i think i am close. Right now i can get the char parameter to replace the the char already there but i need to add it after the char thats already there. Heres my code so far:
Code:
static char[] addArray (char[] x, int e, char y)
  {
    char[] result = new char[x.length+1];
    x[e+1] = y;
    result = x;
    return result;
  }
cwl157 is offline   Reply With Quote
Old 12-10-2004, 09:27 PM   PM User | #2
turbowrx
New Coder

 
Join Date: Nov 2004
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
turbowrx is an unknown quantity at this point
Your creation of the new char array is fine.
Next step. For loop for the length of the new char array.
In the body of the loop, check to see if you at the the bucket position, if not, add the char from the old array at loop position, assuming you started at zero.
Now to check for bucket position. Say it is 6. You want position 5 in the array, unless you mean that 6 means 6.
Once he hit the bucket position, add to the char array the new value.
Now, all you have to do is add the rest of the old values. These are actually going to be loop position - 1. Hope this helps.
turbowrx is offline   Reply With Quote
Old 12-10-2004, 09:28 PM   PM User | #3
turbowrx
New Coder

 
Join Date: Nov 2004
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
turbowrx is an unknown quantity at this point
I'm trying to psuedo this for you. Any problems just let me know and I'll explain myself more clearly and with some code.

Another hint. The way you view the bucket position will impact your test condition.

Last edited by turbowrx; 12-10-2004 at 09:36 PM..
turbowrx is offline   Reply With Quote
Old 12-11-2004, 12:02 AM   PM User | #4
cwl157
New Coder

 
Join Date: Jun 2003
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
cwl157 is an unknown quantity at this point
ok so i followed what you said this is what i got so far. it compiles ok but then comes up with an array index out of bounds exception. what am i doing wrong?
Code:
static char[] addArray (char[] x, int e, char y)
  {
    char[] result = new char[x.length+1];
    for (int i = 0; i < result.length; i++)
    {
      if(i != e)
         result[i] = x[i];
      if (i == e)
      	result[e] = y;
    }

    return result;
  }

Last edited by cwl157; 12-11-2004 at 12:13 AM..
cwl157 is offline   Reply With Quote
Old 12-11-2004, 12:10 AM   PM User | #5
turbowrx
New Coder

 
Join Date: Nov 2004
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
turbowrx is an unknown quantity at this point
As long as you are consider that your arrays start at zero, just compare the index of the loop, i, with your bucket position. If equal, put the new value in the position. If i < bucket, just put the ith position of the old array into the new array at the ith position. If i > bucket, put the ith - 1 position of the old array into the ith position of the new array.

There may be a slightly neater way of doing this, but this was the first method i thought of.

So, you should have 3 conditions. You can use else if statements as well.

Good luck.
turbowrx is offline   Reply With Quote
Old 12-11-2004, 12:18 AM   PM User | #6
cwl157
New Coder

 
Join Date: Jun 2003
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
cwl157 is an unknown quantity at this point
ok so now this is what i have for the char to be added i put in 'z' and when it returns result it just returns z in the e position of the char array it does not write any of the other characters.
Code:
static char[] addArray (char[] x, int e, char y)
  {
    char[] result = new char[x.length+1];
    for (int i = 0; i < result.length; i++)
    {
      if (i == e)
      	result[e] = y;
      if (i < e)
      	x[i] = result[i];
      if (i > e)
      	x[i-1] = result[i];
    }

    return result;
  }
cwl157 is offline   Reply With Quote
Old 12-11-2004, 12:28 AM   PM User | #7
cwl157
New Coder

 
Join Date: Jun 2003
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
cwl157 is an unknown quantity at this point
k i got it i flipped around the result[i] and the x[i]. i got it now. Thanks for all the help
cwl157 is offline   Reply With Quote
Old 12-11-2004, 12:35 AM   PM User | #8
turbowrx
New Coder

 
Join Date: Nov 2004
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
turbowrx is an unknown quantity at this point
No problem. Good work.
turbowrx is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:02 PM.


Advertisement
Log in to turn off these ads.