2

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

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

the question such as > what is it you are trying to accomplish? should be part of the comment rather then an answer
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.