Question: Java I need to write a method for the ArrayIntList class that doubles the size of an arraylist by replacing every number in the list

Java

I need to write a method for the ArrayIntList class that doubles the size of an arraylist by replacing every number in the list with two of that number. e.g.

[2, 10, 12, 8, 14] and we call the list it should give 
[2, 2, 10, 10, 12, 12, 8, 8, 14, 14]

this is what I did so far but it's wrong. Please help.

public void stretch(){ ArrayList list = new ArrayList(); int num; for(int i= 0; i< list.size(); i++){ num= list.get(i); list.add(2 * i, num); list.add(2 * i + 1, num); } for(int i= 0 ; i< list.size(); i++){ System.out.print(" "+ list.get(i)); } }

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!