java中ArrayList是怎么用的,当然我也尝试了Vector,但是结果一样?如下面程序:
packageGenera_decis_tree;importjava.util.ArrayList;importjava.util.HashMap;importjava...
package Genera_decis_tree;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;
public class EveryStepGain {
public void test(){
ArrayList<String> al=new ArrayList<String>();
ArrayList<String> bl=new ArrayList<String>();
al.add("a");
al.add("b");
al.add("c");
al.add("d");
// HashMap hm=new HashMap();
// hm.put(new String("age"), "youth");
// hm.put(new String("age"), "mid-aged");
// System.out.println("age="+hm.get("age"));
for(int i=0;i<al.size();i++){
bl.add(al.get(i));
System.out.print("来自a表------"+al.get(i)+"\t");
al=this.subSet(al, bl);
//al.remove(i);
}
for(int j=0;j<bl.size();j++){
System.out.println(bl.get(j));
}
}
public ArrayList<String> subSet(ArrayList<String> a,ArrayList<String> b){
ArrayList<String> temp=new ArrayList<String>();
for(int i=0;i<b.size();i++){
if(a.contains(b.get(i))){
a.remove(a.indexOf(b.get(i)));
}
}
temp=a;
return a;
}
public static void main(String[] args){
EveryStepGain erv=new EveryStepGain();
erv.test();
}
}
我是想一边删除al表中的元素,一边加入到bl表中,但是删除后al 每次都被删除了两个元素,而bl表中最终也只有两个不连续的元素,为什么会被删除两次?? 展开
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;
public class EveryStepGain {
public void test(){
ArrayList<String> al=new ArrayList<String>();
ArrayList<String> bl=new ArrayList<String>();
al.add("a");
al.add("b");
al.add("c");
al.add("d");
// HashMap hm=new HashMap();
// hm.put(new String("age"), "youth");
// hm.put(new String("age"), "mid-aged");
// System.out.println("age="+hm.get("age"));
for(int i=0;i<al.size();i++){
bl.add(al.get(i));
System.out.print("来自a表------"+al.get(i)+"\t");
al=this.subSet(al, bl);
//al.remove(i);
}
for(int j=0;j<bl.size();j++){
System.out.println(bl.get(j));
}
}
public ArrayList<String> subSet(ArrayList<String> a,ArrayList<String> b){
ArrayList<String> temp=new ArrayList<String>();
for(int i=0;i<b.size();i++){
if(a.contains(b.get(i))){
a.remove(a.indexOf(b.get(i)));
}
}
temp=a;
return a;
}
public static void main(String[] args){
EveryStepGain erv=new EveryStepGain();
erv.test();
}
}
我是想一边删除al表中的元素,一边加入到bl表中,但是删除后al 每次都被删除了两个元素,而bl表中最终也只有两个不连续的元素,为什么会被删除两次?? 展开
2个回答
2013-03-12
展开全部
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;
public class EveryStepGain {
public void test(){
ArrayList<String> al=new ArrayList<String>();
ArrayList<String> bl=new ArrayList<String>();
al.add("a");
al.add("b");
al.add("c");
al.add("d");
// HashMap hm=new HashMap();
// hm.put(new String("age"), "youth");
// hm.put(new String("age"), "mid-aged");
// System.out.println("age="+hm.get("age"));
for(int i=al.size()-1;i>-1;i--){
bl.add(al.get(i));
System.out.print("来自a表------"+al.get(i)+"\t");
al=this.subSet(al, bl);
//al.remove(i);
}
for(int j=0;j<bl.size();j++){
System.out.println(bl.get(j));
}
}
public ArrayList<String> subSet(ArrayList<String> a,ArrayList<String> b){
ArrayList<String> temp=new ArrayList<String>();
for(int i=b.size()-1;i>-1;i--){
if(a.contains(b.get(i))){
a.remove(a.indexOf(b.get(i)));
}
}
temp=a;
return a;
}
public static void main(String[] args){
EveryStepGain erv=new EveryStepGain();
erv.test();
}
}
试试这样的写法
import java.util.HashMap;
import java.util.Vector;
public class EveryStepGain {
public void test(){
ArrayList<String> al=new ArrayList<String>();
ArrayList<String> bl=new ArrayList<String>();
al.add("a");
al.add("b");
al.add("c");
al.add("d");
// HashMap hm=new HashMap();
// hm.put(new String("age"), "youth");
// hm.put(new String("age"), "mid-aged");
// System.out.println("age="+hm.get("age"));
for(int i=al.size()-1;i>-1;i--){
bl.add(al.get(i));
System.out.print("来自a表------"+al.get(i)+"\t");
al=this.subSet(al, bl);
//al.remove(i);
}
for(int j=0;j<bl.size();j++){
System.out.println(bl.get(j));
}
}
public ArrayList<String> subSet(ArrayList<String> a,ArrayList<String> b){
ArrayList<String> temp=new ArrayList<String>();
for(int i=b.size()-1;i>-1;i--){
if(a.contains(b.get(i))){
a.remove(a.indexOf(b.get(i)));
}
}
temp=a;
return a;
}
public static void main(String[] args){
EveryStepGain erv=new EveryStepGain();
erv.test();
}
}
试试这样的写法
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询