如何将表转换为字符串?

时间:2014-03-15 20:52:35

标签: java android string

我想将表转换为一个字符串:

int y []  =  {30, 34, 45, 57, 77, 89, 100, 111, 123, 145};

必须是这样的:

String y = 30, 34, 45, 57, 77, 89, 100, 111, 123, 145;

远码:

public class GraphLoading extends Activity {
public static int [] y;
int Test;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.loading);




int y []  =  {30, 34, 45, 57, 77, 89, 100, 111, 123, 145};



 }

public static int[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

}

我必须将该tablo转换为字符串,然后我可以将其保存到SD卡中。

请问您可以粘贴完整代码吗? (我很业余)

4 个答案:

答案 0 :(得分:1)

您可以依赖Arrays.toString的实施:

String y1 = Arrays.toString(y).replaceAll("\\[|\\]", "");

否则,您可以遍历int数组,并使用StringBuilder附加每个元素。

答案 1 :(得分:0)

试试这个:

        int[] a = { 1, 2, 3, 4, 5 };
        String str = "";
        for (int i : a) {
            str += i + ",";
        }
        str = str.substring(0, str.length() - 1);
        System.out.println(str);

答案 2 :(得分:0)

代码未经过测试,但应该可以解决问题:

final StringBuilder builder = new StringBuilder();
String comma = "";

for (int i : y) {
    builder.append(comma);
    builder.append(i);
    comma = ", ";
}

答案 3 :(得分:0)

试试这个:

String str = Arrays.toString(y);
/*  Will return Array as a string as [30, 34, 45, ....]
*   Now use java  String library functions */

str = (str.replaceAll("}", ""));    
str = (str.replaceAll("{", ""));      

System.out.println(str);