What will be the output of the program?
public class Test {
public static void main(String args[]) {
ArrayList<String> list = new ArrayList<String>();
list.add("2");
list.add("3");
list.add("4");
list.add("5");
System.out.println("size :"+list.size());
for(int i=0;i<list.size();i++) {
list.remove(i);
}
System.out.println("size after:"+list.size());
}
}
Answer Posted / qim2010
The final Output is
size :4
Size after:2
While called ArrayList.remove() method, in remove method, we
can pass object or index number to remove.
// remove array list element by index number
list.remove(2); //will remove the second element
// remove ArrayList element by Object value
list.remove("2"); //will remove the element
| Is This Answer Correct ? | 0 Yes | 1 No |
Post New Answer View All Answers
Can we call a non-static method from inside a static method?
How an object is serialized in java?
What is difference between static and final?
Explain super keyword in java.
What is executor memory?
How to compare strings in java?
What are namespaces in java?
What are autoboxing and unboxing? When does it occur?
How do you use substring in java?
What is the difference between yielding and sleeping?
can rmi and corba based applications interact ?
What is Enum in Java?
Can we override constructor?
How to restrict a member of a class from inheriting by its sub classes?
What are the types of java languages?