请问下 怎样排除 数组和list里 的相同值? 请问下 怎样排除 数组里 的相同值?比如说 a[]={2,3,4,4,5};将4排除掉一个? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用set吧,每个不同的关键字只能有一个值,关键值不能相同,否则就只有写程序去掉了。 import java.util.HashSet;public class testSet { public static void main(String []args){ int a[] = {2,3,4,4,5}; HashSet hashset = new HashSet(); for (int i=0 ; i<a.length ; i++){ hashset.add(a[i]); } System.out.println(hashset); }} 哦 谢谢了 知道了 但是怎么取出Hashset里面得值 再转化为int 啊 没有这个方法啊? import java.util.HashSet;import java.util.Iterator;public class testSet { public static void main(String []args){ int a[] = {2,3,4,4,5}; HashSet<Integer> hashset = new HashSet(); for (int i=0 ; i<a.length ; i++){ hashset.add(a[i]); } //System.out.println(hashset); Iterator it = hashset.iterator(); while(it.hasNext()){ System.out.println(it.next()); } }} 如果是list中有重复的,那么可以将元素取出,然后放入到set中,set是无序且不允许重复的,这样可以达到你的目的。如果是数组中有重复的,当然也可以像list一样存入set中解决问题,也可用程序,用一个新的数组存放从该数组中取出的元素,去掉重复的即可 那 存入hashset后 要怎样取出里面的值 转化为Int用呢? HashSet<Integer> 已經定死了 就是int的了~ 拿過來直接用就行了 直接用?咋弄不起喃说是Object喃比如 int a[] = {0,0,1}; HashSet<Integer> hashset = new HashSet<Integer>(); for (int i=0 ; i<a.length ; i++){ hashset.add(a[i]); }hashset 是[0,1] 咋取出 0 或则 1啊? //如:清除重复状态 String[] args = status.split(","); HashSet hashset = new HashSet(); for(int j=0;j<args.length;j++) { hashset.add(args[j]); } Iterator it = hashset.iterator(); String endStatus = ""; while(it.hasNext()){ endStatus += it.next()+","; } endStatus = endStatus.substring(0, endStatus.length()-1); 在主JFrame怎么打开子JFrame? greedy match和possessive match什麽意思? JTable救助 请问在这个程序中的main方法中到底有几个并发线程啊? 请教一个问题 新手告急:类的重用 你散,俺也散,大家都散,为什么?(见里面 ^^) 请教各位,一个小迷惑,为什么main函数也需要throws Throwable? JButton的click event 关于学习 有关TextArea类的问题??? native2ascii 转码问题
import java.util.HashSet;public class testSet {
public static void main(String []args){
int a[] = {2,3,4,4,5};
HashSet hashset = new HashSet();
for (int i=0 ; i<a.length ; i++){
hashset.add(a[i]);
}
System.out.println(hashset);
}
}
import java.util.HashSet;
import java.util.Iterator;public class testSet {
public static void main(String []args){
int a[] = {2,3,4,4,5};
HashSet<Integer> hashset = new HashSet();
for (int i=0 ; i<a.length ; i++){
hashset.add(a[i]);
}
//System.out.println(hashset);
Iterator it = hashset.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
如果是数组中有重复的,当然也可以像list一样存入set中解决问题,也可用程序,用一个新的数组存放从该数组中取出的元素,去掉重复的即可
比如
int a[] = {0,0,1};
HashSet<Integer> hashset = new HashSet<Integer>();
for (int i=0 ; i<a.length ; i++){
hashset.add(a[i]);
}hashset 是[0,1] 咋取出 0 或则 1啊?
String[] args = status.split(",");
HashSet hashset = new HashSet();
for(int j=0;j<args.length;j++)
{
hashset.add(args[j]);
}
Iterator it = hashset.iterator();
String endStatus = "";
while(it.hasNext()){
endStatus += it.next()+",";
}
endStatus = endStatus.substring(0, endStatus.length()-1);