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

    another array problem with java

    Now i need to take a char array and an int as parameters and remove the bucket represented by the int from the array. this is what i have so far
    Code:
    static char[] removeArray (char[] x, int e)
      {
        char[] result = new char[x.length-1];
        for (int i = 0; i < result.length; i++)
        {
          if (i == e)
          	result[e] = 
          if (i < e)
          	result[i] = x[i];
          if (i > e)
          	result[i] = x[i+1];
        }
      }
    im not sure about this part
    Code:
    if (i == e)
        result[e] =
    how do i tell it to get rid of the char at int e?

  • #2
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Don't do anything when the index equals the bucket value. Just add the other values in.

    Just remove that condition.

  • #3
    New Coder
    Join Date
    Jun 2003
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok so now i have this
    Code:
     static char[] removeArray (char[] x, int e)
      {
        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[i] = x[i+1];
        }
    
        return result;
      }
    it sorta works. for example if i enter "hello" and tell it to remove the char at 2 it should return "helo" instead it returns "he o" what am i doing wrong?
    Last edited by cwl157; 12-11-2004 at 02:29 AM.

  • #4
    Regular Coder
    Join Date
    Oct 2004
    Location
    England
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Do a trace to find the problem, i would but it's half 2 in the morning and i really cba

  • #5
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, there is another way to do it. You can do something like this.

    PHP Code:
    StringBuffer s = new StringBuffer();
    s.append(x);
    s.deleteCharAt(e);
    String cs s.toString();
    char[] result cs.toCharArray(); 
    If you are using java 1.5, just switch the StringBuffer with StringBuilder.

    I'm going to keep looking at the other way though, because it should work.

  • #6
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay, I fixed the other method now. A couple of minor changes needed to be made.

    PHP Code:
    static char[] removeArray (char[] xint e)
          {
            
    char[] result = new char[x.length 1];
            
            for (
    int i 0x.lengthi++)
            {
              if (
    e){
                  
    result[i] = x[i];
              }
              else if (
    e){
                  
    result[1] = x[i];
              }     
            }
            return 
    result;
          } 

  • #7
    New Coder
    Join Date
    Jun 2003
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yea it works great Thanks sooo much for all the help.


  •  

    Posting Permissions

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