首页 > 代码库 > 将text 文件转为List

将text 文件转为List

Integer 类型
ArrayList<Integer> Mlist = new ArrayList<Integer>(); Scanner scM = new Scanner(new File("D://m.txt"));
     
     
while (scM.hasNextInt()) { int i = scM.nextInt(); Mlist.add(i); }
   
/*将list转为数组*/
       Integer[] Marray = new Integer[Mlist.size()];
       Marray = (Integer[])Mlist.toArray(Marray);

 


String 类型
ArrayList<String> Nlist = new ArrayList<String>(); Scanner scN = new Scanner(new File("D://n.txt")); while (scN.hasNextLine()) { String i = scN.nextLine(); Nlist.add(i); }
/*将list转为数组*/
      String[] Narray = new String[Nlist.size()];
       Narray = (String[])Nlist.toArray(Narray);

 

/*数组转List*/

String[] userid = {"aa","bb","cc"};
List list = new ArrayList(Arrays.asList(userid));

 

将text 文件转为List