I can’t figure this out. I have the following code:
import java.util.ArrayList;
import java.util.List;
public class TestLoop {
public static List<String> strArray = new ArrayList<String>();
static {
strArray.add("Some");
strArray.add("Silly");
strArray.add("String");
}
public static void main(String[] args) {
int doNotPrintIndex = 1;
int beginIndex = doNotPrintIndex == 0 ? 1 : 0;
for (int i = beginIndex; i < strArray.size() && i != doNotPrintIndex; i++) {
System.out.println(strArray.get(i));
}
}
}
Essentially, if I have an ArrayList, I never want to print out what is stored at index ‘doNotPrintIndex‘ within the ArrayList. However, in the following case, the loop only executes once. Is there something wrong in my logic?
Your loop is exiting as soon as
ihitsdoNotPrintIndex. Try this loop:Alternatively, you could have two loops one after another: