简体   繁体   中英

Java ArrayList<Integer> to ArrayList<String> & vice versa

I'm wondering how to convert ArrayList<String> to ArrayList<Integer> .

I have this code:

ArrayList<String> sNumbers = new ArrayList<String>();
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(1);
numbers.add(2);
numbers.add(3);

sNumbers.add(numbers.toString());
System.out.println(sNumbers);//for testing

I'm getting this:

[[1, 2, 3]]

And that's my question: How can I reverse this procces (Get one line of numbers and put it in numbers ArrayList )

It should be like this:

sNumbers
0 [1, 2, 3] --> 

numbers
0 [1]
1 [2]
2 [3]
(without brackets) 

Invoking toString on a List<Integer> will return a String representation of the List (ie "[1,2,3]" ), not a List<String> containing a String representation of each of its contents.

You must fill your String s manually:

for (Integer i: numbers) {
    sNumbers.add(String.valueOf(i));
}

Note

There is probably some fancier stuff you could do with Java 8 in terms of declarative vs imperative coding - unfortunately i don't have the jdk installed at the moment.

Hopefully someone else will pick this up and answer for Java 8.

As @mena mentioned you can do it with:

pure Java:

List<String> sNumbers = new ArrayList<String>();
for (Integer i: numbers) {
    sNumbers.add(String.valueOf(i));
}

JDK 8 - map:

List<String> sNumbers = numbers.stream().map(
    n -> n.toString()).collect(Collectors.toList()
);

Or use guava library, it's similar to JDK8.

I assume you can find more examples here: Convert List<String> to List<Integer> directly

Heres a simple algorithm for turning the string representation of an array back into an array

public static ArrayList<Integer> arrayStringToIntegerArrayList(String arrayString){
    String removedBrackets = arrayString.substring(1, arrayString.length() - 1);
    String[] individualNumbers = removedBrackets.split(",");
    ArrayList<Integer> integerArrayList = new ArrayList<>();
    for(String numberString : individualNumbers){
        integerArrayList.add(Integer.parseInt(numberString.trim()));
    }
    return integerArrayList;
}

You would call it with

System.out.println(arrayStringToIntegerArrayList(sNumbers.get(0)));

and it will return an ArrayList<Integer> with the values [1, 2, 3]

只需循环遍历数组

for(int i : numbers ) sNumbers.add(String.valueOf(i));

You could use the following loops:

//number 1
for(Integer number : numbers){
            sNumbers.add(String.valueOf(number));
    }

//number 2
for(int i = 0; i < numbers.size(); i++){
    sNumbers.add(String.valueOf(numbers.get(i)));
}

//number 3
int j = 0;
while(numbers.size() > j){
    sNumbers.add(String.valueOf(numbers.get(j)));
    j++;
}

//number 4  
Iterator<Integer> iterator = numbers.iterator();
while(iterator.hasNext()){
    sNumbers.add(String.valueOf(iterator.next()));
}

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