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 flag in python?
What do you understand by private, protected and public?
What is casting in java programming?
What is object-oriented programming?
Which package has light weight components in java programming?
Write a regular expression to validate a password. A password must start with an alphabet and followed by alphanumeric characters; its length must be in between 8 to 20.
What is the difference between path and classpath variables?
What is a copy constructor in java?
What is a stringbuffer?
What is palindrome in java?
Why parameters should be passed by reference?
What do you mean by thread safe?
What does exp mean in math?
How is string immutable in java?
What is an immutable object?