简体   繁体   English

在没有重复Java的Char数组中输入

[英]Input in Char Array without Duplicates Java

Imagine I have this: 想象一下,我有这个:

String input = "AB   BC";   //  ( Just an Example)

I want to put this String into an char array, but i want to have no duplicates and blank symbols in my char Array. 我想把这个String放到一个char数组中,但我想在我的char数组中没有重复项和空白符号。 My solution so far: 到目前为止我的解

String input = "AB  BC"

char array[]=input.toCharArray();
for(int i=0;i<array.length;i++){
    System.out.println("Data at ["+i+"]="+array[i]);
}

The Output is : 输出是:

This is my input String AB  BC 
This is the content of my Char Array
Data at [0]=A
Data at [1]=B
Data at [2]= 
Data at [3]= 
Data at [4]=B
Data at [5]=C
Data at [6]= 

So now I don't know how I can erase the duplicates and the blank symbols. 所以现在我不知道如何删除重复项和空白符号。

You can use a LinkedHashSet<Character> (to maintain insertion order). 您可以使用LinkedHashSet<Character> (以维护插入顺序)。

  1. Use the replaceAll method on your String object to replace whitespaces 使用String对象上的replaceAll方法替换空格
  2. Transform your String in a char array 在char数组中转换String
  3. For each char add it to the Set (a Set doesn't allow duplicates) 对于每个char,将其添加到Set (一个Set不允许重复)
  4. Use toArray(T[] object) method to get back a Character array. 使用toArray(T[] object)方法获取Character数组。

So it would be something like this : 所以它会是这样的:

        String input = "AB  BC";
        input = input.replaceAll("\\s+", "");
        Set<Character> s = new LinkedHashSet<>();

        for(char c : input.toCharArray())
            s.add(c);

        Character [] array = s.toArray(new Character[0]);
        System.out.println(Arrays.toString(array));

Output : 输出:

[A, B, C]

If you want to have back an array of primitive you can use (note that you have to use the apache commons library) char[] arr = ArrayUtils.toPrimitive(array); 如果你想拥有一个可以使用的原语数组(注意你必须使用apache commons库) char[] arr = ArrayUtils.toPrimitive(array);

Here's the source code : 这是源代码:

2647      public static char[] toPrimitive(Character[] array) {
2646            if (array == null) {
2647                return null;
2648            } else if (array.length == 0) {
2649                return EMPTY_CHAR_ARRAY;
2650            }
2651            final char[] result = new char[array.length];
2652            for (int i = 0; i < array.length; i++) {
2653                result[i] = array[i].charValue();
2654            }
2655            return result;
2656        }

Transfer content to LinkedHashSet . 将内容传输到LinkedHashSet It will remove the duplicates for you ! 它将为您删除重复项! Here's an example to start with. 这是一个开始的例子

使用泛型character Hashset

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

You could use a LinkedHashSet but I assume you want an array at the end. 您可以使用LinkedHashSet,但我假设您最后需要一个数组。 You can do this. 你可以这样做。

String input = ...
StringBuilder sb = new StringBuilder();
BitSet seen = new BitSet(); // more compact than a HashSet<Character>
seen.set(' ');              // pretend we have seen space already
for(char ch: input.toCharArray()) {
    if(!seen.get(ch)) {
        sb.append(ch);
        seen.set(ch);
    }
}
char[] unique = sb.toString().toCharArray();

this will add only the chars without any blanks to the hashset 这将只添加没有任何空白的字符到hashset

char array[]=input.toCharArray();
Set<Character> m=new LinkedHashSet<Character>();
for(int i=0;i<array.length;i++){
   if(array[i]!=' ')
      m.add(array[i])
}
Character[] text = m.toArray(new Character[0]);
System.out.println(Arrays.toString(text))

Simply you can try this: 你可以试试这个:

String input = "AB  BC";
char array[]=input.toCharArray();
for(int i=0;i<array.length;i++){
      if(!input.substring(0,i).contains(array[i]+"") && array[i]!=' ')
      System.out.println("Data at ["+i+"]="+array[i]);
}

Output: 输出:

Data at [0]=A
Data at [1]=B
Data at [5]=C

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

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