【Java基础】list转为Integer[]、list转为int[]、Integer[]转为int[]、int[]转为Integer[]
来源:互联网 发布:新闻英语听力训练软件 编辑:程序博客网 时间:2024/05/17 23:17
list转为Integer[]:通过List的toArray()方法转换,具体见下面代码
list转为int[]:1、通过list的toArray()方法转成Integer[]型再一个个转成int[]型;2、直接从list中读出来,一个一个转成int[]型
Integer[]转为int[]:一个一个转
int[]转为Integer[]:一个一个转
public class InTest {public static void main(String[] args) { /*将list转化为Integer[]*/ ArrayList<Integer> intList = new ArrayList<Integer>();//泛型为Integer intList.add(123); intList.add(456); intList.add(789); Integer[] b = new Integer[intList.size()];//当泛型为Integer时,需要以 b = (Integer[])intList.toArray(b); //Integer类来作为数组基本元素 System.out.println(Arrays.toString(b));//,否则输出会报错。 /*强制转换为Integer[]*/ try{ Integer[] c = (Integer[])intList.toArray(); //此处编译没问题,但是, System.out.println(Arrays.toString(c)); //泛型向下转换运行会出错 }catch(Exception e){ System.out.println("捕获到异常"); } /*循环的方法把list<Integer>型数组转换成int[]数组,只能一个一个转换*/ int[] d = new int[intList.size()]; for(int i = 0;i<intList.size();i++){ d[i] = intList.get(i); } System.out.println( Arrays.toString(d)); /*把Integer型转换成int型*/ Integer a1 = new Integer(10); int a2 = a1; System.out.println(a2); /*把Integer[]型数组转换成int[]只能一个一个转换*/ int[] a4 = new int[3]; Integer[] a3 = new Integer[]{1, 2, 3}; for(int i =0; i<a3.length; i++){ a4[i] = a3[i].intValue(); } System.out.println(Arrays.toString(a4)); /*把int型转换成Integer型*/ int b1 = 30; Integer b2 = b1; System.out.println(b2); /*把int[]型转换成Integer[]型,则也需要一个一个的转换*/ int[] b3 = new int[]{1,2,3}; Integer[] b4 = new Integer[3]; for(int i =0; i<b3.length; i++){ b4[i] = b3[i]; } System.out.println(Arrays.toString(b4));}}
阅读全文
0 0
- 【Java基础】list转为Integer[]、list转为int[]、Integer[]转为int[]、int[]转为Integer[]
- Integer转为int型的三种方法.txt
- 算法基础(一)---- 不使用Integer.parseInt将String转为Int
- INT转为CHAR
- int 转为16 进制数
- NSData转为Int
- 字符串转为int类型
- 实现字符串转为int
- python Unicode转为int
- 8. String to Integer (atoi) 字符串转为int类型的所有可能情况
- 包含对象为Integer的List列表转为Integer数组并排序的简单方法
- Java中int 转为char的方法
- java int数组转为long数组
- java string强转为int型问题
- java 4位byte转为int类型
- C++中string转为int
- C++中string转为int
- unsigned int转为十六进制方法
- Android编译单个apk到rom报错的问题解决(无错误提示)
- [一]Mina入门实例-Mina网络通信框架
- C# DllImport的用法
- 扫描一个多列的文件,将每一列扫描到一个结构体变量中
- HYActivityView
- 【Java基础】list转为Integer[]、list转为int[]、Integer[]转为int[]、int[]转为Integer[]
- mybatis中数值型0被判为空字符串
- python Day 4
- mysql与oracle like小知识
- 经典语句二
- 值传递的Java
- Sql中查询的具体应用及子句的优先级
- 进程运行状态
- es6小记