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 7191413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:43:24+00:00 2026-05-28T19:43:24+00:00

I have an assignment to implement my own version of Collections.fill() and Collections.reverse(). The

  • 0

I have an assignment to implement my own version of Collections.fill() and Collections.reverse(). The algorithms are simple enough, but I’m getting a little bit lost in the generics involved, specifically when I need to do casting.

My original idea is something like:

public static void reverse(List<?> a_list) {
    int list_size = a_list.size();
    ListIterator<?> left_to_right = a_list.listIterator();
    ListIterator<?> right_to_left = a_list.listIterator(list_size);
    ? temp_variable;

    // ..doing some stuff

    right_to_left.set(temp_variable);

}

But of course I can’t declare the temp as type “?”. Declaring as “Object temp_variable” makes sense, but then the call to set(temp_variable) at the end won’t work (since the ListIterator won’t take Object — because the list is likely not type List<Object>).

It makes sense to me, then to declare temp as an Object, and cast the ListIterators:

ListIterator<Object> left_to_right = 
    (ListIterator<Object>) a_list.listIterator();
ListIterator<Object> right_to_left = 
    (ListIterator<Object>) a_list.listIterator(list_size);

The compiler gives me unchecked cast warnings when I do this, but I can’t think of how this would break, given my implementation. Is there any danger in doing this?

Then in Collections.fill(), I originally wanted to do something like:

public static <E> void fill(List<? super E> a_list, E an_object) {
    ListIterator<E> an_iterator = (ListIterator<E>) a_list.listIterator();

            // ..doing some stuff
    an_iterator.set(an_object);

}

This gives an unchecked cast warning, too. And thinking about it more, even though I still get a warning, would it be safer to do a cast like:

ListIterator<Object> an_iterator = (ListIterator<Object>)a_list.listIterator();

If the generic type of the list is actually a superclass of E, it should be safer to cast to a ListIterator for a List of Objects, and not to a ListIterator for a subclass of the list’s generic type, shouldn’t it?

Any advice would be appreciated…I don’t think our instructor wanted us to get this involved, but I’d like to understand generics better.

  • 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-05-28T19:43:25+00:00Added an answer on May 28, 2026 at 7:43 pm

    Don’t cast, and don’t use ?. Use type-parameterized methods; something like this.

    public static <T> void reverse(List<T> a_list) {
        int list_size = a_list.size();
        ListIterator<T> left_to_right = a_list.listIterator();
        ListIterator<T> right_to_left = a_list.listIterator(list_size);
        T temp_variable;
    
        while(left_to_right.nextIndex() < list_size / 2) {
            temp_variable = left_to_right.next();
            left_to_right.set(right_to_left.previous());
            right_to_left.set(temp_variable);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an assignment and I need to make my own (simple) generic linked
My assignment is to implement a multithreaded web server in Java but i have
I have an assignment to implement a Ram-Lak filter, but nearly no information given
I have the following assignment: Implement a basic shopping basket without using any predefined
Hi I have a homework assignment where I need to implement an intersection of
I have to implement a search algorithm for a school assignment. Right now, i'm
I have an assignment that I'm supposed to implement the MIPS processor in C++
I have got an assignment to edit a priority-queue and implement (among other things)
I have an assignment that requires us to implement a doubly linked list class.
I am working on a homework assignment where I have to implement a MergeSort

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.