将 int 数组转换为 String 数组 [英] Converting an int array to a String array

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

问题描述

所以我有这个整数列表".它可以是 Vectorint[]List 等等.

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

我的目标是对整数进行排序并以 String[] 结束.int 数组是如何开始的.

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.

例如:开头:{5,1,2,11,3}结束于:String[] = {"1","2","3","5","11"}

有没有办法在没有 for 循环的情况下做到这一点?我现在有一个 for 循环来收集整数.我宁愿跳过另一个 for 循环.

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));

更新:

更短的版本:

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天全站免登陆