Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

How would we like to see ourselves?

Answered: 1 week ago