class A{
private String name;
private int age;
}
List<A> list=new ArrayList<A>,假设list中有1000条数据,要用最快的方法把age=1删除,age=0留下

解决方案 »

  1.   

    看LZ的意思,就是说age是递增的,能不能把list转换成map来存储,正好age是key,name是value。这样直接调用remove(key)方法来删掉age呢,学java没多久,错了请包涵
      

  2.   

    这真是笔试题吗?唬人的吧!
    首先,A类的属性都是私有的,如何赋值和取值?如果不能取值,如何做判断?
    最快的方法?
    除了遍历remove还真不知有什么能更快的方法。
    顶多考一个remove之后注意容量的变化,索引也跟着变,所以最好倒序删除。
      

  3.   

    重写Class的equals方法  如果age相同则返回true  然后调用然后new一个age为1的对象test  list.remove(test)就行了吧
      

  4.   

    age属性是私有的,怎么取啊?
      

  5.   

    我晕  我回复的帖子怎么没有  这CSDN什么情况?
    重写classA的equals方法  如果两个对象的age相同返回true
    然后再new一个List list2  里面只有一个age为1的对象
    最后list.removeAll(list2);就行了
      

  6.   

    12楼的,人家list里面放的对象,你的那个适合里面放的为值的
      

  7.   

    给你上代码吧:
    class A {
    private String name; public String getName() {
    return name;
    } public A(String name, int age) {
    super();
    this.name = name;
    this.age = age;
    } public void setName(String name) {
    this.name = name;
    } public int getAge() {
    return age;
    } public void setAge(int age) {
    this.age = age;
    } private int age; @Override
    public boolean equals(Object obj) {
    A obj1 = null;
    if (obj instanceof A)
    obj1 = (A) obj;
    return this.age == obj1.age;
    }
    } List<A> list = new ArrayList<A>(); private void test() {
    List<A> listToRemove = new ArrayList<LuceneDaoImpl.A>();
    listToRemove.add(new A("", 0));
    list.remove(listToRemove);
    }
      

  8.   

    看看这个:http://blog.csdn.net/comaple/article/details/7265375
      

  9.   

    你没理解清楚我的意思  我是说list2里面放的是age为1的A的对象  不是Integer对象
    16的代码跟我的意思一样  但我记得好像是list.removeAll(otherList)
      

  10.   

    思路分析:首先比较直观的方式就是进行list循环,然后remove年龄等于1的,但是由于list的删除会涉及到数组内元素移动等内存操作(长度越大,效率越低),所以时间效率会低,如果实际需求就是只存在age 0 or 1的情况,那么我觉得可以先进行排序,然后找到0和1的分界坐标,直接截取,只不过会牺牲一些空间复杂度。public class TestListSort { public static String[] strArray = { "a", "b", "c", "d", "e", "f", "g" };// 生成name用 public static void main(String[] args) { // 生成测试用的数据
    ArrayList<A> list = new ArrayList();
    String str = "";
    for (int i = 0; i < 300000; i++) {// 数量300000个
    shuffleSort(strArray);
    str = "";
    for (int j = 0; j < strArray.length - 1; j++) {
    str += strArray[j];
    }
    list.add(new A(str, i % 2 == 0 ? 1 : 0));// name,age
    }
    System.out.println("生成测试用的数据完毕:" + list.size()); // ====================================================================
    long l1 = System.currentTimeMillis();

    /** 第一种方法 先排序,在截取 */
    int mid = bucketSort(list, 0, 2);// 排序,并返回age 0和age 1的分界点
    ArrayList<A> listEnd = new ArrayList();// 最终集合
    for (int i = 0; i < mid; i++) {
    listEnd.add(list.get(i));
    }
    /** 第二种方法 循环,删除 */
    /*
    int count = 0;
    for (int i = 0; i < list.size(); i++) {
    int age = list.get(i).getAge();
    if (age == 1) {
    list.remove(i);
    count++;
    }
    }*/
    long l2 = System.currentTimeMillis();
    System.out.println("操作时间:" + ((l2 - l1) / 1000L) + "秒");
    } /**
     * 计数排序
     */
    public static int bucketSort(ArrayList<A> list, int min, int max) { int[] tmp = new int[list.size()];
    int[] buckets = new int[max - min];
    // 计算每个元素在序列出现的次数
    for (int i = 0; i < list.size(); i++) {
    buckets[list.get(i).getAge() - min]++;
    }
    for (int i = 1; i < max - min; i++) {
    buckets[i] = buckets[i] + buckets[i - 1];
    }
    int mid = buckets[0];
    System.out.println("分界点: " + mid);
    ArrayList<A> listNew = new ArrayList(Arrays.asList(new Object[list
    .size()]));
    Collections.copy(listNew, list);
    // 根据buckets数组中的信息将待排序列的各元素放入相应位置
    for (int k = list.size() - 1; k >= 0; k--) {
    int tmp1 = listNew.get(k).getAge();
    list.set((--buckets[tmp1 - min]), listNew.get(k));
    }
    return mid;
    } public static void shuffleSort(String[] data) {
    for (int i = 0; i < data.length - 1; i++) {
    int j = (int) (data.length * Math.random());
    swap(data, i, j);
    }
    } public static void swap(String[] data, int i, int j) {
    if (i == j) {
    return;
    }
    String tmp = data[i];
    data[i] = data[j];
    data[j] = tmp;
    }
    }class A { private String name;
    private int age; public A(String name, int age) {
    super();
    this.name = name;
    this.age = age;
    } public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } public int getAge() {
    return age;
    } public void setAge(int age) {
    this.age = age;
    } public String toString() {
    return "name: " + name + ",age:" + age;
    }
    }
    两种方法我简单测试了一下:
    第一种,300000的数据需要不足1秒;
    第二种,300000的数据需要17秒;
      

  11.   

    age不是年龄吗?sex才可能只有1,0吧?
      

  12.   

    list2里面放的是age为1的A的对象 不是Integer对象
    16的代码跟我的意思一样 但我记得好像是list.removeAll(otherList)
      

  13.   

    20楼和28楼,受教了。
    反射我知道的,spring用的很多,我自己也经常用反射动态加载类。
    我的意思是本处如果非要用反射,就有些本末倒置了。
      

  14.   

    不知道是不是这个意思:你拿去看看,呵呵呵,是的话别忘了给点分哦,兄弟public class Test {
    public static void main(String[] args) {
    //这个是你说的装有1000条数据的list
    List<A> list=new ArrayList<A>();
    //把list中的数据清理出来放入该list1中
    List<A> list1=new ArrayList<A>();
    A a1=new A();
    for(A a:list){
    if(a.getAge()==0){
    a1.setAge(a.getAge());
    a1.setName(a.getName());
    list1.add(a1);
    }
    }
    for(A a:list1){
    System.out.println(a.getName()+"---"+a.getAge());
    }

    }}
    class A{
    private String name;
    private int age;
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    }
      

  15.   

    我觉得直接循环就可以了,时间复杂度为O(n)。别的没想到。代码贴出来,看看是不是你要的结果。package com.walkman.test;
    /**
     * author: tank
     * date: 2012-2-24
     * content: get rid of the element in the arraylist
     */
    import java.util.ArrayList;
    import java.util.List;class A {
    private int age;
    private String name; public A(int age, String name) {
    super();
    this.age = age;
    this.name = name;
    } public int getAge() {
    return age;
    } public void setAge(int age) {
    this.age = age;
    } public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    }
    }public class TestValueInArrayList { public static void main(String[] args) {
    List<A> list = new ArrayList<A>();
    A a1 = new A(1,"www");
    A a2 = new A(2,"ddd");
    A a3 = new A(3,"sss");
    list.add(0, a1);
    list.add(1, a2);
    list.add(2, a3);

    new TestValueInArrayList().deleteElementOfArrayList(list);

    System.out.println(list.size());
    }

    void deleteElementOfArrayList(List<A> list) {
    if(0 == list.size()) {
    return;
    }

    for(int i = 0; i <list.size(); i++) {
    if(list.get(i).getAge() == 1) {
    list.remove(i);
    }
    }
    }}
      

  16.   

    终于有空写点代码了,这是我按照前后换位、删除后面数据的思路写的,你看看吧public static void testDeletList1()
        {
         List<Integer> nums = new ArrayList<Integer>();
         for( int i=0; i<100000; i++ )
         {
         nums.add( 0 );
         nums.add( 1 );
         }
         Calendar c = Calendar.getInstance();
         ShowTime( c.getTime() );
         int i = 0;  
         while( i < nums.size() )
         {
         if(nums.get(i).intValue()==0)
         {
         int j = nums.size() - 1;
         while( (j>=i))
         {
         if(nums.get(j).intValue()!=0)
         {
         nums.set(i, nums.get(j) );
         nums.remove(j);
         break;
         }
         nums.remove(j);
         j--;
         }
         }
         i++;
         }
         c = Calendar.getInstance();
         ShowTime( c.getTime() );
         for( int j=0; j<nums.size(); j++)
         {
         if(nums.get(j).intValue()==0)
         System.out.println( nums.get(j) );
         }
         System.out.println(nums.size());
        
        }