java.lang.IndexOutOfBoundsException index:12 size 12 java -
this question has answer here:
problem: trying take string input files , store in arraylist trying search specific words , removing them , resultant output should go text file... code works fine if there 2 elements in list . if put more showing java.lang.indexoutofboundsexception index:12 size 12 . not in java pls , sorry silly mistakes ...
import java.io.bufferedreader; import java.io.filereader; import java.io.printwriter; import java.util.arraylist; import java.util.list; public class replace{ public static void main(string[] args) throws exception{ string filen = "c:/users/damini/desktop/hi.txt"; filereader file = new filereader("c:/users/damini/desktop/rules.txt"); bufferedreader b = new bufferedreader(file); printwriter pw = new printwriter(filen); list<string> temps = new arraylist<string>(10); string line = b.readline(); while(line!= null) { temps.add(line); line = b.readline(); } int size = temps.size()-1; for(int i=0;i<=size;i++) { if(temps.get(i).equals("hello")) { temps.remove(i); } } pw.println(temps); pw.close(); system.out.println("output:"+ temps); b.close(); } }
try write arraylist temps without capacity 10 this:
list<string> temps = new arraylist<string>(); - the
capacityhow many elements list can potentially accommodate without reallocating internal structures.
Comments
Post a Comment