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 a private class in java?
What are invisible components?.
What is anonymous inner class?
Explain the private protected method modifier?
Differentiate between static and non-static methods in java.
Detail discussions on JVM, memory management and garbage collector.
What is thread safe in java?
What are the types of casting?
Which is dependent variable?
How to reverse string in java?
What is a java string?
Is vector thread safe in java?
Is java jre still free?
What about interthread communication and how it takes place in java?
How would you dynamically allocate memory to an array?