开发者

Remove from Destination array all occurrences of all characters that occur in Source Array [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. 开发者_如何学JAVA Closed 10 years ago.

This post was edited and submitted for review 8 months ago and failed to reopen the post:

Original close reason(s) were not resolved

Given two character arrays a[] and b[], remove from b[] all occurrences of all characters that occur in array a[]. You need to do this in-place i.e. without using an extra array of characters. E.g.:

Input: a[] = [‘G’, ‘O’]          
Input  b[] = [‘G’, ’O’, ’O’, ’G’, ’L’, ’E’] 

Output: b[] = [‘L’, ‘E’]

Code :

public class ReplaceCharacterArray{         
       public static void main(String args[]){        
          char a[] = [‘G’, ‘O’]         
          char b[] = [‘G’, ’O’, ’O’, ’G’, ’L’, ’E’]     

         //to replace all the occurences of all the characters of     
         //a[] array in b[] array we have below logic.     

          for(int i=0;i<a.length;i++){     
             for(int j=0;j<b.length;j++){     
                  if(b[j] == a[i]){     
                    //im stuck here how to proceed      
             }     
          }    


You can't remove elements "in place" in Java arrays. They have fixed length. That is, in your example you'll have to return a new array, since you can't change the length of the b array.

Here are some pointers for you:

  • Maintain a write-index for the b array (left of this index, you have only characters not present in a).
  • Iterate through the b array
  • While the current character is contained in a, step forward
  • Swap current character (not contained in a) with the character at the write-index
  • Increment the write-index, and continue from there.

Use for instance Arrays.copyOfRange to return the part of the array to the left of the write-index.


Regarding your update:

  • Arrays are not written using [ and ] and characters are not written using , change them to {, } and '.
  • Having a helper method with the signature boolean arrayContains(char[] arr, char c) will make it easier to write the algorithm
  • If you follow my approach, you will also benefit from having a helper method void swap(char[] arr, int index1, int index2).


You can go about like this

Iterate the loop a, that is 2 times and In each iteration, perform the copare that value exists in the b[] array, if yes delete that. Continue that until you iterate the array b[] completely.

So that in the end you will have b[] array only with the other elements which is not in the array a[].

I hope this will help you to complete your work.


I guess since this is a homework 3rd party library's may not be allowed.Any way i'll just post a code using Guava:

 char[] removeChars(char a[],char b[])
 {
    return  CharMatcher.anyOf(new String(b)).removeFrom(new String(a)).toCharArray();    
 }

UPDATE:(check this sample,it's one way to solve)

import java.util.*;
import java.lang.*;

class CharReplace{
public static void main(String[] args)
{
char a[] = "GOOGLE".toCharArray();
char b[] = "GO".toCharArray();
BitSet charToRemove=new BitSet();
for(char c:b)
 charToRemove.set(c);
StringBuilder str=new StringBuilder();
for(char c:a)
 if(!charToRemove.get(c))
  str.append(c);
b=str.toString().toCharArray();
System.out.println(Arrays.toString(b));

}

}


You have mistakes in your code,

  • Arrays don't start and end with [ and ] respectively. They start and end with { and } respectively.
  • Characters are enclosed with ' only.
  • You will have to keep the state of your iterations (in my case I kept the indexing state and the state of to know whether the character in b exists in a).
  • Have a remove(char[] array, char characterToRemove) and pass the b[] and it's respective character to remove to complete the flow.

Update without using String, StringBuffer, or any libraries, this is what works....

Warning the array b never gets reduced (it gets appended with NULL character). That I will leave it for you to crack.

/**
 * @author The Elite Gentleman
 * 
 */
public class ReplaceCharacterArray {
    public static boolean containsCharacter(char character, char[] array) {
        for (int i = 0; i < array.length; i++) {
            if (array[i] == character) {
                return true;
            }
        }

        return false;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        char a[] = {'G', 'O'};
        char b[] = {'G', 'O', 'O', 'G', 'L', 'E'};
        //to replace all the occurences of all the characters of a[] array in b[] array we have below logic.

        int index = 0;
        int count = 0;
        while (index < b.length) {
            while (ReplaceCharacterArray.containsCharacter(b[index], a)) {
                System.arraycopy(b, index + 1, b, 0, b.length - index - 1);
                count++;
            }

            index++;
        }

        for (int i = (b.length - count); i < b.length; i++) {
            b[i]= '\0';
        }

        for (int i = 0; i < b.length; i++) {
            System.out.println(b[i]);
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜