如何使用 java 中的索引交换 List<List<String>> 数组值?
How to interchange List<List<String>> array values using index in java?
我有 List<List<String>>
这样的数据,
List<List<String>> repdata = [
["1185","R","4t","G","06","L","GT","04309","2546","2015","CF FE","01H1","20","23840","FF20"],
["1186","R","5t","R","01","L","TP","00110","1854","2016","FE LL","06W3","01","19065","FB01"],
["1187","R","6t","H","06","L","TP","04333","1864","2015","CF FE SL","0209","FD22","19845",null],
["1188","R","7t","H","06","L","PR","04041","6951","2015","CC CT FE GN PC","0070","00","36590","LB00"],
["1189","R","8t","H","06","L","WS","04290","4450","2014","CF EN FE PC TP","0070","EA30","28320.00",null],
["1190","R","9t","H","06","L","LA","04915","4430","2015","CF DK FE RR TC","0040","10","23680","FB10"],
["1191","R","10t","H","06","L","LF","04335","2532","2015","CF FE GE","0040","FC10","22970",null],
["1192","R","11t","H","06","L","SA","04772","8345","2015","BZ C8 FE","01D6","13","33390","LC13"]]
我想比较和交换每个内部列表中的元素:特别是内部列表中第 12 个和第 14 个索引处的值。
ex: 在此内部列表数据中
[["1185","R","4t","G","06","L","GT","04309","2546","2015","CF FE","01H1","20","23840","FF20"]]
我想使用此逻辑将“20”与 'FF20' 进行比较和互换。
If 14th index value != null then assign,
12th index=14th index value.
else if 14th index value ==null,
then leave 12th index=12th index value as it is.
并且必须对 List<List<String>>
repdata 中的所有内部列表重复此操作。
所以,我的最终列表>会是这样的,
List<List<String>> repdata = [
["1185","R","4t","G","06","L","GT","04309","2546","2015","CF FE","01H1","FF20","23840","FF20"],//interchange 12th with 14th as 14th !=null
["1186","R","5t","R","01","L","TP","00110","1854","2016","FE LL","06W3","FB01","19065","FB01"],//interchange 12th with 14th as 14th !=null
["1187","R","6t","H","06","L","TP","04333","1864","2015","CF FE SL","0209","FD22","19845",null],//leave 12th as IT IS as 14th ==null
["1188","R","7t","H","06","L","PR","04041","6951","2015","CC CT FE GN PC","0070","00","36590","LB00"],//interchange 12th with 14th as 14th !=null
["1189","R","8t","H","06","L","WS","04290","4450","2014","CF EN FE PC TP","0070","EA30","28320.00",null],//leave 12th as IT IS as 14th ==null
["1190","R","9t","H","06","L","LA","04915","4430","2015","CF DK FE RR TC","0040","10","23680","FB10"],//interchange 12th with 14th as 14th !=null
["1191","R","10t","H","06","L","LF","04335","2532","2015","CF FE GE","0040","FC10","22970",null],//leave 12th as IT IS as 14th ==null
["1192","R","11t","H","06","L","SA","04772","8345","2015","BZ C8 FE","01D6","13","33390","LC13"]]//interchange 12th with 14th as 14th !=null
我尝试删除第 12 个索引,然后在第 12 个索引处添加第 14 个索引值。
但我不断收到 concurrentmodificationexception。
谁能帮我解决这个问题?
为了用第 14 个元素替换列表的第 12 个元素,您必须编写如下代码:
List<List<String>> listOfLists = ...;
for( List<String> list: listOfLists ){
String el;
if( list.size() > 14 && // list is long enough
(el = list.get(14)) != null ){ // element is not null
list.set( 12, el ); // replace element at index 12
}
}
我有 List<List<String>>
这样的数据,
List<List<String>> repdata = [
["1185","R","4t","G","06","L","GT","04309","2546","2015","CF FE","01H1","20","23840","FF20"],
["1186","R","5t","R","01","L","TP","00110","1854","2016","FE LL","06W3","01","19065","FB01"],
["1187","R","6t","H","06","L","TP","04333","1864","2015","CF FE SL","0209","FD22","19845",null],
["1188","R","7t","H","06","L","PR","04041","6951","2015","CC CT FE GN PC","0070","00","36590","LB00"],
["1189","R","8t","H","06","L","WS","04290","4450","2014","CF EN FE PC TP","0070","EA30","28320.00",null],
["1190","R","9t","H","06","L","LA","04915","4430","2015","CF DK FE RR TC","0040","10","23680","FB10"],
["1191","R","10t","H","06","L","LF","04335","2532","2015","CF FE GE","0040","FC10","22970",null],
["1192","R","11t","H","06","L","SA","04772","8345","2015","BZ C8 FE","01D6","13","33390","LC13"]]
我想比较和交换每个内部列表中的元素:特别是内部列表中第 12 个和第 14 个索引处的值。
ex: 在此内部列表数据中
[["1185","R","4t","G","06","L","GT","04309","2546","2015","CF FE","01H1","20","23840","FF20"]]
我想使用此逻辑将“20”与 'FF20' 进行比较和互换。
If 14th index value != null then assign,
12th index=14th index value.
else if 14th index value ==null,
then leave 12th index=12th index value as it is.
并且必须对 List<List<String>>
repdata 中的所有内部列表重复此操作。
所以,我的最终列表>会是这样的,
List<List<String>> repdata = [
["1185","R","4t","G","06","L","GT","04309","2546","2015","CF FE","01H1","FF20","23840","FF20"],//interchange 12th with 14th as 14th !=null
["1186","R","5t","R","01","L","TP","00110","1854","2016","FE LL","06W3","FB01","19065","FB01"],//interchange 12th with 14th as 14th !=null
["1187","R","6t","H","06","L","TP","04333","1864","2015","CF FE SL","0209","FD22","19845",null],//leave 12th as IT IS as 14th ==null
["1188","R","7t","H","06","L","PR","04041","6951","2015","CC CT FE GN PC","0070","00","36590","LB00"],//interchange 12th with 14th as 14th !=null
["1189","R","8t","H","06","L","WS","04290","4450","2014","CF EN FE PC TP","0070","EA30","28320.00",null],//leave 12th as IT IS as 14th ==null
["1190","R","9t","H","06","L","LA","04915","4430","2015","CF DK FE RR TC","0040","10","23680","FB10"],//interchange 12th with 14th as 14th !=null
["1191","R","10t","H","06","L","LF","04335","2532","2015","CF FE GE","0040","FC10","22970",null],//leave 12th as IT IS as 14th ==null
["1192","R","11t","H","06","L","SA","04772","8345","2015","BZ C8 FE","01D6","13","33390","LC13"]]//interchange 12th with 14th as 14th !=null
我尝试删除第 12 个索引,然后在第 12 个索引处添加第 14 个索引值。
但我不断收到 concurrentmodificationexception。
谁能帮我解决这个问题?
为了用第 14 个元素替换列表的第 12 个元素,您必须编写如下代码:
List<List<String>> listOfLists = ...;
for( List<String> list: listOfLists ){
String el;
if( list.size() > 14 && // list is long enough
(el = list.get(14)) != null ){ // element is not null
list.set( 12, el ); // replace element at index 12
}
}