converts an array of integers to an array of strings with a constant number of decimals - Java Collection Framework

Java examples for Collection Framework:Array Join

Description

converts an array of integers to an array of strings with a constant number of decimals

Demo Code


//package com.java2s;
import java.util.ArrayList;

public class Main {
    /**/*from  w  w w  . j  a v  a 2s.com*/
     * converts an array of integers to an array of strings with a constant number of decimals
     * 
     * @param integerArrayList the list of integers that you want to convert
     */
    public static ArrayList<String> intsToPaddedStrings(
            ArrayList<Integer> integerArrayList) {
        if (integerArrayList.size() > 0) {
            ArrayList<String> stringArrayList = new ArrayList<String>();
            int maxInt = integerArrayList.get(0);
            for (Integer i : integerArrayList) {
                if (i.compareTo(maxInt) == 1) {
                    maxInt = i;
                }
            }
            int numDigits = String.valueOf(maxInt).length();
            for (Integer i : integerArrayList) {
                String string = String.valueOf(i);
                while (string.length() < numDigits) {
                    string = "0" + string;
                }
                stringArrayList.add(string);
            }
            return stringArrayList;
        } else
            return null;
    }
}

Related Tutorials