2

I need to access all elements of a list, but only ever 10 elements at most in one go. For this I thought about a nested loop like this:

for (int i = 0; i < 50 / 10; i++) {
    for (int k = 0; k < 10; k++) {
        paramList.addParam(xyz)
    }
    sendRequest(paramList);
}

With this nested loop I can access all elements from 0-49 and only ever 10 elements in the inner loop. My problem occurs when the list only hast 49 elements instead of 50. Then I only get up to 39 elements with this logic. Also my best thought on only accessing e.g. 5 elements in the last iteration (list size = 45), is to break the loop, before it goes any further.

So my question is, how can I access all elements in a List with undefined size, but only a maximum of 10 elements in the inner loop (and less if list size doesn't allow 10)?

12
  • Why not add a break on condition (i * 10) + k > size? Commented Mar 21, 2018 at 10:34
  • Why not simply go for(int i = myStartIndex; i < myStartIndex + 10 && i < myList.size(); ++i)? Commented Mar 21, 2018 at 10:37
  • @Ben you'd end up processing elements 0-10, 1-11, 2-12 etc. You need to add 10 onto i each time. Commented Mar 21, 2018 at 10:39
  • @user7 makes no sense because with my current loop i wont even got to e.g. 45 if that is the list size Commented Mar 21, 2018 at 10:40
  • OP, what do you mean by access all elements of a list, but only ever 10 elements at most in one go? Can you clarify what you mean by access, and why you are printing a modified value of i? Commented Mar 21, 2018 at 10:41

3 Answers 3

3

As an option you can just iterate in outer loop using step of 10 elements:

int step = 10;
for (int i = 0; i < list.size(); i+=step) {
    for (int k = i; k < i + step && k < list.size()); k++) {
        System.out.println(list.get(k));
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Is it possible to not use Math.min() for this?
@XtremeBaumer You could use a ternary operator for it... (i+step)>list.size()?list.size():i+step
@XtremeBaumer as an option you can use ternary operator, so it would be something like: k < i + step <= list.size() ? i + step : list.size() but honestly I think min here seems to be just fine, or you can just use a condition as @Kepotx mention
This kind of loops are not easily understood, I suggest good comment or simply find a better approach for any production environment.
2

I had to find a reason for your need, breaking the iteration with an inner loop like your snippet doesn't do anything useful, it's still iterate a list.

The only reason I see your logic needed is if you want to call a method after 10 element like :

for (int i = 0; i < 50 / 10; i++) {
    for (int k = 0; k < 10; k++) {
        System.out.println((i * 10) + k);
    }
    System.out.println("####");
}

That would make sense, but that second loop is not needed and also give a "complex" code to understand.

As an alternative, you can iterate normally a list and add a condition to execte that method. It gives a much more readable code

for (int i = 0; i < list.size(); i++) {
    System.out.println(list.get(i));

    if( (i + 1)%10 == 0){ //add one to not trigger this on the first iteration
        System.out.println("####");
    }
}

From your comment:

I retrieve up to 50 records from my database. For each record I need to create a request which will be send in a POST request. Each POST request can contain 10 data requests and I can only send 5 of those request per minute (5*10=50). Since I always fetch TOP 50 data from my database, it can happen that I only get 35 entries instead of 50. To create my requests, I need that data from my database and therefore I have to iterate the whole list, but only ever 10 elements in 1 go to create 1 full POST request

I can see the need here, let's assume you have an instance post that have two method addParam and sendRequest. You need to iterate the list to send String parameters :

int i = 0; 
for(String s : params){
    post.addParam(s);
    if( ++i % 10 == 0){
         post.sendRequest();
    }
}
//If the last `addParam` was not sended, we check again.
if( i % 10 != 0){
     post.sendRequest();
}

Note the condition change outside the loop

2 Comments

Very nice approach. Somehow I was thinking that nested loops are the best (and only) way to do it and never even thought about just adding a condition to trigger what I need. Yet I found it hard to come up with a solution using nested loops
@XtremeBaumer I have added some content based on your comment (the context). That give a readable solution that's easily undestood I believe. Nested-loop are sometimes a bit more difficult to understand because of the scope. On some cases, a sheet of paper is the best approch to understand one ;) or good comments !
2

as user7 say, you can add a condition to the nested loop

for (int i = 0; i < list.size(); i+=10) {
    for (int k = i; k < list.size() && (k-i) < 10; k++) {
        System.out.println(k);
    }
}

5 Comments

I dont think that the check && i < 10 is even needed in the inner loop
@XtremeBaumer no, it's needed, otherwise you will each time iterate through the whole collection starting from the index i
I tested this solution and its not working as needed at all. It only iterates to the list size once and that in the inner loop. Only became clear, when adding a System.out.println() to the outer loop
@XtremeBaumer that's because of a small mistake in this peace of code, but the idea is good, I edited my solution according to this proposal.
just small mistake, it's not "i < 10" but "(k-i) < 10" or "k < i + 10", as you want

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.