java中怎么将字符串数组中的重复的元素去掉,但是还是保留一个。 Java ArrayList中怎么去掉相同的元素并保留相同元...
Java\u628a\u4e00\u4e2a\u6570\u7ec4\u4e2d\u91cd\u590d\u7684\u5143\u7d20\u53bb\u9664\uff0c\u4f46\u4fdd\u7559\u7b2c\u4e00\u4e2a\uff0c\u660e\u5929\u4ea4\u4f5c\u4e1a\uff0c\u6025\u6025\u6025\uff01\uff01\uff01\uff01\uff01\uff01\u671b\u5927\u795e\u5e2e\u52a9debugpackage com.equals.test;import java.util.HashSet;import java.util.Scanner;import java.util.Set;public class Circle { public static void main(String args[]) { System.out.println("Enter ten numbers : "); int[] a = new int[10]; Scanner reader = new Scanner(System.in); for (int i = 0; i num = new HashSet(); for(Integer n : a) { num.add(n); } for(Integer n : num) { System.out.println(n); } }}
\u5b9e\u73b0\u601d\u8def\uff1a\u5c06list\u5bf9\u8c61\u5faa\u73af\u4e24\u6b21\uff0c\u62ff\u5916\u5c42\u6570\u636e\u548c\u91cc\u9762\u7684\u6570\u636e\u5bf9\u6bd4\uff0c\u4e00\u6837\u7684\u5220\u9664\u5916\u5c42\uff08\u5916\u5c42\u5143\u7d20\u80af\u5b9a\u6bd4\u5185\u5b58\u7684\u9760\u524d\uff09\uff0c\u5982\u679c\u4e00\u6837\u7684\u8bdd\uff0c\u5220\u9664\u5916\u5c42\u6570\u636e\uff0c\u8fd9\u6837\u6700\u540e\u8f93\u51fa\u5916\u5c42\u6570\u636e\u7684list\uff0c\u7ed3\u679c\u5c31\u80fd\u4fdd\u8bc1\u552f\u4e00\u6027\uff0c\u5e76\u4e14\u4fdd\u7559\u4e86\u540e\u9762\u7684\u5143\u7d20\u3002
List list = new ArrayList(); Object s = new Object(); for(int i = 0;i<5;i++){ list.add(s); } for(int i = 0;i<list.size();i++){//\u5faa\u73aflist for(int j = i+1;j<list.size();j++){ if(list.get(i).equals(list.get(j))){ list.remove(i);//\u5220\u9664\u4e00\u6837\u7684\u5143\u7d20 i--; break; } } } for(int i = 0;i<list.size();i++){ System.out.println(list.get(i)); }
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]+" ");
tr.add(s[i]);
}
String[] s2= new String[tr.size()];
System.out.println("=====处理后======");
for(int i=0;i<s2.length;i++){
s2[i]=tr.pollFirst();//从TreeSet中取出元素重新赋给数组
System.out.print(s2[i]+" ");
}
}
}
不考虑性能和空处理,给个思路:
1、定义一个判断的方法,判断数组是否包含一个字符串
private static boolean isContain(String str,String[] result){
for(int i = 0;i<result.length;i++){
if(str.equals(result[i])){
return true;
}
}
return false;
}
2、主函数调用
for(int i = 0,j=0; i<strs.length;i++){
if(isContain(strs[i], result)){
continue;
}else{
result[j]=strs[i];
j++;
}
}
for(int i = 0;i<result.length;i++){
System.out.println(result[i]);
}
1、要想没重复的话,可以直接使用set类集合。
2、数组去重,方法很多,先排好序再去重就可以了。
希望对你有帮助。
可以自己写个方法,先将数组元素添加到list中,循环判断,list中包含了就不子添加,最后将list转换成数组
把它放到set集合中,在取出来。set集合中的元素不能重复
绛旓細1. 浣跨敤 `Scanner` 绫讳粠鏍囧噯杈撳叆涓鍙栦竴涓瓧绗︿覆锛屽瓨鍌ㄥ湪涓涓 `String` 绫诲瀷鐨勫彉閲忎腑銆2. 灏嗚瀛楃涓茶浆鎹负瀛楃鏁扮粍锛屼究浜庨愪釜璁块棶姣忎釜瀛楃骞惰繘琛岃浆鎹3. 閬嶅巻瀛楃鏁扮粍涓殑姣忎釜瀛楃锛屽鏋滆瀛楃鏄ぇ鍐欏瓧姣嶏紝鍒欏皢鍏惰浆鎹负灏忓啓瀛楁瘝锛涘鏋滆瀛楃鏄皬鍐欏瓧姣嶏紝鍒欏皢鍏惰浆鎹负澶у啓瀛楁瘝锛涘惁鍒欎笉鍋氬鐞嗐4. ...
绛旓細1 浣跨敤寰幆缁撴瀯 杩欑鏂规硶鏈鐏垫椿銆傚敮涓涓嶈冻鐨勫湴鏂瑰彲鑳藉氨鏄唬鐮佽緝澶 2 浣跨敤Object绫荤殑clone锛堬級鏂规硶锛 杩欑鏂规硶鏈绠鍗曪紝寰楀埌鍘熸暟缁勭殑涓涓壇鏈傜伒娲诲舰涔熸渶宸傛晥鐜囨渶宸紝灏ゅ叾鏄湪鏁扮粍鍏冪礌寰堝ぇ鎴栬呭鍒跺璞℃暟缁勬椂銆3 浣跨敤Systems鐨刟rraycopy杩欑鏂规硶琚憡涔嬮熷害鏈蹇紝骞朵笖鐏垫椿鎬т篃杈冨ソ锛屽彲浠ユ寚瀹氬師鏁扮粍鍚嶇О銆佷互...
绛旓細1锛夌敤java.util.Random绫伙細Random rad = new Random();int index = rad.nextInt(app.length);System.out.println(app[index]);2锛夌敤Math绫伙細int index = (int) Math.random() * app.length;System.out.println(app[index]);
绛旓細java.util.Arrays绫荤殑copyOf()鍜宑opyOfRange();public staic xxx[] copyOf(xxx[] original.int newLength)original鏄换鎰忕被鍨嬬殑鏁扮粍寮曠敤锛屾寚鍚戞簮鏁扮粍锛宯ewLength瑕佽冭礉鐨勯暱搴︼紟public static xxx[] copyOfRange(xxx[] original,int from,int to)original鏄换鎰忕被鍨嬬殑鏁扮粍寮曠敤锛屾寚鍚戞簮鏁扮粍锛屽皢鎸囧畾婧...
绛旓細1. 浣跨敤toCharArray鏂规硶锛歋tring绫荤殑toCharArray鏂规硶鍙互灏嗗瓧绗︿覆杞崲涓哄瓧绗︽暟缁銆傝繖鏄渶绠鍗曠洿鎺ョ殑鏂瑰紡锛屽畠浼氬皢瀛楃涓蹭腑鐨勬瘡涓瓧绗﹁浆鎹负鏁扮粍鐨勪竴涓厓绱犮備緥濡傦細java String str = "Hello";char[] charArray = str.toCharArray; // str鐜板湪鏄竴涓瓧绗︽暟缁勶紝鍖呭惈"H", "e", "l", "l", "o"2. ...
绛旓細涓鏄湪鍒濆鍖瀛楃涓鏃讹紝鐢String鐨鏋勯犳柟娉曞垱寤猴細渚嬶細char[] chars=new char[]{'a','b','c','d'};String string=new String(chars);浜屾槸鐢⊿tring绫荤殑闈欐佹柟娉晇alueOf()渚嬶細char[] chars=new char[]{'a','b','c','d'};String string=String.valueOf(chars);鍏蜂綋浣跨敤鏂规硶浣犲彲浠ュ弬瑙...
绛旓細public static void main(String[] args) { // TODO Auto-generated method stub String[] strs=new String[]{"100","202","abcd","%^&","int"}; List<Integer> ints=new ArrayList<>(); for(String str:strs){ Integer num=null; try { num=Integer.parseInt...
绛旓細鏁扮粍闀垮害鏄笉鍙彉鐨勩傝鍘婚櫎绌哄笺傞渶瑕佹墜鍔ㄨ缃垚null ,澶嶅埗鍒伴泦鍚堢殑璇 鐢 寰幆璧嬪煎氨鍙互浜嗐傜劧鍚鎶涓嶉渶瑕佺殑 绌哄肩殑閭d釜鍏冪礌 remove();灏卞彲浠ヤ簡銆
绛旓細1銆Java鏁扮粍鐨勮娉曪細 String[鏁扮粍涓嬫爣]锛 Java鏁扮粍鐨勪笅鏍囨槸浠0寮濮嬬殑銆2銆佽幏鍙鏁扮粍涓殑鎵鏈夊厓绱狅紝浼氱敤鍒版暟缁勭殑閬嶅巻锛岄氬父鐢╢or寰幆銆3銆佽緭鍑烘墦鍗版暟缁勪腑鐨勬墍鏈夊厓绱狅紝骞剁敤鈥滐紝鈥濆垎闅斻4銆佸彲浣跨敤瀹氫箟涓変釜瀛楃涓k锛宲锛宖鍜宼涓虹鍑犱釜瀛楃涓层5銆佸彲浠ヤ娇鐢ˋrrays绫讳腑binarySearch(Object[] a, Object key)...
绛旓細public static void main(String[] args){ String aString;String bString="";StringBuffer answer = new StringBuffer(bString);aString="鎴戞槸娴嬭瘯瀛楃涓";System.out.println("澶嶅埗鍓嶅瓧绗︿覆A:"+aString+"\n瀛楃涓睟:"+bString);//杩欓噷寮濮嬪鍒跺瓧绗︿覆 for(int i=0;i<aString.length();i++)...