Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Without more code I can't say for sure, but it is most likely that you're never making <code>series2 = new LinkedList&lt;Number&gt;</code> or something similar.</p> <p>Another problem I see is that you're starting at 1. The <code>List</code> interface implementations are typically 0 indexed, so you'd want</p> <pre><code>for(int num = 0; num &lt; series.size(); num += 2) </code></pre> <p>Your best bet is to provide the entire code (what is necessary to compile and reproduce the problem) and the stack trace you're getting on the error as well.</p> <p>EDIT:</p> <p>It also depends on what you want for an output. Let's pretend your input in series is <code>0,1,2,3,4,5,6,7,8,9</code> and that we're dealing with only 10 numbers.</p> <p>Would you want series 2 to be:</p> <ul> <li><code>null,1,null,3,null,5,null,7,null,9</code></li> <li><code>1,null,3,null,5,null,7,null,9,null</code></li> <li><code>1,3,5,7,9</code></li> </ul> <p>If it's one of the first two, then you need to modify your loop to add the extra ones, like so:</p> <pre><code>for (int num = 0; num &lt; 120; num++) { series2.add(num % 2 == 1 ? series.get(num) : null); } </code></pre> <p>Or alternatively:</p> <pre><code>for(int num = 1; num &lt; 120; num+=2) { series2.add(null); series2.add(series.get(num)); } </code></pre> <p><strong>In any event</strong> attempting to add like this: <code>series2.add(num, series.get(num))</code> will probably fail. According to the documentation for <code>List.add(int,E)</code></p> <blockquote> <p>Throws</p> <p>IndexOutOfBoundsException - if the index is out of range (index &lt; 0 || index > size())</p> </blockquote> <p>So you can only use that to add to the middle of the <code>List</code>. If you want to add to the end, you have to use <code>List.add(E)</code>. If you want to add to some specific index in the <code>List</code> that it doesn't contain yet, you need to add <code>null</code>s (or something) until you get there. Obviously this is a problem if your <code>List</code> implementation doesn't support <code>null</code>s.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload