简体   繁体   English

创建通用列表的列表

[英]Create a List of generic list

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. 我有一个带通用参数的Boxcar类和一个应该创建Boxcars列表的Train类。 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. 我们应该在单独的主类中指定Boxcar中的类型,因此在此之前,boxcar必须保持通用。 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) 它可以编译,但是在调用load方法时在单独的驱动程序类中,我得到了错误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 我希望这能更好地传达我想要实现的目标

BoxCar which is a generic class: BoxCar是通用类:

class BoxCar<T>{


}

Train class which has a list of Boxcar: 有Boxcar清单的火车课:

class Train {
List<BoxCar<PassTheTypeHere>> = new ArrayList<BoxCar<PassTheTypeHere>>();

}

You need to supply a type in place of T and ? 您需要提供一个类型来代替T? when creating a Generic List. 创建通用列表时。 For example a List of Boxcars containing Strings would look like this: 例如,包含字符串的Boxcar列表如下所示:

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 . 通配符的示例,而T表示在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 ). 这意味着您传递给new Boxcar<type>()任何类型都将被带到内部列表(以及其他期望类型为T的对象的方法)。

Based on the wording of your original question it sounds like you want to create a list of boxcars. 根据原始问题的措辞,听起来您想创建一个棚车清单。

Below is all you would need to do. 以下是您需要做的所有事情。

private List<Boxcar> boxcarList = new ArrayList<Boxcar>(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM