How to split Integer [] arr into more than one interger [] takes in Java?

I have a comma separated string with ids. I want to split this comma separated line into more than one line if not. more than 500 identifiers.

What can I do: I can convert this string to an integer array and then check its size. Break than an array in multiple arrays, and convert them to comma separated strings.

My code:

 Integer[] empIdInt = null;
     String tokens[]=Application.splitstr(outerArray, ",");
                      if(!ErmUtil.isNull(tokens) && tokens.length>0){       
                          empIdInt=new Integer[tokens.length];
                          for(int i=0;i<tokens.length;i++){
                              empIdInt[i]=Integer.valueOf(tokens[i]);
                          } 
                      }

      

Questions

  • The correct approach to solve this problem?
  • if so, how to split an integer array [] into more than one array?
  • if not, what should I do?

Edit: input: "1,2,3,4,5,6,7,8,9,10,11" // list of ids;

I want to split them more than one line if not. Id more than say 3. How do we do it, 10 no. Id. so the output might be

Output "1,2,3" // 1st string
      "4,5,6" // Second line
      "7,8,9" // Third line
      "10" // 4th line

+3


source to share


5 answers


I am using List<String>

to store your data and by count I am using List.subList (fromIndex, toIndex) to get a sublist from the main list.

Try to enter the code:

        String str = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15";
        String[] ar_str = str.split(",");
        List<String> list = Arrays.asList(ar_str);

        int count = 4;
        int fromIndex = 0;
        int toIndex = count;
        for(int i=0;i<list.size()/count;i++){
            fromIndex = i * count;
            toIndex = fromIndex + count;
            List<String> temp = list.subList(fromIndex, toIndex);
            System.out.println(temp); //Convert List into comma separated String
        }
        if(list.size()%count > 0){
            System.out.println(list.subList(toIndex, list.size()));
        }

      



OutPut

[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10, 11, 12]
[13, 14, 15]

      

May this help you.

+2


source


I don't think it is a good option to convert it to an integer and then count the integers, when you can do the same by counting "," (commas) in your input string. Use Apache StringUtils class which will make your task easier. If I assumed the list size was 2, you would have to change it to 500. To do this, do the following:



import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

public class test {

    public static void main(String[] args) {
        String input = "1,2,3,4,5,6";
        List<String> strList=new ArrayList<String>();
        while (StringUtils.ordinalIndexOf(input, ",", 2) != -1) {

            String s1 = input.substring(0, StringUtils.ordinalIndexOf(input, ",", 2));
            String leftover = input.substring(StringUtils.ordinalIndexOf(input, ",", 2) + 1);
            input = leftover;
            strList.add(s1);
        }
        if(input!=""){
            //for leftover strings which are less than your specified list size
            strList.add(input);
        }
        System.out.println(strList);
    }
}

      

+1


source


    public static List<String> breakStrings(String idListString) {
    int limit = 3;
    char separator = ',';
    String[] idList = idListString.split("\\" + separator);
    List<String> finalList = new ArrayList<String>();

    if (idList != null && idList.length > 3) {
        int j = 0;
        int index = 0;
        StringBuffer oneList = null;

        while (j < idList.length) {
            oneList = new StringBuffer();

            for (int i = 0; i < limit && index < idList.length; i++) {
                boolean isLast = (i + 1) == limit
                        || (index + 1) == idList.length;
                oneList.append(idList[index++]);
                if (!isLast) {
                    oneList.append(separator);
                }
            }

            finalList.add(oneList.toString());
            j += limit;
        }
    } else {
        finalList.add(idListString);
    }

    return finalList;
}

      

This will give you a list of final lines as per your requirement. Hope this helps you.

+1


source


check it

int[] test={10212,10202,11000,11000,11010};
ArrayList<Integer> test2 = new ArrayList<Integer>();


for(int i = test.length -1; i >= 0; i--){
    int temp = test[i];
    while(temp>0){
        test2.add(0, temp%10);  //place low order digit in array
        temp = temp /10;        //remove low order digit from temp;
    }
}

      

0


source


To get the counter ID in a string and split it across multiple Integer arrays, you can do something like this.

    String str = "1234,567,123,4567,890";
    String strArray[] = str.split(",");

    if (strArray.length > 500) {
        Integer[][] ids = new Integer[300][2];
        int j = 0;
        int i = 0;
        for (String s : strArray) {
            if (i < 2) {
                ids[j][i] = Integer.parseInt(s);
            } else {
                i = 0;
                j = j + 1;
            }
        }
    }
}

      

0


source







All Articles