Results 1 to 2 of 2
  1. #1
    barney is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Remove the bucket represented by the int from array

    Hi, 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
    Java 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];
        }
      }
    I'm not sure about this part
    Java Code:
    if (i == e)
        result[e] =
    how do i tell it to get rid of the char at int e?
    Thanks

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
        public static void main(String[] args) {
            String s = "Hello World";
            char[] chars = s.toCharArray();
            print(chars, "  chars");
            char[] reduced = removeElement(chars, 9);
            print(reduced, "reduced");
        }
    
    
        static char[] removeElement(char[] x, int e)
        {
            char[] result = new char[x.length-1];
            for (int i = 0, k = 0; i < x.length; i++)
            {
                if (i == e)
                    continue;
                result[k++] = x[i];
            }
            return result;
        }
    
        private static void print(char[] array, String s)
        {
            System.out.print(s + " = [");
            for(int j = 0; j < array.length; j++)
            {
                System.out.print(array[j]);
                if(j < array.length-1)
                    System.out.print(", ");
            }
            System.out.print("]\n");
        }

Similar Threads

  1. how to remove an image icon
    By cecily in forum Advanced Java
    Replies: 1
    Last Post: 08-05-2007, 05:25 AM
  2. how to remove whitespaces in a text
    By christina in forum New To Java
    Replies: 2
    Last Post: 08-03-2007, 06:24 PM
  3. Is there a way to remove an option from optionsCollection?
    By schu777 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 08-02-2007, 10:47 PM
  4. how to remove an object from the array list
    By cecily in forum New To Java
    Replies: 3
    Last Post: 08-02-2007, 03:26 PM
  5. how to remove an old version of JDK
    By tommy in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 09:59 AM

Posting Permissions

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