简体   繁体   中英

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. 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).

  1. Use the replaceAll method on your String object to replace whitespaces
  2. Transform your String in a char array
  3. For each char add it to the Set (a Set doesn't allow duplicates)
  4. Use toArray(T[] object) method to get back a Character array.

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);

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 . 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. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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