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
What is vector capacity in java?
What is api in java?
describe method overloading
Explain about doubly linked list
What is a Null object?
Why super is first line in java?
What is e in java?
How to reverse a string in java?
Can we change the scope of the overridden method in the subclass?
What is your platform’s default character encoding?
What's the difference between an abstract class and interface in java?
Explain the polymorphism principle?
What is an infinite loop? How infinite loop is declared?
What is the difference between abstract class and interface1? What is an interface?
What is an immutable object? How do you create one in java?