3 ArrayLists that creates an ArayList -Integers object named list and fills list
ID: 3891549 • Letter: 3
Question
3 ArrayLists that creates an ArayList -Integers object named list and fills list with these numbers (using one or a pair of for or while loops): 3.2 Consider the ArrayList-Integer> object named list containing these Integers: list 1, 2, 3, 4, 5, 4, 3, 2, 1, o What are the contents of list after this loop completes? for (int i-1; i object named list are negative. 3.4 Write a method named arrayListSum) that has one parameter pList which is an object of the class ArrayList 3.5 Write a method named named arrayLisiCreate) that has two int parameters plen and plnitValue. The method shall Print the count after the loop terminates. Integer>. The method shall return the sum of the elements of pList. create and retun a new ArrayList-Integer? object which has exactly plen elements. Each element shall be initialized to plnit Value. 3.6 Write a void method named insertName0 that has two input parameters: (1) pList which is an object of ArrayList
Explanation / Answer
Question 3.1
ArrayList<Integer> list = new ArrayList<Integer>(10);
// Add elements to ArrayList
for(int i=0;i<=4;i++)
{ list.add(i);
}
for(int i=0;i<=4;i++)
{ list.add(i);
}
Question 3.2
The contents of the list after the execution of the for loop is :
{1,3,6,10,15,19,22,24,25,25}
Question 3.3
int ct=0;
for(int i=0;i<10;i++)
if(list.get(i)<0)
ct++;
System.out.println("Number of negative values "+ ct);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.