分割成ArrayList中根据用户输入多的ArrayList [英] Split ArrayList into Multiple ArrayLists depending on user input

查看:197
本文介绍了分割成ArrayList中根据用户输入多的ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个程序:

public static void main(String[] args){
    System.out.println("Enter number of nodes");
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    for(int i=0;i<=n;i++)
    {
        nodes.add(i);
    }

    System.out.println(nodes);
    System.out.println("How many subnetworks you want? Enter Small(10), Med(30), Large(50)");
    small = sc.nextInt();
    med = sc.nextInt();
    large = sc.nextInt();
    System.out.println("Small = " + small + "med" + med + "large" + large);

现在取决于每一个整数的小型,中型和大型并考虑倍数值,我想ArrayList的分割成不同的ArrayList或数组。
例如,小= 100),MED(= 50,大= 10应该分头主要的ArrayList到每一个大小为10 100小的ArrayList,每个尺寸30和10大的ArrayList每个大小为50的50 MED的ArrayList。
 拆分后,我想一些属性分配到sublsits元素。而且我不知道是否应该数组列表或数组或其他任何东西。

Now depending on value of small, medium and large and considering multiples of each of these integers, I want to split ArrayList into different arraylists or array. For example, small = 100, med = 50, large = 10 should split main arraylist into 100 small arraylists each of size 10, 50 med arraylists each of size 30 and 10 large arraylists each of size 50. After the split, I want to assign some properties to elements in sublsits. And I am not sure whether it should be arraylist or array or anything else.

推荐答案

您可以使用拆分列表功能。

You can use split list function.

private static List<List<Integer>> splitAndReturn(List<Integer> numbers,
        int size) {
    List<List<Integer>> smallList = new ArrayList<List<Integer>>();
    int i = 0;
    while (i + size < numbers.size()) {
        smallList.add(numbers.subList(i, i + size));
        i = i + size;
    }
    smallList.add(numbers.subList(i, numbers.size()));
    return smallList;
}

该函数将返回数组的ArrayList大小为尺寸的每个原始。
所以,如果你需要100数组大小为10,然后

The function will return an arrayList of arrays with each raw of size size. So if you need 100 array with size 10, then

splitAndReturn(yourList, 10).subList(0, 100);

将让你的阵列的列表。

will get you the list of arrays.

这篇关于分割成ArrayList中根据用户输入多的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆