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 ...
随机推荐
- HDU 1226 BFS
注意密码位数<=500 输出注意十六进制改成字母 要点题目都已说明 ac就好 #include<iostream> #include<stdio.h> #include& ...
- C# 通用数据访问类(SqlHelper)
[转]C# 通用数据访问类(SqlHelper) 注:本文转自http://www.tzwhx.com/newOperate/html/3/31/312/13080.htmlVisual C# 动态操 ...
- Python Telnet弱口令爆破脚本及遇到的错误与问题
写得时候遇到了一个很大的问题,就是我在发送用户名,接受用户名就会一直卡住.然后等了好久后提示 recv ‘\r\nSession timed out.\r\n\r\nTelnet Server has ...
- git拉取远程分支并创建本地分支和Git中从远程的分支获取最新的版本到本地
git拉取远程分支并创建本地分支 一.查看远程分支 使用如下Git命令查看所有远程分支: git branch -r 二.拉取远程分支并创建本地分支 方法一 使用如下命令: git checkout ...
- Android动画Animation之Tween用代码实现动画
透明度动画.旋转动画.尺寸伸缩动画.移动动画 package com.javen.tween; import android.annotation.SuppressLint; import andro ...
- ios开发——实战OC篇&FMDB详解
FMDB详解 前一篇文章中我们介绍的SQLite的使用,在iOS中原生的SQLite API在使用上相当不友好. 于是,就出现了一系列将SQLite API进行封装的库,例如FMDB.Plausibl ...
- 标准I/O库之缓冲
标准I/O库提供缓冲的目的是尽可能减少使用read和write调用的次数.它也对每个I/O流自动地进行缓冲管理,从而避免了应用程序需要考虑这一点所带来的麻烦. 标准I/O提供了三种类型的缓冲: (1) ...
- 应聘.net开发工程师常见的面试题(五)
1.描述一下C#中索引器的实现过程,是否只能根据数字进行索引? 答:不是.可以用任意类型. 2.在C#中,string str = null 与 string str = ” ” 请尽量使用文字或图象 ...
- IIS 之 启用日志记录
如何为网站启用日志记录或 在 Microsoft Internet Information Services (IIS) 6.0 中,在 IIS 5.0 中,并在 IIS 4.0 中的FTP 站点.可 ...
- js中点击事件方法三种方式的区别
在javascript中,可以为某个元素指定事件,指定的方式有以下三种: 1.在html中,使用onclick属性 2.在javascript中,使用onclick属性 (1)注意函数名没有双引号. ...