java数据问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java数据问题相关的知识,希望对你有一定的参考价值。
编写一个程序,这个程序把一个整数数组中的每个元素用逗号连接成一个字符串,例如,根据内容为[1][2][3]的数组形成内容为"1,2,3"的字符串。
看好了,是每个元素用逗号连接起来,而且只能用j2se所学到的知识。
public static String toString(int[] a)
StringBuilder temp = new StringBuilder();// 用StringBuilder来减少字符串合并时的开销
temp.append(a[0]);// 加入数组中的第一个成员
int size = a.length;// 获得数组a的长度
for (int i = 1; i < size; i++)
temp.append(',');// 加入逗号
temp.append(a[i]);// 加入数组相应位置上的元素
return temp.toString();// 将StringBuilder类型转化为String,然后返回
public static void main(String[] args)
int[] a = 1, 2, 3, 4, 5, 6, 7, 8, 9 ;
System.err.println(toString(a));// 测试
参考技术B import java.util.Arrays;
public class Calen
public static void main(String[] args) throws Exception
int[] num = 1,2,3;
String str = Arrays.toString(num).replaceAll("\\[", "").replaceAll("\\]", "");
System.out.println(str);
全部是j2se的知识本回答被提问者采纳 参考技术C int[] nums = 1,2,3,4,5;
StringBuffer sb = new StringBuffer();
for(int i : nums)
sb.append(i).append(",");
// 删除最后一个逗号
String resultString = sb.toString().replaceAll(",$",""); 参考技术D public static void main(String args[])
int nums=1,2,3,4,5;
String str="";
for(int i=0;i<nums.length;i++)
if(i!=0)str+=",";
str+=nums[i];
System.out.println(str);
第5个回答 2009-08-22 int num[]=.....................//输入源
int i=0;
String s="";
for(i=0;i<num.length;i++)
s=s+String.valueOf(i)+",";
以上是关于java数据问题的主要内容,如果未能解决你的问题,请参考以下文章