Question: 2. The following declaration will compile ok. Show a better way to declare the same variable. ArrayList numbers; 3. The method shown below is supposed

2. The following declaration will compile ok. Show a better way to declare the same variable.

ArrayList numbers;

3. The method shown below is supposed to remove all negative values from a given List of

Integers.

(a) Explain why this could be very inefficient.

(b) Explain why it fails for the List: [9,0,-3,-2,4]

Hint: Run this method in your Java IDE.

(b) Rewrite the method using an Iterator.

void meth (List nums)

{

for (int i=0; i<100; i++)

if (nums.get(i) < 0)

nums.remove(i);

}

4. Try the following using your Java IDE (if using BlueJ, this is easily done with the

CodePad). Show the value of each of the following expressions:

String name = "Mary";

name == "Mary"

name .equals ("Mary"))

name == new String ("Mary"))

name .equals (new String("Mary"))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!