If you allocate the following ArrayList, how would you add a new element to it?
ArrayList<String> myList = new ArrayList<String>();
add.myList("Mary");
myList.insert("Mary");
myList.add("Mary");
myList + "Mary";
What method can you use to determine the number of elements in the ArrayList myList?
myList.size()
myList.length()
myList.count()
myList.capacity()
What will be printed by the following code segment?
ArrayList<Integer> numList = new ArrayList<>();
numList.add(1);
numList.add(5);
numList.add(10);
for(Integer i: numList){
System.out.print(i + " ");
}
1 5 10
1
5
10
Nothing. The segment will not compile.
1)
To add new element into list use myList.add("Mary");
So the answer is : myList.add("Mary");
2)
To determine the number of elements in the ArrayList myList myList.size() is used.
if you use myList.length(), myList.count() or myList.capacity() then it will return error.
So the answer is: myList.size()
3)
The program will print each of the element from list and separate it by space.
As numList contain 1,5 and 10 so it print 1 5 10
import java.util.*;
public class Main
{
public static void main(String[] args) {
ArrayList<Integer> numList =
new ArrayList<>();
//add 1 to numList
numList.add(1);
//add 5 to numList
numList.add(5);
//add 10 to numList
numList.add(10);
//using for each print elements of the list
for(Integer i: numList){
//print each of the element from list and separate it by
space.
System.out.print(i + " ");
}
}
}
So the answer is: 1 5 10
Get Answers For Free
Most questions answered within 1 hours.