string[] 和 arraylist互转及问题解决
1,String 数组转成 list<String>
String[] s={"1","2","3","5","6"};
List<String> listA = Arrays.asList(s);
String 数组在转成 list<String>后, 直接对该list进行操作, 会出异常,例如:
public static void main(String[] args) {
String[] s={"1","2","3","5","6"};
List<String> listA = Arrays.asList(s);
listA.add(3,"4");
for(String temp:listA){
System.out.println(temp);
}
}
运行时会抛出如下异常:
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:148)
原因分析:
public static void main(String[] args) {
String[] s={"1","2","3","5","6"};
List<String> listA = Arrays.asList(s);
List<String> listB = new ArrayList<String>(listA);
listB.add(3,"4");
for(String temp:listB){
System.out.println(temp);
}
}
运行结果:输出 1 2 3 4 5 6
PS: 这里顺便说明一下arraylist 的 remove() add() 特点
1)arraylist add时, 指定了index添加时,原本该index上的位置不会被删除, 而是从index位置上的数据都向右移。
2)arraylist delete时, 删除后, 该位置后面的所有数据自动向左移,所以遍历的时候, index需要-1 才不会漏掉, 或者直接从倒序遍历,
例如如下代码:
public static void main(String[] args) {
String[] s={"1","2","2","3","5","6"};
List<String> listA = Arrays.asList(s);
List<String> listB = new ArrayList<String>(listA);
for(int i=0;i<listB.size();i++){
if (listB.get(i).equals("2")){
listB.remove(i);
}
}
for(String temp:listB){
System.out.println(temp);
}
}
运行结果:输出 1 2 3 4 5 6 , 该原list index=2位置漏掉遍历,所以得到结果不符合预期
改成如下:
public static void main(String[] args) {
String[] s={"1","2","2","3","5","6"};
List<String> listA = Arrays.asList(s);
List<String> listB = new ArrayList<String>(listA);
//1. Should reduce 1 once remove
for(int i=1;i<listB.size();i++){
if (listB.get(i).equals("2")){
listB.remove(i);
i--;
}
}
/**
* 2. reverse traversal
for(int i=listB.size()-1;i>=0;i--){
if (listB.get(i).equals("2")){
listB.remove(i);
}
}**/
for(String temp:listB){
System.out.println(temp);
}
}
运行结果:输出 1 3 4 5 6
2, list<String>转成String 数组
String[] b = list.toArray(new String[list.size()]);
3,String数组直接转成string输出
Arrays.toString(数组名)
string[] 和 arraylist互转及问题解决的更多相关文章
- c++ string 与 char 互转 以及base64
c++ string 与 char 互转 很简单如下 ] = {'A','B','C','D','E'}; printf("%s\n",bts); //char to string ...
- String[]和ArrayList和LinkedList区别
String[]和ArrayList和LinkedList区别 参考文档如下: http://www.blogjava.net/flysky19/articles/92775.html http:// ...
- 将ArrayList<HashMap<String, String>>转为ArrayList<Bundle>类型的解决方案
Bundle是一种利用键值对存储的数据格式,而我们在程序中通常利用HashMap存储数据.在开发中,通过Http请求得到JSONArray类型的返回值,我选择利用ArrayList<HashMa ...
- string与wstring互转
string与wstring互转 C++ Code 123456789101112131415161718192021222324252627282930313233343536373839404 ...
- 下载STRING数据库检索互作关系结果为空,但是在STRING网站却能检索出互作关系,为什么呢???关键词用的是蛋白ID(ENSP开头)
首先介绍下两种方法: 一.本地分析 1.在STRING数据库下载人的互作文件,如下图,第一个文件 https://string-db.org/cgi/download.pl?sessionId=HGr ...
- String、String[]、ArrayList<String>之间的转换
1. ArrayList<String> 转换为 String[]: ArrayList<String> list = new ArrayList<>(); li ...
- Go语言网络通信---string与int互转,int64与[]byte互转,int直接互转,string与[]byte互转
string与int互转 #string到int int,err:=strconv.Atoi(string) #string到int64 int64, err := strconv.ParseInt( ...
- List<String> 和 ArrayList<String>的区别
最近对这两个问题比较懵逼,关于List和ArrayList.List<String> list = new ArrayList<String>(); 好了,先搞明白List 和 ...
- SONObjetc和String Map Bean互转,JSONArray和String List互转
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; i ...
随机推荐
- nyoj 14 会场安排问题
会场安排问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 学校的小礼堂每天都会有许多活动,有时间这些活动的计划时间会发生冲突,需要选择出一些活动进行举办.小刘的工 ...
- AutoLayout框架Masonry使用心得
AutoLayout框架Masonry使用心得 字数1769 阅读1481 评论1 喜欢17 我们组分享会上分享了页面布局的一些写法,中途提到了AutoLayout,会后我决定将很久前挖的一个坑给填起 ...
- 转载总结一些关于Google chart api的知识
<script type="text/javascript"> google.setOnLoadCallback(drawChartLine); f ...
- MSSQLSERVER数据库- 慎用SELECT INTO复制表
很多时候我们习惯于用SELECT INTO复制一个表或表结构,因为它方便,快捷,而且在某些情况下效率比INSERT INTO 效率要高一些.但是要注意: SELECT INTO 复制表或表结构的时候, ...
- Reactive native 项目创建失败如何处理
首先感谢党的英明决策. 一开始我总觉得Awesomeproject这个名字是固定的,和我有同感的同学请举手. 其实我们可以起任意的名字,执行native react init碰到的最大的问题是 npm ...
- Ubuntu 安装 Courier New字体
apt-get install ttf-mscorefonts-installer 它的本质是安装 Courier New字体 安装的时候会出现一个协议 按TAB键 ,可以选中<确定>按 ...
- How to add “Maven Managed Dependencies” library in build path eclipse
If you have m2e installed and the project already is a maven project but the maven dependencies are ...
- 获取WMI硬件清单
WMI服务能够报告详细的硬件信息.通常,每个硬件都来自它们自己的WMI代理类.但是要找出这些硬件类的名字是不容易. 所有硬件类都在同一个WMI根下面,你可以在根类查询所有的硬件: Get-WmiObj ...
- UIAlertController 简单修改title以及按钮的字体颜色
苦逼的开发者,最终败给了一个任性的UI,系统原生UIAlertController的按纽颜色必须改.于是,开始了不归路.之前的版本是自己用view写的一个仿系统UIActionSheet,动画感觉都挺 ...
- android117 下拉列表