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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:52:38+00:00 2026-06-10T04:52:38+00:00

I am learning Java generics and I am trying to adapt some code I

  • 0

I am learning Java generics and I am trying to adapt some code I developed as an exercise.

In particular, I developed an ArrayVisualizer class that uses Sedgewick’s StdDraw library to visualize and animate the behaviour of a dynamic array. I have my own dynamic array class which supports generics, and I am trying to extend the usage of ArrayVisualizer to anything that is akin to this array.

In short, my question is: how can you deal with generic types containing other generic types?

Here is my thought process:

  • I started by making this Interface:
public interface IterableCollection<Item> {
    void add(Item i);

    Item get(int index) throws IndexOutOfBoundsException; // These can be slow for LinkedList implementations
    void set(int index, Item value) throws IndexOutOfBoundsException;

    int capacity();
    int size(); // Capacity and size can be the same for LinkedList implementations
}
  • Then, I would like my ArrayVisualizer class to take as parameters any class that implements this Interface. After some googling, I found that you can’t write
public class ArrayVisualizer<T implements IterableCollection<?> >

but you have to use extends.

However, even if I write that I can’t use the T class inside my functions.

For example, I have a compiler error if I try to generalize this function:

public static void draw(T<String> vector) throws Exception 

It is like I have to specify that T is also generic, but I can’t find a way to declare it. I have tried the following, and none works:

public class ArrayVisualizer<T<?> implements IterableCollection<?> >
public class ArrayVisualizer<T<S> implements IterableCollection<S> >

RESOURCES: I have uploaded the working non-generic version of my code as a reference.

  • DynamicArray.java

  • ArrayVisualizer.java (this takes some spaced strings as input)

  • InteractiveArrayVisualizer.java (this one doesn’t allow to choose the values of the cells but you can fill them by clicking on the next empty spot and you can empty them by clicking on the last open cell. This is meant to use a debug helper for the resize problem of the dynamic array).

  • 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-10T04:52:40+00:00Added an answer on June 10, 2026 at 4:52 am

    Your ArrayVisualizer class must specify both the collection type, and the element type. You cannot quantify over T<String> like that in Java. (Other languages can do it, but not Java.)

    So, something like….

    class ArrayVisualizer<E, T extends IterableCollection<E>> {
      public void draw(T vector) { ... }
    }
    

    (although if I were you, I would try to use the pre-built Iterable or Collection interface rather than my own IterableCollection interface…)

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

Sidebar

Related Questions

Learning java server technologies, trying to clarify some things. There are few technologies that
I'm learning java. I was trying to run the code, where I got this
I’m just learning Java and trying to have an array of a class. When
I'm a C# guy learning Java and I'm trying to understand how generics work
I learned java generics some time ago, but now I'm learning collections and found
Good morning I am learning Java and trying to understand the access to class
I am learning java generics, and trying to implement it for events before someone
I'm learning generics in Java from C++ and am confused how to do math
I'm learning Java on my own; and therefore the code below has no function
I am learning Java with Swing and I have some problems with using JTextField

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.