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


Please Help Members By Posting Answers For Below Questions

What is flag in python?

544


What do you understand by private, protected and public?

510


What is casting in java programming?

574


What is object-oriented programming?

563


Which package has light weight components in java programming?

670






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.

583


What is the difference between path and classpath variables?

518


What is a copy constructor in java?

572


What is a stringbuffer?

506


What is palindrome in java?

550


Why parameters should be passed by reference?

490


What do you mean by thread safe?

530


What does exp mean in math?

522


How is string immutable in java?

536


What is an immutable object?

575