java.util.List接口的方法subList()的使用注意事项

JDK中,List接口有一个实例方法List<E> subList(int fromIndex, int toIndex),其作用是返回一个以fromIndex为起始索引(包含),以toIndex为终止索引(不包含)的子列表(List)。
但值得注意的是,返回的这个子列表的幕后其实还是原列表;也就是说,修改这个子列表,将导致原列表也发生改变;反之亦然。

下面是一段实例代码:

import java.util.ArrayList;
import java.util.List;

public class TestSubList {

public static void main(String[] args) {
List<Integer> test = new ArrayList<Integer>();
//init list
for (int i = 0; i < 5; i++) {
test.add(i); //auto boxing
}
//display the list
System.out.print("the orginal list: ");
for (int i = 0; i < test.size(); i++) {
System.out.print(test.get(i) + " ");
}
System.out.println();

//sub list
List<Integer> sub = test.subList(1, 3); //sub list contains elements: 1, 2
sub.remove(1); //remove element “2” from sub list

//display the list again
System.out.print("the orginal list after sublist modified: ");
for (int i = 0; i < test.size(); i++) {
System.out.print(test.get(i) + " ");
}
System.out.println();
}

}


程序运行结果:
the orginal list: 0 1 2 3 4
the orginal list after sublist modified: 0 1 3 4

在这段代码中,我们并没有改变原始列表“test”其中的元素。然而,当删除由subList方法得到的子列表中的元素时,原始列表中的该元素也被删除了。
java.lang.IllegalStateException: Failed to execute ApplicationRunner at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:759) at org.springframework.boot.SpringApplication.lambda$callRunners$2(SpringApplication.java:746) at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183) at java.util.stream.SortedOps$SizedRefSortingSink.end(SortedOps.java:357) at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:498) at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:487) at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150) at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173) at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:241) at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:485) at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:744) at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1289) at com.zte.vd.insight.config.BootstrapApp.main(BootstrapApp.java:30) Caused by: org.springframework.dao.DuplicateKeyException: ### Error updating database. Cause: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "software_dict_pkey" (seg1 149.168.43. Detail: Key (process_name, os_type)=(powerpnt.exe, 0) already exists. ### The error may exist in file [/home/dexcloud/vd-config/conf/mapping/SoftwareProcessApdb.xml] 如何捕获异常并打印日志 List<List<SoftwareProcess> > splitedList = LocalUtils.splitList(softwareProcesses, BATCH_SIZE); for(List<SoftwareProcess> subList : splitedList){ int count = softwareProcessApMapper.batchInsertSoftwareProcess(subList); log.info("---------->>>>>softwareDictMigrationFromPgToAp: Migrate {} data's from PG to AP", count); }
03-25
JavaFX使用de.saxsys.mvvmfx.spring,定义了: private final ObservableList<Tag<SysRolePageVO>> selRoles = FXCollections.observableArrayList(); 我在Platform.runLater中调用selRoles.clear()报错,错误如下: Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException: toIndex = 2 at java.base/java.util.AbstractList.subListRangeCheck(AbstractList.java:507) at java.base/java.util.AbstractList.subList(AbstractList.java:497) at javafx.base/javafx.collections.ModifiableObservableListBase.subList(ModifiableObservableListBase.java:196) at javafx.base/com.sun.javafx.collections.VetoableListDecorator.subList(VetoableListDecorator.java:361) at javafx.base/com.sun.javafx.binding.ContentBinding$ListContentBinding.onChanged(ContentBinding.java:114) at javafx.base/com.sun.javafx.collections.ListListenerHelper$Generic.fireValueChangedEvent(ListListenerHelper.java:329) at javafx.base/com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:73) at javafx.base/javafx.collections.ObservableListBase.fireChange(ObservableListBase.java:239) at javafx.base/javafx.collections.ListChangeBuilder.commit(ListChangeBuilder.java:482) at javafx.base/javafx.collections.ListChangeBuilder.endChange(ListChangeBuilder.java:541) at javafx.base/javafx.collections.ObservableListBase.endChange(ObservableListBase.java:211) at javafx.base/com.sun.javafx.collections.ObservableListWrapper.clear(ObservableListWrapper.java:157) at com.sekiro.fx.view.user.UserInfoViewModel.lambda$getUserInfo$1(UserInfoViewModel.java:80) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457) at java.base/java.security.AccessController.doPrivileged(AccessController.java:399) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456) at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96) at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184) at java.base/java.lang.Thread.run(Thread.java:840)
最新发布
11-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值