求教个关于数组的问题 String str[]={"a","a","b","c"};把上面数组中重复的值a,a拿出来保存在一个数组,不重复的值b,c保存在一个数组中。这个是题意。。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 @Test public void test(){ String[] strs={"a","a","b","c"}; Set<String> reset=new HashSet<String>();//重复的数组 Set<String> noreset=new HashSet<String>();//不重复的数组 for(int i=0;i<strs.length;i++){//遍历旧数组 if(!noreset.add(strs[i])){//如果存入不成功 noreset.remove(strs[i]); reset.add(strs[i]); } } String[] s1=new String[reset.size()];//重复的数组 String[] s2=new String[noreset.size()];//不重复的数组 System.out.println(reset.toArray(s1)); System.out.println(noreset.toArray(s2)); }我想了一下 这里用set的特点可能简单点 不保证是最好的 有问题可以再交流 @Test public void test(){ String[] strs={"a","a","b","c"}; Set<String> reset=new HashSet<String>();//重复的数组 Set<String> noreset=new HashSet<String>();//不重复的数组 for(int i=0;i<strs.length;i++){//遍历旧数组 if(!noreset.add(strs[i])){//如果存入不成功 noreset.remove(strs[i]); reset.add(strs[i]); } } String[] s1=reset.toArray(new String[reset.size()]);//重复的数组 String[] s2=reset.toArray(new String[noreset.size()]);//不重复的数组 //打印 System.out.println(s1); System.out.println(s2); }改成这样吧 这样s1、s2就是最后需要的数组了 上面的那个 只是把数组打印了 其实打印流也没啥意义 打出来肯定是地址值 第一个元素先放进一个数组A中,后面的元素取出时都和这个数组A遍历查询一下,有重复的放另一个数组B中,结束后,A中把和B中一样的减掉 如果String[] strs={"a","a","a","b","c"};在测试一下 public static void test() { String[] strs = {"a", "a", "a", "b", "c"}; List copy = Arrays.asList(strs.clone()); List reset =new ArrayList();//重复的数组 List noreset =new ArrayList();//不重复的数组 for (int i = 0; i < strs.length; i++) {//遍历旧数组 copy.set(i, null); if (copy.contains(strs[i])) reset.add(strs[i]); else noreset.add(strs[i]); copy.set(i, strs[i]); } //打印 for (Object a : reset) System.out.println("s1:" + a); for (Object a : noreset) System.out.println("s2:" + a); } @Test public void test(){ String[] strs={"a","a","a","a","b","c"}; Set<String> reset=new HashSet<String>();//重复的数组 Set<String> noreset=new HashSet<String>();//不重复的数组 for(int i=0;i<strs.length;i++){//遍历旧数组 if(!noreset.contains(strs[i])){//如果存入成功 noreset.add(strs[i]); }else{ reset.add(strs[i]); } } String[] s1=reset.toArray(new String[reset.size()]);//重复的数组 String[] s2=noreset.toArray(new String[noreset.size()]);//不重复的数组 }我这样就行了 是吧 愁... jsp网站如何从外网访问 struts2 数据提交问题,很simple sql批量插入数据简单问题!!! tomcat下的servlet只执行一次? 如何将Tomcat、MySQL开发的Java Web程序制作成安装光盘?比如用installshield. 如何显示IP jsp中怎么上传文件啊? 谁有用JAVA写的树形控件,我急用?区区100分,不成敬意。 多条数据同时更新如何实现??分不够可以说。 急求SSM开发框架 list java 求交集 求助---mybatis注解问题
public void test(){
String[] strs={"a","a","b","c"};
Set<String> reset=new HashSet<String>();//重复的数组
Set<String> noreset=new HashSet<String>();//不重复的数组
for(int i=0;i<strs.length;i++){//遍历旧数组
if(!noreset.add(strs[i])){//如果存入不成功
noreset.remove(strs[i]);
reset.add(strs[i]);
}
}
String[] s1=new String[reset.size()];//重复的数组
String[] s2=new String[noreset.size()];//不重复的数组
System.out.println(reset.toArray(s1));
System.out.println(noreset.toArray(s2));
}
我想了一下 这里用set的特点可能简单点 不保证是最好的 有问题可以再交流
@Test
public void test(){
String[] strs={"a","a","b","c"};
Set<String> reset=new HashSet<String>();//重复的数组
Set<String> noreset=new HashSet<String>();//不重复的数组
for(int i=0;i<strs.length;i++){//遍历旧数组
if(!noreset.add(strs[i])){//如果存入不成功
noreset.remove(strs[i]);
reset.add(strs[i]);
}
}
String[] s1=reset.toArray(new String[reset.size()]);//重复的数组
String[] s2=reset.toArray(new String[noreset.size()]);//不重复的数组
//打印
System.out.println(s1);
System.out.println(s2);
}
改成这样吧 这样s1、s2就是最后需要的数组了 上面的那个 只是把数组打印了 其实打印流也没啥意义 打出来肯定是地址值
如果String[] strs={"a","a","a","b","c"};在测试一下
public static void test() {
String[] strs = {"a", "a", "a", "b", "c"};
List copy = Arrays.asList(strs.clone());
List reset =new ArrayList();//重复的数组
List noreset =new ArrayList();//不重复的数组
for (int i = 0; i < strs.length; i++) {//遍历旧数组
copy.set(i, null);
if (copy.contains(strs[i])) reset.add(strs[i]);
else noreset.add(strs[i]);
copy.set(i, strs[i]);
}
//打印
for (Object a : reset) System.out.println("s1:" + a);
for (Object a : noreset) System.out.println("s2:" + a);
}
@Test
public void test(){
String[] strs={"a","a","a","a","b","c"};
Set<String> reset=new HashSet<String>();//重复的数组
Set<String> noreset=new HashSet<String>();//不重复的数组
for(int i=0;i<strs.length;i++){//遍历旧数组
if(!noreset.contains(strs[i])){//如果存入成功
noreset.add(strs[i]);
}else{
reset.add(strs[i]);
}
}
String[] s1=reset.toArray(new String[reset.size()]);//重复的数组
String[] s2=noreset.toArray(new String[noreset.size()]);//不重复的数组
}我这样就行了 是吧 愁...