以一个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*/// 下标listList<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);}};// 待删除listList<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;}@Testpublic void testRemoveElementByList() {try {List<Object> result = removeElementByList(indexList, toBeDeleteList);Assert.assertEquals(14, result.size());} catch (RemoveElementException e) {e.printStackTrace();fail();}}@Testpublic 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);}}@Testpublic void testRemoveElementByList3() {try {indexList.add(19);removeElementByList(indexList, toBeDeleteList);fail();} catch (RemoveElementException e) {Assert.assertTrue(e != null);}}@Testpublic void removeElementByArray(){try {List<Object> result = removeElementByArray(indexArray, toBeDeleteList);Assert.assertEquals(14, result.size());} catch (RemoveElementException e) {e.printStackTrace();fail();}}@Testpublic 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);}}@Testpublic 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));}}

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

  1. C语言试题三十七之求除一个2×m整型二维数组中最大元素的值,并将此值返回调用函数。

    1. 题目 请编写一个函数function,它的功能是:求除一个2×m整型二维数组中最大元素的值,并将此值返回调用函数. 2 .温馨提示 C语言试题汇总里可用于计算机二级C语言笔试.机试.研究生复试中 ...

  2. C语言:编写一个函数,计算二维数组中的最大元素,数组以指针的方式传递

    /*编写一个函数,计算二维数组中的最大元素,数组以指针的方式传递*/ #include<stdio.h> #define N 4 #define M 3 int findmax(int ( ...

  3. C语言试题十六之写删除字符串中指定下标的字符。其中,a指向原字符串,删除后的字符串存放在b所指的数组中,n中存放指定的下标。

    1. 题目 请编写一个函数void function(char a[],char b[], int n),其功能是:删除字符串中指定下标的字符.其中,a指向原字符串,删除后的字符串存放在b所指的数组中 ...

  4. 输入一个整数数组,实现一个函数,来调整该数组中数字的顺序使得数组中所有的奇数位于数组的前半部分,所有偶数位于数组的后半部分。

    输入一个整数数组,实现一个函数,来调整该数组中数字的顺序使得数组中所有的奇数位于数组的前半部分,所有偶数位于数组的后半部分. 我们首先会想到常规方法:创建一个临时数组,遍历所给数组中的所有元素,将偶数 ...

  5. C++ 读取txt文件中数据并存入数组中

    #include<iostream> #include<iomanip> #include<fstream> using namespace std; int ma ...

  6. c语言实现在数组中找一个数字显示,C语言找出数组中的特定元素的算法解析

    问题描述:一个int数组,里面数据无任何限制,要求求出所有这样的数a[i],其左边的数都小于等于它,右边的数都大于等于它.能否只用一个额外数组和少量其它空间实现. 思路:如果能用两个辅助数组,那么相对 ...

  7. es6删除对象的属性_javascript - 按对象属性从数组中删除对象

    javascript - 按对象属性从数组中删除对象 var listToDelete = ['abc', 'efg']; var arrayOfObjects = [{id:'abc',name:' ...

  8. 数组中某个元素相同的去重_几种去除数组中重复元素的方法、数组去重

    工作中遇到的一个问题,就是去除数组中重复的元素,记录一下几种有效的方法: 第一种思路:遍历要删除的数组arr, 把元素分别放入另一个数组tmp中,在判断该元素在arr中不存在才允许放入tmp中. 去除 ...

  9. java 数组 包含_Java中高效的判断数组中某个元素是否存在详解

    一.检查数组是否包含某个值的方法 使用List public static boolean useList(String[] arr, String targetValue) { return Arr ...

  10. java中,在一个有序数组中插入元素,使得数组保持有序排列

    //已知有一个数组,/*数组里面的元素有a b c d e f y z,从控制台中随机输入一个字母,按照升序的顺序插入到该数组中并且遍历输出.*/public static void test1(){ ...

最新文章

  1. CL_GUI_PICTURE
  2. oracle 数据库字段名与实体类字段名称不匹配的处理方法
  3. 13-11-27新的一天
  4. ftp邮箱里的文件无法连接服务器,服务器FTP不能连接的一些解决方法 - 新网数据 - 主机,域名,邮箱提供商 - www.nIDC.cn...
  5. Apache Cassandra和Apache Ignite:分布式数据库的明智之选
  6. mysql 停数据库_mysql数据库突然停了
  7. 湖北文理学院学位计算机考试,湖北文理学院学位计算机考试试题答案.doc
  8. IOS view的圆角和阴影并存
  9. 时隔一年,微信发布WeUI2.0.0,各种buff加身
  10. 目标检测-2019年4篇目标检测算法最佳综述
  11. javax.crypto.BadPaddingException: Given final block not properly padded
  12. 网络基础知识:10M、50M、100M宽带下载速率一般是多少?
  13. 图纸识别自动生成BOM清单的方法
  14. python下载自己网易云歌单的歌曲
  15. Python 使用 matplotlib 将离散的节点用光滑曲线连接
  16. 固态硬盘在线测试软件,ssd测试软件,详细教您ssd测试软件
  17. MS17-010永恒之蓝漏洞利用,win32安装,windows 7 32位
  18. C语言 生成集合的幂集
  19. 苹果拍照怎么显示地点和时间_内部秘密中医体质辨识与调理师证报名时间怎么报考考试地点...
  20. 数据清洗------kettle将txt数据转至数据库表中

热门文章

  1. python实现小霸王词典
  2. 每日感悟-华杉讲解论语-2022/1/30
  3. 拼团模式为什么会这么火?最完整的解析思路
  4. 华为android os耗电90%,鸿蒙OS如约而至,90%华为手机支持升级,麒麟9000率先尝鲜...
  5. 修改linux用户密码(passwd)
  6. 计算机毕业设计开题报告的撰写步骤和内容要求
  7. python排序算法大全(附源代码)
  8. B站主播投稿视频数据分析与tableau可视化!完美!
  9. b站投稿工具如何添加字幕
  10. 团队项目第2组-Beta阶段反思与进度管理