在Java中将整数数组转换为字符串的最佳方法

发布于 2024-09-19 22:48:27 字数 121 浏览 7 评论 0原文

在 Java 中,我有一个整数数组。有没有一种快速的方法将它们转换为字符串?

IE int[] x = new int[] {3,4,5} x toString() 应该产生“345”

In Java, I have an array of integers. Is there a quick way to convert them to a string?

I.E. int[] x = new int[] {3,4,5}
x toString() should yield "345"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

时光清浅 2024-09-26 22:48:27

最简单的高性能方法可能是 StringBuilder:

StringBuilder builder = new StringBuilder();
for (int i : array) {
  builder.append(i);
}
String text = builder.toString();

如果您发现自己在多个地方执行此操作,您可能需要查看 Guava 的 Joiner - 尽管我不相信您能够将它用于原始数组。编辑:如下所述,您可以使用 Ints.join 为此。

Simplest performant approach is probably StringBuilder:

StringBuilder builder = new StringBuilder();
for (int i : array) {
  builder.append(i);
}
String text = builder.toString();

If you find yourself doing this in multiple places, you might want to look at Guava's Joiner class - although I don't believe you'll be able to use it for primitive arrays. EDIT: As pointed out below, you can use Ints.join for this.

空心空情空意 2024-09-26 22:48:27
   int[] x = new int[] {3,4,5};
   String s = java.util.Arrays.toString(x).replaceAll("[\\,\\[\\]\\ ]", "")

更新

为了完整性,Java 8 Streams 解决方案,但它并不漂亮(像 vavr< /a> 会更短更快):

String s = IntStream.of(x)
  .mapToObj(Integer::toString)
  .collect(Collectors.joining(""));
   int[] x = new int[] {3,4,5};
   String s = java.util.Arrays.toString(x).replaceAll("[\\,\\[\\]\\ ]", "")

Update

For completeness the Java 8 Streams solution, but it isn't pretty (libraries like vavr would be shorter and faster):

String s = IntStream.of(x)
  .mapToObj(Integer::toString)
  .collect(Collectors.joining(""));
游魂 2024-09-26 22:48:27

尝试这样做 - 您必须导入 java.util.Arrays ,然后 -

String temp = Arrays.toString( intArray ).replace(", ", "");
String finalStr = temp.substring(1, temp.length()-2);

其中 intArray 是您的整数数组。

Try with this - you have to import java.util.Arrays and then -

String temp = Arrays.toString( intArray ).replace(", ", "");
String finalStr = temp.substring(1, temp.length()-2);

Where intArray is your integer array.

披肩女神 2024-09-26 22:48:27
StringBuffer str =new StringBuffer();
for(int i:x){  
str.append(i);
}  

您至少需要阅读一遍。

StringBuffer str =new StringBuffer();
for(int i:x){  
str.append(i);
}  

You need to read all once at least.

情栀口红 2024-09-26 22:48:27
public static void main(String[] args) {
    int[] dice = {1, 2, 3, 4, 5, 0};
    String string = "";

    for (int i = 0; i < dice.length; i++) {
        string = string + dice[i];
    }

    System.out.print(string);   

}

这是您可以做到的另一种方法。基本上只是创建一个 for 循环来访问整数数组中的每个元素并将其添加到字符串中。

public static void main(String[] args) {
    int[] dice = {1, 2, 3, 4, 5, 0};
    String string = "";

    for (int i = 0; i < dice.length; i++) {
        string = string + dice[i];
    }

    System.out.print(string);   

}

This is another way you can do it. Basically just makes a for-loop that accesses every element in the integer array and adds it to the string.

吻泪 2024-09-26 22:48:27
 public static void main(String[] args) {

    System.out.println("Integer Value :" +convertIntToString(new int[]{3,4,5}));

  }

  public static String convertIntToString(int intArray[]) {
    List<Integer> listInteger = new ArrayList<Integer>();

    for (int i = 0; i < intArray.length; i++) {
        listInteger.add(intArray[i]);
    }

    Object o = listInteger;

    return String.valueOf(o).replace("[", "").trim().replaceAll(", ","").trim().replaceAll("\\]","").trim();
  }
 public static void main(String[] args) {

    System.out.println("Integer Value :" +convertIntToString(new int[]{3,4,5}));

  }

  public static String convertIntToString(int intArray[]) {
    List<Integer> listInteger = new ArrayList<Integer>();

    for (int i = 0; i < intArray.length; i++) {
        listInteger.add(intArray[i]);
    }

    Object o = listInteger;

    return String.valueOf(o).replace("[", "").trim().replaceAll(", ","").trim().replaceAll("\\]","").trim();
  }
所有深爱都是秘密 2024-09-26 22:48:27
public static void main(String[] args) {
    System.out.println("Integer Value :" +convertIntToString(new int[]{3,4,5}));
}

public static String convertIntToString(int intArray[]) {
    List<Integer> listInteger = new ArrayList<Integer>();

    for (int i = 0; i < intArray.length; i++) {
        listInteger.add(intArray[i]);
    }

    Object o = listInteger;

    return String.valueOf(o).replace("[", "").trim().replaceAll(", ","").trim().replaceAll("\\]","").trim();
}
public static void main(String[] args) {
    System.out.println("Integer Value :" +convertIntToString(new int[]{3,4,5}));
}

public static String convertIntToString(int intArray[]) {
    List<Integer> listInteger = new ArrayList<Integer>();

    for (int i = 0; i < intArray.length; i++) {
        listInteger.add(intArray[i]);
    }

    Object o = listInteger;

    return String.valueOf(o).replace("[", "").trim().replaceAll(", ","").trim().replaceAll("\\]","").trim();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文