java如何去掉字符串中重复的字符 java中怎么将字符串数组中的重复的元素去掉,但是还是保留一...
java\u5982\u4f55\u53bb\u9664\u5b57\u7b26\u4e32\u4e2d\u91cd\u590d\u7684\u5b57\u7b26lz \u4f60\u597d
\u8fd9\u4e2a\u5c31\u662f\u4e00\u4e2a\u6bd4\u8f83\u7b80\u5355\u7684\u7b97\u6cd5\u9898 , \u4f60\u53ef\u4ee5\u81ea\u5df1\u5199
\u4f46\u662f\u7528Java\u8bed\u8a00\u5c31\u4e0d\u9700\u8981\u4e86 , Java\u81ea\u5e26\u6cdb\u578b\u76f8\u5173\u7684\u7c7b , \u5f88\u6709\u7528
\u5176\u4e2dTreeSet\u96c6\u5408\u80fd\u81ea\u52a8\u8bc6\u522b\u6dfb\u52a0\u7684\u662f\u5426\u91cd\u590d , \u91cd\u590d\u7684\u5c06\u4e0d\u4f1a\u6dfb\u52a0 , \u5f88\u65b9\u4fbf
\u4ee5\u4e0b\u662f\u5b9e\u73b0\u4ee3\u7801:
import java.util.List;
import java.util.Scanner;
public class DeleteRepeated {
private String str;
private TreeSet noReapted;//\u5e26\u6709String\u7c7b\u578b\u7684TreeSet\u6cdb\u578b
public DeleteRepeated() {
Scanner in = new Scanner(System.in);
System.out.println ("\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32:");
str = in.nextLine();
noReapted = new TreeSet();
}
//\u6e05\u695a\u91cd\u590d\u7684\u6570\u636e
public void removeRepeated(){
for (int i = 0; i < str.length(); i++){
noReapted.add(""+str.charAt(i));
//str.charAt(i)\u8fd4\u56de\u7684\u662fchar\u578b \u6240\u4ee5\u5148\u52a0\u4e00\u4e2a""\u7a7a\u683c , \u8f6c\u6362\u6210String\u578b
//TreeSet\u6cdb\u578b\u80fd\u4fdd\u8bc1\u91cd\u590d\u7684\u4e0d\u52a0\u5165 , \u800c\u4e14\u6709\u5e8f
}
str = "";
for(String index:noReapted){
str += index;
}
//\u8f93\u51fa
System.out.println (str);
}
public static void main(String[] args) {
DeleteRepeated dr = new DeleteRepeated();
dr.removeRepeated();
}
}
import java.util.*;
class BaiDu
{
public static void main(String[] args)
{
TreeSet tr = new TreeSet();
String[] s ={"11","22","22","33","33","33"};
System.out.print("====\u5904\u7406\u524d=======");
for(int i=0;i<s.length;i++){
System.out.print(s[i]+" ");
tr.add(s[i]);
}
String[] s2= new String[tr.size()];
System.out.println("=====\u5904\u7406\u540e======");
for(int i=0;i<s2.length;i++){
s2[i]=tr.pollFirst();//\u4eceTreeSet\u4e2d\u53d6\u51fa\u5143\u7d20\u91cd\u65b0\u8d4b\u7ed9\u6570\u7ec4
System.out.print(s2[i]+" ");
}
}
}
lz 你好
这个就是一个比较简单的算法题 , 你可以自己写
但是用Java语言就不需要了 , Java自带泛型相关的类 , 很有用
其中TreeSet集合能自动识别添加的是否重复 , 重复的将不会添加 , 很方便
以下是实现代码:
import java.util.List;
import java.util.Scanner;
public class DeleteRepeated {
private String str;
private TreeSet<String> noReapted;//带有String类型的TreeSet泛型
public DeleteRepeated() {
Scanner in = new Scanner(System.in);
System.out.println ("输入一个字符串:");
str = in.nextLine();
noReapted = new TreeSet();
}
//清楚重复的数据
public void removeRepeated(){
for (int i = 0; i < str.length(); i++){
noReapted.add(""+str.charAt(i));
//str.charAt(i)返回的是char型 所以先加一个""空格 , 转换成String型
//TreeSet泛型能保证重复的不加入 , 而且有序
}
str = "";
for(String index:noReapted){
str += index;
}
//输出
System.out.println (str);
}
public static void main(String[] args) {
DeleteRepeated dr = new DeleteRepeated();
dr.removeRepeated();
}
}
运行截图:
希望能帮助你哈
java去掉字符串中重复的字符参考代码:
//获取差集
public String getDifferenceSet(){
String ds = "";//差集字符串
boolean isDifference=false;//是否相同
//如果one的长度小于two的长度 就交换one和two
if(one.length() < two.length()){
String str = one;
one = two;
two = str;
}
for (int i = 0; i < one.length(); i++){
for(int j = 0; j < two.length(); j++){
//如果相等 那么跳出循环
if(one.charAt(i) == two.charAt(j)){
isDifference=false;
break;
}
//否则继续判断
else{
isDifference=true;
}
}
//如果不相同 就加入到ds中
if(isDifference){
ds += "" + one.charAt(i);
isDifference = false;
}
}
return ds;
}
效果图如下:
lz 你好
这个就是一个比较简单的算法题 , 你可以自己写
但是用Java语言就不需要了 , Java自带泛型相关的类 , 很有用
其中TreeSet集合能自动识别添加的是否重复 , 重复的将不会添加 , 很方便
以下是实现代码:
import java.util.List;
import java.util.Scanner;
public class DeleteRepeated {
private String str;
private TreeSet<String> noReapted;//带有String类型的TreeSet泛型
public DeleteRepeated() {
Scanner in = new Scanner(System.in);
System.out.println ("输入一个字符串:");
str = in.nextLine();
noReapted = new TreeSet();
}
//清楚重复的数据
public void removeRepeated(){
for (int i = 0; i < str.length(); i++){
noReapted.add(""+str.charAt(i));
//str.charAt(i)返回的是char型 所以先加一个""空格 , 转换成String型
//TreeSet泛型能保证重复的不加入 , 而且有序
}
str = "";
for(String index:noReapted){
str += index;
}
//输出
System.out.println (str);
}
public static void main(String[] args) {
DeleteRepeated dr = new DeleteRepeated();
dr.removeRepeated();
}
}
lz 你好
这个就是一个比较简单的算法题 , 你可以自己写
但是用Java语言就不需要了 , Java自带泛型相关的类 , 很有用
其中TreeSet集合能自动识别添加的是否重复 , 重复的将不会添加 , 很方便
以下是实现代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.util.List;
import java.util.Scanner;
public class DeleteRepeated {
private String str;
private TreeSet<String> noReapted;//带有String类型的TreeSet泛型
public DeleteRepeated() {
Scanner in = new Scanner(System.in);
System.out.println ("输入一个字符串:");
str = in.nextLine();
noReapted = new TreeSet();
}
//清楚重复的数据
public void removeRepeated(){
for (int i = 0; i < str.length(); i ){
noReapted.add("" str.charAt(i));
//str.charAt(i)返回的是char型 所以先加一个""空格 , 转换成String型
//TreeSet泛型能保证重复的不加入 , 而且有序
}
str = "";
for(String index:noReapted){
str = index;
}
//输出
System.out.println (str);
}
public static void main(String[] args) {
DeleteRepeated dr = new DeleteRepeated();
dr.removeRepeated();
}
}
放到set集合里面,set集合自动帮你去重!放完读出就好。
绛旓細String num[]=new String[str.length()/4+1];//瀹氫箟涓涓暟缁勶紝鏁扮粍闀垮害涓 4涓瓧姣嶇殑涓暟 for(int i=0;i<num.length;i++){ num[i]=str.substring(i*4, (i+1)*4>str.length()?str.length():(i+1)*4);//灏嗗洓涓瓧姣嶄竴缁勬斁鍏ユ暟缁勪腑 } //寮濮嬫瘮杈冩暟缁勫唴瀹规槸鍚︽湁涓鑷寸殑 int sum...
绛旓細import java.util.*;class BaiDu { public static void main(String[] args){ TreeSet<String> tr = new TreeSet<String>();String[] s ={"11","22","22","33","33","33"};System.out.print("===澶勭悊鍓===");for(int i=0;i<s.length;i++){ System.out.print(s[i]+" ...
绛旓細浠ヤ笅鏄疄鐜颁唬鐮:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 import java.util.List;import java.util.Scanner;public class DeleteRepeated { private String str;private TreeSet noReapted;//...
绛旓細ArrayList a = new ArrayList(鏁扮粍);for(int i = 0 ;i
绛旓細杩欐牱灏辨悶瀹氫簡锛屾湅鍙 public static void main(String[] args) { String str = "123;456;123;789;12;54;12;456;";//杞寲涓烘暟缁 String[] items = str.split(";");List cache = new ArrayList();for(int i=0;i<items.length;i++){ String item = items[i];int count = 0;for(...
绛旓細鐢↙inkedHashSet灏卞彲浠ュ仛鍒颁簡..瀹冩槸鏈夊簭涓閲嶅鐨闆嗗悎.public static void main(String[] args) { String s = "ads_fagaerididfhdus_afiew"; LinkedHashSet noRepeatSet = new LinkedHashSet(); LinkedHashSet repeatSet = new LinkedHashSet(); for (int i = 0; i < s.leng...
绛旓細String a ="sdfasdfasdf";String b ="asdfhearhmykky";for(char i :a.toCharArray() ){ if(b.indexOf(i)!=-1){ System.out.print(i);} }
绛旓細public class Test { public static void main(String[] args) { List<String> list = new ArrayList<String>();list.add("娴嬭瘯1");list.add("娴嬭瘯2");list.add("娴嬭瘯3");list.add("娴嬭瘯4");list.add("娴嬭瘯4");list.add("娴嬭瘯2");list.add("娴嬭瘯5");System.out.println("娌℃湁...
绛旓細import java.util.ArrayList;import java.util.HashMap;import java.util.Scanner;public class Test{ public static void main(String[] args) { Scanner s = new Scanner(System.in);System.out.println("璇疯緭鍏ヤ竴涓瀛楃涓:");String cnt = s.nextLine();ArrayList<Character> array = new ...
绛旓細String str = "aa锛焌a锛焌aaaaaa锛焌aaaaaaa锛焌aaaa锛焌aaaaa锛焌a";String destStr = "";String ss[] = str.split("锛");for (int i = 0; i < ss.length; i++) {if (i % 2 == 1) {destStr = destStr + ss[i] + ">>";} else if (i != ss.length - 1) {destStr =...