简体   繁体   中英

Conversion of Integer Arraylist to string

I'm trying to convert the numbers of my Array list into Strings at point i. How do I do this so I can create substrings of my numbers?

ArrayList<Integer> numbers= new ArrayList<Integer>();
for( int i=0; i<=10; i++){
    String numbersString[i] = String.valueOf(numbers[i]);
}

If numbers[i] is of type Integer you can just use its built in toString() method. However, as numbers is an ArrayList , you need to use numbers.get(i) .

String numbersString[i] = ... is invalid syntax. You have to declare your array outside the loop and then access it simply by numbersString[i] = ... inside the loop.

I would suggest something like this.

StringBuilder sb = new StringBuilder();
for (Integer number : numbers) {
  sb.append(number != null ? number.toString() : "");
}
System.out.println("The number string = " + sb.toString());

Looking your code you need a input of ArrayList and output of String[].

You can use Collections2 of Guava lib to transform to string and after parse to array.

        ArrayList<Integer> numbers = new ArrayList<Integer>();

        Collection<String> transform = Collections2.transform(numbers, new Function<Integer, String>() {

            @Override
            @Nullable
            public String apply(@Nullable Integer input) {
                return input.toString();
            }
        });

        final String[] array = transform.toArray(new String[transform.size()]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM