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 / sreekanth madamanchi
Step 1: The array size: 4
array is list[0]=2, list[1]=3, list[2]=4, list[3]=5;
i=0;i<4;i++
it removes the 0th element, means it removes 2.
now the array is list[0]=3, list[1]=4, list[2]=5
Step 2: Now the size of the array is 3
now i=1 (bcz i++)
i<3;i++
it removes the 1st element, means it removes 4 (Bcz i=1).
now the array is list[0]=3, list[1]=5
Step 3: Now the size of the array is 2
now i=2
the condition is fail in for loop (bcz i=2. means 2<2)
So it won't go inside the loop
So the size after:2
The final Output is
size :4
Size after:2
| Is This Answer Correct ? | 13 Yes | 0 No |
Post New Answer View All Answers
What is included in core java?
What is the use of arraylist class in java?
What is a java string?
What will happen to the exception object after exception handling?
Does string is thread-safe in java?
What are the 6 mandatory procedures for iso 9001?
What are generic methods?
What methodology can be employed to locate substrings inside a string?
What is the method overriding?
Which sorting algorithm is in place?
Can static methods access instance variables in java?
How many types of constructors are used in java?
What comes to mind when someone mentions a shallow copy in java?
State one difference between a template class and class template.
What is the use of a conditional inclusion statement in Java ?