转换一个int数组String数组 [英] Converting an int array to a String array

查看:139
本文介绍了转换一个int数组String数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个整数的排行榜。这可能是一个矢量 INT [] 列表<整数GT; ,等等。

我的目标虽然是对整数排序,并以的String [] 结束。如何int数组开始作为是在空气中。

例如:
首先: {5,1,2,11,3}
结束与的String [] = {1,2,3,5,11}

反正有做这没有一个循环?我有一个for循环现在收集整数。我宁愿跳过做另一个循环。


解决方案

  INT [] = NUM​​S {5,1,2,11,3}; //列表或Vector
Arrays.sort(NUMS); //Collections.sort()的名单,矢量
字符串= Arrays.toString(NUMS); //的toString列表或Vector
串AR [] = a.substring(1,则为a.length() - 1).split(,);
的System.out.println(Arrays.toString(AR));

更新:

一个较短的版本:

  INT [] = NUM​​S {-5,1,2,11,3};
Arrays.sort(NUMS);
串[]一个= Arrays.toString(NUMS).split([\\\\ [\\\\]])[1] .split(,);
的System.out.println(Arrays.toString(一));

So I have this "list" of ints. It could be a Vector, int[], List<Integer>, whatever.

My goal though is to sort the ints and end up with a String[]. How the int array starts out as is up in the air.

ex: Start with:{5,1,2,11,3} End with: String[] = {"1","2","3","5","11"}

Is there anyway to do this without a for loop? I have a for loop now for collecting the ints. I would rather skip doing another for loop.

解决方案

int[] nums = {5,1,2,11,3}; //List or Vector
Arrays.sort(nums); //Collections.sort() for List,Vector
String a=Arrays.toString(nums); //toString the List or Vector
String ar[]=a.substring(1,a.length()-1).split(", ");
System.out.println(Arrays.toString(ar));

UPDATE:

A shorter version:

int[] nums = {-5,1,2,11,3};
Arrays.sort(nums);
String[] a=Arrays.toString(nums).split("[\\[\\]]")[1].split(", "); 
System.out.println(Arrays.toString(a));  

这篇关于转换一个int数组String数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆