java中Array类型与js中的Array类型 数据处理

方法 描述

concat()	连接两个或更多的数组,并返回结果。
copyWithin()	从数组的指定位置拷贝元素到数组的另一个指定位置中。
entries()	返回数组的可迭代对象。
every()	检测数值元素的每个元素是否都符合条件。
fill()	使用一个固定值来填充数组。  
filter()	检测数值元素,并返回符合条件所有元素的数组。  返回条件为true 的数据组成数组  ,let areaObject =newArray.filter(e => e.name = name);返回符合条件的数据返回数组
find()	返回符合传入测试(函数)条件的数组元素。
findIndex()	返回符合传入测试(函数)条件的数组元素索引。
forEach()	数组每个元素都执行一次回调函数。
from()	通过给定的对象中创建一个数组。
includes()	判断一个数组是否包含一个指定的值。
indexOf()	搜索数组中的元素,并返回它所在的位置。如果要检索的字符串值没有出现,则该方法返回-1
isArray()	判断对象是否为数组。
join()	把数组的所有元素放入一个字符串。 通常用于加分隔符
keys()	返回数组的可迭代对象,包含原始数组的键(key)lastIndexOf()	搜索数组中的元素,并返回它最后出现的位置。
map()	通过指定函数处理数组的每个元素,并返回处理后的数组。
pop()	删除数组的最后一个元素并返回删除的元素。
push()	向数组的末尾添加一个或更多元素,并返回新的长度。
reduce()	将数组元素计算为一个值(从左到右)。
reduceRight()	将数组元素计算为一个值(从右到左)。
reverse()	反转数组的元素顺序。
shift()	删除并返回数组的第一个元素。
slice()	选取数组的的一部分,并返回一个新数组。
some()	检测数组元素中是否有元素符合指定条件。
sort()	对数组的元素进行排序。
splice()	从数组中添加或删除元素。
toString()	把数组转换为字符串,并返回结果。
unshift()	向数组的开头添加一个或更多元素,并返回新的长度。
valueOf()	返回数组对象的原始值。
const list = supportList.slice(0, 3)   复制数组中下标从0-2的数组(三条)
//更新值
const arr = [ {id:1,score:1} ,{id:2,score:2} , {id:3 , score:3} ]
const newValue = { id:3 , score:3 }
const result = initial.map ( x=> x.id === newValue.id ? newValue : x);
  
方案1const updated = arr.map(function(item){
    return item.id == newValue.id ? newValue : item ;
});
方案2// 遍历数组,建立新数组,利用indexOf判断是否存在于新数组中,不存在则push到新数组,最后返回新数组 

//利用filter对s1进行过滤 ,去掉s2中存在的数字
const s1 = [ 1, 2, 3, 4, 5 ];
 const s2 = [ 2, 4 ];
 const subtracted = s1.filter(x => s2.indexOf(x) < 0);
 console.log(subtracted);//[1,3,5]

两个数组里面的对象中的属性比较,使用到hashMap 【

public List<DifferenceWapper> comparePosInfoVo(List<PosInfoVo> first,List<PosInfoVo> second, List<DifferenceWapper> list){
    HashMap<String, Integer> firstHashMap = new HashMap();
    if (null!=first &&!first.isEmpty()){
        for (PosInfoVo posInfoVo : first) {
            if (posInfoVo.getTrmMod() != null) {
                posInfoVo.setTrmQuantity(posInfoVo.getTrmQuantity() == null ? 0 : posInfoVo.getTrmQuantity());
                if (null != firstHashMap.get(posInfoVo.getTrmMod())) {
                    firstHashMap.put(posInfoVo.getTrmMod(),
                            firstHashMap.get(posInfoVo.getTrmMod()) + posInfoVo.getTrmQuantity());
                } else {
                    firstHashMap.put(posInfoVo.getTrmMod(), posInfoVo.getTrmQuantity());
                }
            }
        }
    }
    HashMap<String, Integer> secondHashMap = new HashMap();

    if (null!=second && !second.isEmpty()){
        for (PosInfoVo posInfoVo : second) {
            if (posInfoVo.getTrmMod() != null) {
                posInfoVo.setTrmQuantity(posInfoVo.getTrmQuantity() == null ? 0 : posInfoVo.getTrmQuantity());
                if (null != secondHashMap.get(posInfoVo.getTrmMod())) {
                    secondHashMap.put(posInfoVo.getTrmMod(),
                            secondHashMap.get(posInfoVo.getTrmMod()) + posInfoVo.getTrmQuantity());
                } else {
                    secondHashMap.put(posInfoVo.getTrmMod(), posInfoVo.getTrmQuantity());
                }
            }
        }
    }
    HashSet hashSet = new HashSet();
    hashSet.addAll(firstHashMap.keySet());
    hashSet.addAll(secondHashMap.keySet());
    Iterator<String> it = hashSet.iterator();
    while (it.hasNext()) {
        DifferenceWapper differenceWapper = new DifferenceWapper();
        String next = it.next();
        int secondFi = secondHashMap.get(next) == null ? 0 : secondHashMap.get(next);
        int firstFi = firstHashMap.get(next) == null ? 0 : firstHashMap.get(next);
        if (secondFi != firstFi) {
            differenceWapper.setColumn(next);
            differenceWapper.setCloumnName(next);
            Difference difference = new Difference();
            difference.setOldChangeValue(Integer.valueOf(firstFi).toString());
            difference.setOldValue(Integer.valueOf(firstFi).toString());
            difference.setNewValue(Integer.valueOf(secondFi).toString());
            difference.setNewChangeValue(Integer.valueOf(secondFi).toString());
            differenceWapper.setDifference(difference);
            list.add(differenceWapper);
        }

    }
    return list;
} 

ArrayList集合为什么不能使用foreach增删改: 会抛出java.util.ConcurrentModificationException异常
1.原因例子1:

 List<String> arrayList1 = new ArrayList<String>();
		arrayList1.add("1");
		arrayList1.add("2");
		for (String s : arrayList1) {
			 if("1".equals(s)){
			 arrayList1.remove(s);
			 }
		}
List<String> arrayList2 = new ArrayList<String>();
		arrayList2.add("2");
		arrayList2.add("1");
		for (String s : arrayList2) {
			 if("1".equals(s)){
			 arrayList2.remove(s);
			 }
		}
程序运行结果如下:

arrayList1的remove方法成功执行,但是arrayList2的remove方法运行抛出ConcurrentModificationException异常。
为了寻找原因,我们只能看源代码了。

因为foreach的本质就是使用的迭代器Iterator,所有的Collection集合类都会实现Iterable接口。
找到ArrayList类的iterator()方法
迭代器的本质是先调用hasNext()方法判断存不存在下一个元素,然后再使用next()方法取下一个元素
上面arraylist1为什么能remove成功呢,其实它只循环了一次,所以成功了。
因为它在remove元素1之后,它的size - 1变成1,然后Itr内部的cursor变量由0变成1
此时1 = 1,循环结束,所以成功了。
arraylist2为什么remove失败呢,因为他在循环第二次的时候,也remove成功了,但是第三次判断next的时候cursor的值为2导致不等于现在的size 1,所以执行了next方法,最重要的来了,之前remove的操作导致ArrayList的modCount值加1,然后Itr类中的expectedModCount保持不变,所以会抛出异常。
同理可得,由于add操作也会导致modCount自增,所以不允许在foreach中删除、增加、修改ArrayList中的元素。
对此,推荐大家使用迭代器Iterator删除元素,
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值