当前位置:首页 > 常用

Java中数组,List和Set的互相转换

数组转换成List
String[] strArray = new String[] {"1", "2"};
List <String> list = Arrays.asList(strArray );
List转换成数组:
List list = new ArrayList();
list.add("1");
list.add("2");
String[] strArray = (String[])list.toArray(new String[list.size()]);
数组转换成Set:
String[] strArray = {"a", "b"};	
Set set = new HashSet(Arrays.asList(strArray));
Set转换成数组:
Set set = new HashSet<String>();
set.add("a");
set.add("b");

Object[] objArray = set.toArray();
或:
String[] strArray = (String[])set.toArray(new String[0]);
或:
String[] strArray = (String[])set.toArray(new String[set.size()]);
List转换成Set:
List<String> list = new ArrayList<String>();
Set set = new HashSet(list);
Set转换成List:
Set<String> set = new HashSet<String>();
set.add("a");
set.add("b");
	
String[] strArray = set.toArray(new String[set.size()]);

阅读全文

Java中整型、double、float型数字与字符串的相互转换

整型转换成字符串,方式1
int nValue = 15;
String str = Integer.toString(nValue);
整型转换成字符串,方式2:
int nValue = 15;
String str = String.valueOf(nValue);
字符串转换成整型:
String str = "15";
int nValue = Integer.parseInt (str);
double转换成字符串:
double dValue = 1.234;
String str = String.valueOf(dValue);
字符串转换成double:
String str = "1.234";
double dValue = Double.parseDouble(str);
float转换成字符串:
float fValue = 1.234f;
String str = String.valueOf(dValue);
字符串转换成float:
String str = "1.234";
float fValue = Float.parseFloat(str);
阅读全文

返回页首返回页尾