Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

User Consider the correctness of the following two methods that add an element x at index i in an ArrayList. public void add ( int

User
Consider the correctness of the following two methods that add an element x at index i in an ArrayList.
public void add(int i, T x){
// if( i <0|| i > n) throw Exception
if( n+1> a.length) resize();
for( int j=n; j > i; j--)
a[j]= a[j-1];
a[i]= x;
n++;
}
public void add(int i, T x){
// if( i <0|| i > n) throw Exception
if( n+1> a.length) resize();
for( int j=i+1; j <= n; j++)
a[j]= a[j-1];
a[i]= x;
n++;
}
Select all options below that are true.
A) Neither method is correct
B) The second method is correct
C) The first method is correct

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

1. Which position would you take?

Answered: 1 week ago