1

I have this program:

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);

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.

2
  • Are the sizes of the 100 small arraylists, 50 med,... fixed to 10, 30,...? Or are they determined based on the input of small, med and large? Commented Mar 13, 2014 at 4:28
  • For now I have kept it fixed. If I get this thing working, I will try to do them dynamic like over a range or something Commented Mar 13, 2014 at 4:56

2 Answers 2

3

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;
}

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.

Sign up to request clarification or add additional context in comments.

10 Comments

May I know what actually is count variable doing? And also for list of med size.. where each list is of size 30 and I want 40 such lists then will it be like splitAndReturn(nodes,30).subList(0,40) ?
I am getting IndexOutofBoundsException at line List<Integer> small = numbers.subList(i, numbers.size()+size);
That was a mistake, thanks for making me know. And what error
That was i+size sorry i have made the edit. Please check again
I am giving value for small as 50. Like I need 50 nodes in one List and 10 lists like that. So I should call it like splitAndReturn(nodes, 10).subList(0,50) right? It is giving me following errors Exception in thread "main" java.lang.IndexOutOfBoundsException: toIndex = 50 at java.util.ArrayList.subListRangeCheck(Unknown Source) at java.util.ArrayList.subList(Unknown Source) at project1.Input.main(Input.java:30)
|
0

Well the simple thing to do is to loop through the list and split them accordingly. Considering your example,

small = 100, med = 50, large = 10

first check if the list has enough values

if (list.size() >= ((small*10)+(med*30)+(large*50))){
    for (int i=0;i<small;i++){
        for(int j=0;j<10;j++){
            //copy array value
        }
    }    
    // do the same for med and large.
}

3 Comments

Sorry but I did not understand what do you mean by copy array value?
create the list for the current small outside the inner loop (say smallList) and assign smallList.add(list.get(j))
You could also loop through the list and use subList(int fromIndex, int toIndex) to copy the value to a sublist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.