list 删除元素 以一个list中的元素(或数组中的元素)为下标

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36135928/article/details/84401497

以一个list中的元素为下标,或者用一个数组中的元素为下标,来删除某个list中对应下标的元素。

package cn.iponkan.test;

import static org.junit.Assert.*;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;

/**
 * @author dongtangqiang
 *
 */
public class TestRemoveListElement {

  /*
   * 以一个indexList中元素为下标,删除另一个toBeDeleteList中对应下标的元素
   * 
   * 期望在 toBeDeleteList中删除以下元素: [2]Wednesday [5]Saturday [7]January [15]Auguest
   * [18]November
   */

  // 下标list
  List<Integer> indexList = new ArrayList<Integer>() {
    private static final long serialVersionUID = 1L;
    {
      add(2);
      add(5);
      add(5);
      add(18);
      add(7);
      add(15);
      add(7);
    }
  };

  // 待删除list
  List<Object> toBeDeleteList = new ArrayList<Object>() {
    private static final long serialVersionUID = 1L;
    {
      add("Monday");
      add("Tuesday");
      add("Wednesday");
      add("Thursday");
      add("Friday");
      add("Saturday");
      add("Sunday");
      add("January");
      add("February");
      add("March");
      add("April");
      add("May");
      add("June");
      add("July");
      add("Auguest");
      add("September");
      add("October");
      add("November");
      add("December");
    }
  };

  // 下标数组
  int[] indexArray = new int[] {
      2, 5, 5, 18, 7, 15, 7 };

  /**
   * 以一个list元素为下标,删除另一个list中对应元素
   * 
   * @param indexList
   *          下标list
   * @param toBeDeleteList
   *          待删除元素
   * @return 删除元素后的list
   */
  public List<Object> removeElementByList(List<Integer> indexList, List<Object> toBeDeleteList)
      throws RemoveElementException {
    // 下标list去重操作
    HashSet indexSet = new HashSet(indexList);
    indexList.clear();
    indexList.addAll(indexSet);

    // 下标list长度不能大于待删除list的长度
    if (indexList.size() > toBeDeleteList.size()) {
      throw new RemoveElementException(indexList.size(), toBeDeleteList.size());
    }

    // 下标list排序操作
    Collections.sort(indexList);

    /**
     * list移除一个元素其后的元素会自动向前移动一位,删除一个元素,移动后,不能影响后面继续要删除元素的下标,从后往前删就避免了下标变化的问题.
     */
    for (int i = indexList.size() - 1; i >= 0; i--) {
      if (indexList.get(i) >= toBeDeleteList.size()) {
        throw new RemoveElementException(i);
      }
      /**
       * 取到Integer类型的下标,会发生list集合数据删除不了,这里做强制类型转换. remove(Object object);
       * remove(int index);
       */
      toBeDeleteList.remove((int) indexList.get(i));
    }

    return toBeDeleteList;
  }

  /**
   * 以一个array元素为下标,删除另一个list中对应元素
   * 
   * @param indexArray
   * @param toBeDeleteList
   * @return
   * @throws RemoveElementException
   */
  public List<Object> removeElementByArray(int[] indexArray, List<Object> toBeDeleteList)
      throws RemoveElementException {
    HashSet indexSet = new HashSet();
    for (int i = 0; i < indexArray.length; i++) {
      indexSet.add(indexArray[i]);
    }

    Object[] array = indexSet.toArray();

    if (array.length > toBeDeleteList.size()) {
      throw new RemoveElementException(array.length, toBeDeleteList.size());
    }

    Arrays.sort(array);

    for (int i = array.length - 1; i >= 0; i--) {
      if ((int) (array[i]) >= toBeDeleteList.size()) {
        throw new RemoveElementException(i);
      }
      toBeDeleteList.remove((int) (array[i]));
    }

    return toBeDeleteList;
  }

  @Test
  public void testRemoveElementByList() {
    try {
      List<Object> result = removeElementByList(indexList, toBeDeleteList);
      Assert.assertEquals(14, result.size());
    } catch (RemoveElementException e) {
      e.printStackTrace();
      fail();
    }
  }

  @Test
  public void testRemoveElementByList2() {
    try {
      indexList.add(1);
      indexList.add(3);
      indexList.add(4);
      indexList.add(6);
      indexList.add(8);
      indexList.add(9);
      indexList.add(10);
      indexList.add(11);
      indexList.add(12);
      indexList.add(13);
      indexList.add(14);
      indexList.add(16);
      indexList.add(17);
      indexList.add(19);
      indexList.add(20);
      removeElementByList(indexList, toBeDeleteList);
      fail();
    } catch (RemoveElementException e) {
      Assert.assertTrue(e != null);
    }
  }

  @Test
  public void testRemoveElementByList3() {
    try {
      indexList.add(19);
      removeElementByList(indexList, toBeDeleteList);
      fail();
    } catch (RemoveElementException e) {
      Assert.assertTrue(e != null);
    }
  }
  
  
  @Test
  public void removeElementByArray(){
    try {
      List<Object> result = removeElementByArray(indexArray, toBeDeleteList);
      Assert.assertEquals(14, result.size());
    } catch (RemoveElementException e) {
      e.printStackTrace();
      fail();
    }
  }
  
  @Test
  public void removeElementByArray2() {
    try {
      indexArray = new int[] {1, 2,3,4, 5, 5, 6,8,9,10,11,12,13,14,15,16,17,19,18, 7, 15, 7 };
      removeElementByArray(indexArray, toBeDeleteList);
      fail();
    } catch (RemoveElementException e) {
      Assert.assertTrue(e != null);
    }
  }
  
  @Test
  public void removeElementByArray3() {
    try {
      indexArray = new int[] {3,19,7};
      removeElementByArray(indexArray, toBeDeleteList);
      fail();
    } catch (RemoveElementException e) {
      Assert.assertTrue(e != null);
    }
  }
  
}

class RemoveElementException extends Exception {
  private static final long serialVersionUID = 1L;

  public RemoveElementException() {
    super();
  }

  public RemoveElementException(int indexSize, int toBeDeletListSize) {
    super(MessageFormat.format("下标长度“{0}”,大于待删除list的长度“{1}”", indexSize, toBeDeletListSize));
  }

  public RemoveElementException(int index) {
    super(MessageFormat.format("下标“{0}”超过待删除list的长度", index));
  }

}

猜你喜欢

转载自blog.csdn.net/qq_36135928/article/details/84401497