查找ArrayList<Bean>的值

该代码段展示了一个Kotlin程序,它创建了一个包含10个Bean对象的ArrayList,每个Bean对象含有pg和url属性。然后定义了一个函数findPgInList用于在列表中查找具有特定pg值的Bean对象,并打印出找到的对象。如果未找到匹配项,函数返回null。

查找bean

fun main() {
    val list= ArrayList<Bean>()
    for (i in 0..10){
        list.add(Bean("pg$i","url$i"))
    }
    val bean=findPgInList("pg4",list)
    println(bean)
}
fun findPgInList(pg: String, list: ArrayList<Bean>): Bean? {
    list.forEach {
         if (it.pg == pg){
             return it
        }
    }
    return null
}
data class Bean(val pg:String,val url:String)
public void updateData(List<IotClientBean> iotDeviceBeanList, List<SpaceBean> spaceList, List<AutomationTaskBean> scheduleList, boolean isModifyFail) { List<IotClientBean> newIotDeviceList = new ArrayList<>(); List<AutomationTaskBean> newScheduleList = new ArrayList<>(); if (!iotDeviceBeanList.isEmpty()) { newIotDeviceList.addAll(iotDeviceBeanList); } if (!scheduleList.isEmpty()) { newScheduleList.addAll(scheduleList); } List<IotClientBean> oldList = new ArrayList<>(mIotDeviceBeanList); List<AutomationTaskBean> oldScheduleList = new ArrayList<>(mScheduleList); if (!spaceList.isEmpty()) { mSpaceList.clear(); mSpaceList.addAll(spaceList); } mScheduleList.clear(); mScheduleList.addAll(newScheduleList); mIotDeviceBeanList.clear(); mIotDeviceBeanList.addAll(newIotDeviceList); if (isModifyFail) { notifyDataSetChanged(); } else { DiffUtil.DiffResult result = DiffUtil.calculateDiff(new IotDeviceDiffCallback( oldList, newIotDeviceList, oldScheduleList, newScheduleList), false); result.dispatchUpdatesTo(this); } } private void setSmartLightStripUI(MpIotItemSmartClientBinding binding, IotClientBean iotDeviceBean) { ILightBean iotLightBean = null; if (iotDeviceBean instanceof ILightBean) { iotLightBean = (ILightBean) iotDeviceBean; } binding.lightLayout.getRoot().setVisibility(View.VISIBLE); MpIotLayoutSmartClientLightBinding lightBinding = binding.lightLayout; lightBinding.smartDeviceIconIv.setImageResource(R.drawable.mp_iot_selector_smart_client_light_strip_icon); lightBinding.smartDeviceNameTv.setText(iotDeviceBean.getName()); lightBinding.smartDeviceLocationTv.setText(IotSpaceUtil.getIotSpaceNameById(mContext, iotDeviceBean.getSpace_id(), mSpaceList)); ......} private void setDeviceUI(MpIotItemSmartClientBinding binding, IotClientBean iotDeviceBean) { resetView(binding); EnumIotCategoryType category = iotDeviceBean.getCategory(); switch (category) { case LIGHT_STRIP: setSmartLightStripUI(binding, iotDeviceBean); break; ......} @Override public void onBindViewHolder(@NonNull SmartDeviceViewHolder holder, int position) { IotClientBean bean = mIotDeviceBeanList.get(position); setDeviceUI(holder.mBinding, bean); holder.itemView.setOnClickListener(view -> { if (mDeviceClickListener != null) { mDeviceClickListener.onOnItemClick(bean, mCameraBlockedList.contains(getSmartDeviceMac(bean))); } }); } 请分析,对于这个SpaceList,它并没有添加到IotDeviceDiffCallback里进行差异判断,这样是否会导致出现该崩溃Fatal Exception: java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 0(offset:-1).
07-10
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值