Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7918761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:37:02+00:00 2026-06-03T15:37:02+00:00

I have the following code segment: public void reorder(int fromIndex, int toIndex) { getElements().add(toIndex,

  • 0

I have the following code segment:

public void reorder(int fromIndex, int toIndex) {
    getElements().add(toIndex, getElements().remove(fromIndex));
}

Here, the method getElements has the return type List<?>. The remove method has the return type ?, and the add method shows its arguments as int index, ? element. So my assumption was, since the return type of remove method and second argument of add method are the same – ? – the method call must succeed. But, I was wrong, the above code segment results in the error:

The method add(int, capture#17-of ?) 
in the type List<capture#17-of ?> 
is not applicable for the arguments (int, capture#18-of ?)

Here, I don’t have any direct access to the list, and I don’t know it’s original type returned by getElements method. All I want here is to remove the item at fromIndex and put it at toIndex. So, how do I achieve that? Also is there anything wrong with my understanding of the generics?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T15:37:03+00:00Added an answer on June 3, 2026 at 3:37 pm

    Just add a cast that makes that ? concrete:

    public void reorder(int fromIndex, int toIndex) {
      final List<Object> els = (List<Object>)getElements();
      els.add(toIndex, els.remove(fromIndex));
    }
    

    Since you are just rearranging elements within the list, this will never cause any trouble. But I must say there’s something wrong with the design if you are seeing that kind of return value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a controller class as in the following code segment: @Controller public class
I have the following piece of code: public interface Segment<T> extends Period { ...
I have two identical byte arrays in the following segment of code: /// <summary>
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have the following code segment: var framelist = BL.FooBL.GetFrame() ; var foreach( var
I have the following code segment in which I'm getting error using (var session
I have the following code segment: var run = 0; var obj = {'item1':0,'item2':5,'item3':10};
So I have the following code segment in x86 assembly: mov $0x0, %eax callq
I have the following code segment: std::list<reply_t> l = m[index]; for (std::list<reply_t>::iterator it =
I have the following segment of code in which I want to Group everything

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.