Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jun 2003
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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;
      }

  • #2
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    New Coder
    Join Date
    Jun 2003
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    New Coder
    Join Date
    Jun 2003
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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;
      }

  • #7
    New Coder
    Join Date
    Jun 2003
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    k i got it i flipped around the result[i] and the x[i]. i got it now. Thanks for all the help

  • #8
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No problem. Good work.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •