java - unable in removing an element using iterator -
i had entered 3 elements using vector , trying remove specific elemnts list unable so.
i getting following error while compiling:
iterator.java:28: error: 'void' type not allowed here {system.out.println(e.printstacktrace());} ^ 1 error
where doing wrong.
import java.io.*; import java.util.*; import java.lang.*; class iterator { public static void main(string[] args) { vector<integer> v= new vector<integer>(); //vector<string> v= new vector<string>(); v.addelement(new integer(10)); v.addelement(new integer(20)); v.addelement(new integer(30)); //v.addelement(new string("mayank")); //v.addelement(new boolean(true)); //enumeration e = v.elements(); iterator = v.iterator(); while(i.hasnext()) { system.out.println(i.next()); } try{ v.remove(10); } catch(exception e) {system.out.println(e.printstacktrace());} //system.out.println("remove error exception");} while(i.hasnext()) { system.out.println(i.next()); } } }
e.printstacktrace()
need. since returns void, have println trying print void doesn't work (and doesn't make sense either).
solution: rid of system.out.println
. call e.printstacktrace()
. better yet, use logging.
edit: also, remove integer 10 value, consider doing:
remove(new integer(10));
so jvm doesn't think remove parameter represents index position of item wish remove.
edit 2: note contrary question title, not using iterator remove anything.
edit 3: removing without using iterator
import java.util.iterator; import java.util.vector; class hisiterator { public static void main(string[] args) { vector<integer> v = new vector<integer>(); v.addelement(new integer(10)); v.addelement(new integer(20)); v.addelement(new integer(30)); iterator<integer> = v.iterator(); while (i.hasnext()) { system.out.println(i.next()); } try { v.remove(new integer(10)); } catch (exception e) { e.printstacktrace(); } system.out.println(); = v.iterator(); while (i.hasnext()) { system.out.println(i.next()); } } }
edit 4: iterator
import java.util.iterator; import java.util.vector; class hisiterator { public static void main(string[] args) { vector<integer> v = new vector<integer>(); v.addelement(new integer(10)); v.addelement(new integer(20)); v.addelement(new integer(30)); iterator<integer> = v.iterator(); while (i.hasnext()) { integer nexti = i.next(); system.out.println(nexti); if (nexti.equals(integer.valueof(10))) { i.remove(); } } // try { // v.remove(new integer(10)); // } catch (exception e) { // e.printstacktrace(); // } system.out.println(); = v.iterator(); while (i.hasnext()) { system.out.println(i.next()); } } }
Comments
Post a Comment