如何使用java示例程序将字符串值拆分为数组列表值(总数组大小为300)

时间:2016-02-15 14:31:29

标签: java arraylist split

我在java中创建了一个示例程序。概念是拆分字符串值以逐个生成数组列表,这意味着总字符串输入如下:

第一步总大小:

Sting numberlist ="1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890........................1234567890";
ArrayList aList= new ArrayList(Arrays.asList(numberlist.split(",")));

for (int i = 0;i<aList.size();i++)
{
    System.out.println(" new number-->" + aList.get(i));
    // Toast.makeText(getApplicationContext(),"number    is"+aList.get(i),Toast.LENGTH_SHORT).show();
}

我得到了答案+ alist.get(i)= 300,比第二步知道我的总大小为300 该过程逐个分割值

显示前100个值1234567890,1234567890,1234567890,.....

显示第二个100值1234567890,1234567890,1234567890,.....

显示第三个100值1234567890,1234567890,1234567890,.....

示例程序是:

    String numberlist="1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890........................1234567890";

    int splitNumber = 100;
    int length = numberlist.length();
    ArrayList<String>splitList = new ArrayList<String>();
    int splitCount = 0;

    for(int i = 1 ; i<= length;i++)
    {
        splitCount++;
        boolean last = false;

        if(i==length)
        {
            last = true;
        }

        if((splitNumber == splitCount) || last)
        {
            String number = numberlist.substring(splitList.size()*splitNumber,i);
            number = number.endsWith(",") ? number.substring(0,number.length()) : number;
            number = number.startsWith(",") ? number.substring(1,number.length()) : number;
            splitList.add(number);
            splitCount = 0;
        }
    }

    for(String number : splitList)
    {
        System.out.println();
        System.out.println("Display First 100 value"+number);
    }
}

但是程序中的错误并不知道有人给我解决方案的问题在哪里。

我需要输出如下:

显示前100个值1234567890,1234567890,1234567890,.....

显示第二个100值1234567890,1234567890,1234567890,.....

显示第三个100值1234567890,1234567890,1234567890,.....

它一个接一个..

注意:对不起我的语法错误..

2 个答案:

答案 0 :(得分:0)

如果我正确地理解了这个问题..你可以显示100个元素和剩下的任何东西。

    for (int i = 0; i < s.length(); i += 100) {
        if ((i+100) > s.length()) {
            System.out.println(s.substring(i, s.length()));
        } else {
            System.out.println(s.substring(i, i + 100));
        }
    }

答案 1 :(得分:0)

根据您的问题判断,我假设元素的数量可以被splitNumber整除。有几种方法可以解决这个问题。在这里,我使用modulo创建一个用于创建大小为splitNumberint分组的组来获取组号:

public class Test {

    public static void main(String[] args) throws Exception {

        String numberlist = "1234,1234,1234,1234,1234,1234,1234,1234,1234";
        int splitNumber = 3;
        ArrayList<String> splitList = new ArrayList<String>(Arrays.asList(numberlist.split(",")));

        if (splitList.size() % splitNumber != 0)
            throw new Exception("Number of elelemnts not divisable by split number");

        for (int i = 0; i < splitList.size(); i += splitNumber) {
            System.out.print(((i / splitNumber) + 1) + " group of " + splitNumber +": ");
            for (int j = 0; j < splitNumber; j++) {
                System.out.print(splitList.get(i + j) +", ");
            }
            System.out.println();
        }
    }
}

输出:

1 group of 3: 1234, 1234, 1234, 
2 group of 3: 1234, 1234, 1234, 
3 group of 3: 1234, 1234, 1234, 

执行自己的格式化。