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 create constructor in abstract class ?
Why does my function print none?
What is concurrent hashmap and its features?
give an example for encapsulation?
Can constructor be inherited?
How do you sort in ascending order in java?
What is main method?
Where is const variable stored?
Which is better singleton or static class?
What does g mean in regex?
What is int lol?
How does compareto work in java?
How do you create a null object?
What is comparable and comparator interface? List their differences
How do you end a program?