java批量修改foreach,Java的修改元素在foreach

I'm learning Java on my own; and therefore the code below has no function other than for learning/testing.

Essentially I'm trying to modify the elements of an Integer array (namely, halving them) whilst in a foreach loop.

I should note that I'm not re-ordering, adding, or deleting elements; simply changing their values.

Here is my code:

Logger.describe("Now copying half of that array in to a new array, and halving each element");

Integer[] copyArray = new Integer[DEFAULT_SAMPLE_SIZE / 2];

System.arraycopy(intArray, 0, copyArray, 0, DEFAULT_SAMPLE_SIZE / 2);

for (Integer x : copyArray) x /= 2;

Logger.output(Arrays.deepToString(copyArray));

However, the original array (intArray) is this:

[47, 31, 71, 76, 78, 94, 66, 47, 73, 21]

And the output of copyArray is:

[47, 31, 71, 76, 78]

So although the array has been halved in size, the elements (Integers) haven't also been halved in value. So what am I doing wrong?

Thank you

解决方案

You can't do that in a foreach loop.

for (int i=0; i

copyArray[i] /= 2;

Else you are not assigning it back into the array. Integer objects are immutable by the way so can't modify them (creating new ones though).

Updated from comment:

Beware though that there are a few things going on, autoboxing/unboxing for example, roughly:

copyArray[i] = Integer.valueOf(copyArray[i].intValue()/2);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mybatis批量修改操作可以通过使用foreach标签来实现。在使用foreach标签时,可以将要修改的数据封装在一个List集合中,然后通过调用Mapper接口中定义的批量修改方法来进行操作。在Mapper接口中,可以使用@Param注解指定参数名称,然后在Mapper.xml文件中使用这个参数名称来引用。例如,对于批量修改操作,可以在Mapper接口中定义一个方法,方法参数使用List类型来接收要修改的数据,然后在Mapper.xml文件中使用foreach标签来循环处理每条数据,并进行修改操作。具体的代码示例如下所示: 引用:在ProcessUnitDurationMapper接口中定义批量修改方法updateMultiple(List<ProcessUnitDuration> list)。 引用:批量修改操作相比于循环单个修改操作,可以显著提升性能并且使代码更加简洁明了。通过一次数据库操作,可以同时修改多条数据,减少了数据库访问的次数,提高了效率。 示例代码如下: ```java @Repository public interface ProcessUnitDurationMapper { int updateMultiple(List<ProcessUnitDuration> list); } ``` ```xml <!-- Mapper.xml文件中 --> <update id="updateMultiple" parameterType="java.util.List"> UPDATE table_name SET column1 = #{item.column1}, column2 = #{item.column2}, ... WHERE id = #{item.id} </update> ``` 在上述示例中,updateMultiple方法接收一个List参数来批量修改数据。在Mapper.xml文件中,使用foreach标签循环处理List中的每个元素,并执行相应的修改操作。通过在SQL语句中使用#{item.xxx}来引用List中的每个元素的属性值。 需要注意的是,参数列表的名称应与Mapper接口方法中的@Param注解指定的名称一致,以便正确引用参数。同时,修改语句中的表名、字段名和条件等需要根据实际情况进行修改。 通过以上方式,可以实现Mybatis中的批量修改操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [MyBatis动态sql之批量修改批量新增(使用foreach标签的一条sql语句解决)](https://blog.csdn.net/Da_zhenzai/article/details/127817838)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值