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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:43:26+00:00 2026-06-15T14:43:26+00:00

How do I create a list of generic list? I have a Boxcar class

  • 0

How do I create a list of generic list? I have a Boxcar class that takes a generic argument and a Train class that is supposed to create a list of Boxcars. We are supposed to specify the type that will be in Boxcar in a separate main class, so until then boxcar has to stay generic. The following is the code that I have written. It compiles but in a separate driver class when calling the load method I get the error The method load(capture#1-of ?) in the type Boxcar<capture#1-of ?> is not applicable for the arguments (Person)

package proj5;

 import java.util.ArrayList;
 import java.util.List;

public class Train {

private List<Boxcar<?>> train;
private int maxSpeed;
private int minSpeed;
private String position;
private int numBoxcars;
private int maxNumBoxcars;
private int speed;
private String destination;
private boolean stopped = true;

public Train(int maxSpeed, int minSpeed, int maxNumBoxcars, String position){
    train = new ArrayList<Boxcar<?>>();
    this.maxSpeed = maxSpeed;
    this.minSpeed = minSpeed;
    this.maxNumBoxcars = maxNumBoxcars;
    this.position = position;
}

public int getMaxNumBoxcars(){
    return maxNumBoxcars;
}

public int getSpeed(){
    return speed;
}

public String getPosition(){
    return position;
}

public int getMaxSpeed(){
    return maxSpeed;
}

public int getNumBoxcars(){
    return numBoxcars;
}

public List<Boxcar<?>> getTrain(){
    return train;
}

public void depart(String destination){
    this.destination = destination;
    speed = minSpeed;
    stopped = false;
}

public void arrive(){
    stopped = true;
    position = destination;
}

public void addCar(Boxcar<?> boxcar, int i){
    if(stopped){
        boxcar.setMaxItems(i);
        train.add(boxcar);
    }
}

public void removeCar(int i){
    if(stopped){
        train.remove(i);
    }
}

}

package proj5;

import java.util.ArrayList;
import java.util.List;
import java.util.Collections;

public class Boxcar<T extends Comparable<T>> {

private List<T> boxcar;
private int maxItems;

public Boxcar(){
    boxcar = new ArrayList<T>();
}

public void load(T thing){
    if(!boxcar.contains(thing) && boxcar.size() < maxItems){
        boxcar.add(thing);
        Collections.sort(boxcar);
    }else{

    }
}

public int getMaxItems(){
    return maxItems;
}

public void setMaxItems(int i){
    maxItems = i;
}

public void unload(T thing){
    boxcar.remove(thing);
}

public List<T> getBoxcar(){
    return boxcar;
}

}

I hope this better conveys what I am trying to accomplish

  • 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-15T14:43:28+00:00Added an answer on June 15, 2026 at 2:43 pm

    You need to supply a type in place of T and ? when creating a Generic List. For example a List of Boxcars containing Strings would look like this:

    List<Boxcar<String>> train = new ArrayList<Boxcar<String>>();
    

    The ? is an example of a wildcard, while a T represents a type that is referenced inside the source of List. That point can be tricky to understand without a deeper understanding of Generics, but I wanted to be sure address it for completeness sake. Take a look at this page for more information about how to use Generics inside your code.

    Looking at your revised question, I would direct you to this line of code:

    public class Boxcar<T extends Comparable<T>> {
    

    and then just below it this line:

    private List<T> boxcar;
    

    This means whatever type you pass to new Boxcar<type>() will be carried over to the inner list (and other methods that expect an object of type T).

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

Sidebar

Related Questions

I'am trying to create a generic class that contains a list of generic type
Please explain what this task is about? Create a generic linked list class that
I have a generic items list class to create a more specific listing using
I'm trying to have a generic 'List' class, which will have: Property: Items -
I have a generic list List<T> and I want to create a view that
I want to create a generic list of the Type object. I have ...
I have a Powershell script where I would like to create a generic List
I have a generic list of custom objects and would like to reduce that
How can I use reflection to create a generic List with a custom class
As an exercise I'm trying to create a function that returns a generic list

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.