I want to display all elements in an ArrayList, except element 10 (which is the word “will”). How do I do this? When I run the below code it shows nothing.
private void practiceButtonActionPerformed(java.awt.event.ActionEvent evt) {
ArrayList <String> practice1 = new ArrayList();
Collections.addAll(practice1, "If", "she", "boarded", "the", "flight"
, "yesterday", "at", "10:00,", "she", "will", "be"
, "here", "anytime", "now.");
contentTextPane.setText(practice1.get(0+1+2+3+4+5+6+7+8+9+11+12+13));
}
The line
Does not do what you think it does. This will compute the value of 0 + 1 + 2 + … + 13, then look up that entry in the
ArrayList. Since yourArrayListdoesn’t have this many elements, it will throw anIndexOutOfRangeexception.If you want to display everything except the tenth element, try using a loop:
Hope this helps!