简体   繁体   English

从char数组中删除重复项的Java方法

[英]Java Method for removing duplicates from char array

I have a char array filled by the user ( arrayInput[] ) with some characters, like {b, d, a, b, f, a, g, a, a, f}, and I need to create a method which returns a new char array with only the first occurrence of the character, but in the order of input. 我有一个由用户( arrayInput[] )填充的字符数组,其中包含一些字符,例如{b,d,a,b,f,a,g,a,a,f},我需要创建一个返回的方法一个新的char数组,其中只有字符的第一个出现,但按输入顺序。 The book also says "A way to solve this problem is to create a boolean array to keep track of the characters to mantain!", but I can't imagine how the boolean array should work with the other arrays. 该书还说“解决此问题的一种方法是创建一个布尔数组来跟踪要维护的字符!”,但我无法想象布尔数组应如何与其他数组一起工作。

The main problem is that I can save in a boolean array if arrayInput contains a specific character, and even how many times, but only creating a very long ramified if-else into a for, like 主要问题是,如果arrayInput包含特定字符,甚至是多少次,我都可以保存在布尔数组中,但是只能将很长的分支if-else创建为for,例如

    if ((arrayOutput[i] == 'A') && (arrayControl[0] = false)) {
        arrayControl[0] = true;  }

where arrayOutput is the array I want to return from the method, arrayControl[0] is the value of 'A' in my boolean array I created into the method. 其中arrayOutput是我要从该方法返回的数组, arrayControl[0]是我在该方法中创建的布尔数组中的'A'值。 A = 0, B = 1, ... Z = 25, a = 26, b = 27, ... 51 = z. A = 0,B = 1,... Z = 25,a = 26,b = 27,... 51 = z。 For every single character, uppercase and lowercase, I created a place into the array, so I could check everything, but now I can't go any further. 对于每个单个字符(大写和小写),我都在数组中创建了一个位置,因此可以检查所有内容,但是现在我无法再进行任何操作了。 I don't know how to save the characters on arrayOutput , how to check if a character is already on arrayOutput and if it's already there, the array passes that specific character and go to the next one. 我不知道如何将字符保存在arrayOutput ,如何检查一个字符是否已经在arrayOutput ,如果已经存在,则数组将传递该特定字符并转到下一个字符。

Also please remember I'm a newbie, so I know very little about Java. 还请记住我是新手,所以我对Java知之甚少。 Please explain yourself the best you can. 请尽自己所能解释。 Thanks in advance! 提前致谢!

This could work: 这可以工作:

public static void main(String[] args) {
    Main main = new Main();
    char[] array = {'e','a','b','a','c','d','b','d','c','e'};
    main.getCharArray(array);
}

private char[] getCharArray(char[] array) {
    String _array = "";
    for(int i = 0; i < array.length; i++) {
        if(_array.indexOf(array[i]) == -1) // check if a char already exist, if not exist then return -1
            _array = _array+array[i];      // add new char
    }
    return _array.toCharArray();
}

Output: 输出:

eabcd EABCD

 boolean arr[26]; //considering only small letters arrive. otherwise take a larger array.
for( i=0;i<str.length;i++ )
  arr[str[i]-'a']=true;

The ones at last after the loop are true are the actual character. 循环后最后一个为真的是实际字符。 (all duplicates eleminated). (删除所有重复项)。

To take into consideration the positions, 考虑到职位,

int arr[26];
  //initialize all the array elemnts to 0
  for( i=0;i<str.length();i++ )
      if(i>=arr[str[i]-'a'])
            arr[str[i]-'a']=i+1;

//Those greater than 0 are non-duplicated characters. //大于0的字符是非重复字符。 Their poistion of first occurence= (arr[i]-1) 他们第一次出现的地点=(arr [i] -1)

EDIT: I have last used java almost a year ago. 编辑:我最近一年以前使用过Java。 The algorithm is shown properly. 该算法已正确显示。 Sorry for my awkward java code. 对不起,我尴尬的Java代码。

This might help. 这可能会有所帮助。 Make a separate array and store only non-duplicate characters. 制作一个单独的数组,并仅存储非重复字符。

char[] removeDuplicates (char[] arrayInput) {
    boolean exists[]=new boolean[26];
    char arrayOutput[] = new char[26];
    int ctr=0;
    for(int i=0; i<26; i++) {
        exists[i] = false;
    }
    for(int i=0; i<arrayInput.length; i++) {
        if(!exists[arrayInput[i]-97]) {
            exists[arrayInput[i]-97]=true;
            arrayOutput[ctr++]=arrayInput[i];
        }
    }

   return Arrays.copyOfRange(arrayOutput, 0, ctr);

}

If you consider using of collection framework then it would be much easier. 如果您考虑使用集合框架,那么会容易得多。 Your array of char with duplicate is arrayInput . 您的重复char数组为arrayInput Now put each char from it to a HashSet like this - 现在,将每个char从中放入这样的HashSet -

HashSet<Character> uniqueCharSet = new HashSet<Character>();
for(char each : arrayInput){

   uniqueCharSet.add(each);
}   

Now the HashSet uniqueCharSet will contains only the unique characters from the char array arrayInput . 现在, HashSet uniqueCharSet将仅包含char数组arrayInput的唯一字符。 Note here all element in uniqueCharSet are wrapper type - Character . 注意,这里的所有元素uniqueCharSet是包装类型- Character

You can convert the HashSet uniqueCharSet to array of Character like this - 您可以像这样将HashSet uniqueCharSet转换为Character数组-

Object[] uniqueCharArray = uniqueCharSet.toArray();

And then you can use them like this - 然后您可以像这样使用它们-

for(Object each : uniqueCharArray){
   Character c = (Character) each;
   System.out.println(c);
}

Here's the method to achieve what you need: 这是实现您所需的方法:

public static void main(String[] args) {
    char[] arr= {'A','B','C','A','B'};

    HashSet<Character> hset=new HashSet<Character>();

    for(int i=0;i<arr.length;i++) {
        hset.add(arr[i]);
        }

    Object[] ObjChar=hset.toArray();

    char[] resultArr = new char[ObjChar.length];

    for(int j=0;j<ObjChar.length;j++) {

    resultArr[j]=(char) ObjChar[j];
    }

    for(char eachChar: resultArr) {

        System.out.println(eachChar);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM