I am trying to remove repeating common characters from a string but the problem is only the last common character which in this case is y is getting deleted and all the others common characters are displayed as they are. I am not getting any exception .
我试图从一个字符串中删除重复的公共字符,但问题是只有最后一个公共字符,在这个例子中,就是y被删除了所有其他公共字符都显示为原样。我没有任何例外。
public class stringCompare {
private static String s;
private static char[] c;
private static boolean[] b;
public static void judge(boolean flag , int l){
b[l] = flag;
}//judge
public static void main(String[] args) {
s = "unlucky unlucky unlucky unlucky";
c = new char[s.length()];
b = new boolean[s.length()];
int j;
System.out.println(s.length());
for(int i=0 ; i<s.length() ; i++){
c[i] = s.charAt(i);
System.out.println(c[i]);
for(j=0 ; j<s.length() ; j++){
System.out.println(c[j]);
if(c[i] == s.charAt(j)){
judge(true , i);
}//if
else if(c[i] != s.charAt(j)){
judge(false , i);
}//else if
}//for
}//for
StringBuilder sb = new StringBuilder(s.length());
for(int k=0 ; k<b.length ; k++){
System.out.println(b[k] +"===="+k);
if(b[k] == false){
sb.append(c[k]);
}//if
}//for
System.out.println(sb.toString());
}//main
}//stringCompare
Output from the program -
unluck unluck unluck unluck
But i was expecting output as - unlucky
但我希望输出是-不幸的
3 个解决方案
#1
0
just try this Logic
试试这个逻辑
StringBuilder sb=new StringBuilder();
String s = "unlucky unlucky unlucky unlucky";
char[] inp_cpy=s.toCharArray();
for( char x : inp_cpy)
{
if(!sb.ToString().Contains(""+x))
{
sb.append(x);
}
}
System.out.println(sb.toString());
#2
0
This code prints "unlcky", that is, it removes all repeating characters. I understood that was your problem. If "indexOf" is not allowed then you should replace it with a loop that searches for a character in the string.
此代码打印“unlcky”,即删除所有重复的字符。我知道那是你的问题。如果不允许“indexOf”,那么您应该用一个搜索字符串中字符的循环来替换它。
public class stringCompare {
private static String s;
public static void main(String[] args) {
s = "unlucky unlucky unlucky unlucky";
StringBuilder result = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
String currentChar = s.substring(i,i+1);
if (result.indexOf(currentChar)==-1) {
result.append(currentChar);
}
}
System.out.println(result.toString());
}
}
#3
0
You can use LinkedHashSet if ordering is a matter of concern, otherwise TreeSet or HashSet.
如果排序需要关注,可以使用LinkedHashSet,否则可以使用TreeSet或HashSet。
import java.util.Arrays;
import java.util.LinkedHashSet;
public class StringCompare{
public static void main(String[] args) {
String s = "unlucky unlucky unlucky unlucky";
LinkedHashSet<Character> linkedhashset = new LinkedHashSet<Character>();
for (int i = 0; i < s.length(); i++) {
if(s.charAt(i) != ' ')
linkedhashset.add(s.charAt(i));
}
System.out.println(Arrays.toString(linkedhashset.toArray()));
linkedhashset.forEach(System.out::print);
}
}
The ouput is:
输出是:
[u, n, l, c, k, y]
unlcky
#1
0
just try this Logic
试试这个逻辑
StringBuilder sb=new StringBuilder();
String s = "unlucky unlucky unlucky unlucky";
char[] inp_cpy=s.toCharArray();
for( char x : inp_cpy)
{
if(!sb.ToString().Contains(""+x))
{
sb.append(x);
}
}
System.out.println(sb.toString());
#2
0
This code prints "unlcky", that is, it removes all repeating characters. I understood that was your problem. If "indexOf" is not allowed then you should replace it with a loop that searches for a character in the string.
此代码打印“unlcky”,即删除所有重复的字符。我知道那是你的问题。如果不允许“indexOf”,那么您应该用一个搜索字符串中字符的循环来替换它。
public class stringCompare {
private static String s;
public static void main(String[] args) {
s = "unlucky unlucky unlucky unlucky";
StringBuilder result = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
String currentChar = s.substring(i,i+1);
if (result.indexOf(currentChar)==-1) {
result.append(currentChar);
}
}
System.out.println(result.toString());
}
}
#3
0
You can use LinkedHashSet if ordering is a matter of concern, otherwise TreeSet or HashSet.
如果排序需要关注,可以使用LinkedHashSet,否则可以使用TreeSet或HashSet。
import java.util.Arrays;
import java.util.LinkedHashSet;
public class StringCompare{
public static void main(String[] args) {
String s = "unlucky unlucky unlucky unlucky";
LinkedHashSet<Character> linkedhashset = new LinkedHashSet<Character>();
for (int i = 0; i < s.length(); i++) {
if(s.charAt(i) != ' ')
linkedhashset.add(s.charAt(i));
}
System.out.println(Arrays.toString(linkedhashset.toArray()));
linkedhashset.forEach(System.out::print);
}
}
The ouput is:
输出是:
[u, n, l, c, k, y]
unlcky