Java 对每种类型创建的对象分别计数 类节点{ 私人电子数据; 私有节点下一步; 节点(E数据,节点下一个){ 这个数据=数据; this.next=next; } //…接球手 }

Java 对每种类型创建的对象分别计数 类节点{ 私人电子数据; 私有节点下一步; 节点(E数据,节点下一个){ 这个数据=数据; this.next=next; } //…接球手 },java,generics,Java,Generics,为了统计创建了多少Node类的对象,我们可以在类中添加一个静态属性,并在创建新对象时递增该属性 如何保持为每种类型创建的节点实例的计数? (例如,6个节点实例的类型为Integer,4个节点的类型为Float等) 一种简单的方法是为每种类型(例如IntCount、FloatCount)添加静态变量,并根据传递给构造函数的数据实例对其进行增量 class Node<E> { private E data; private Node<E> next;

为了统计创建了多少Node类的对象,我们可以在类中添加一个静态属性,并在创建新对象时递增该属性

如何保持为每种类型创建的节点实例的计数? (例如,6个节点实例的类型为Integer,4个节点的类型为Float等)

一种简单的方法是为每种类型(例如IntCount、FloatCount)添加静态变量,并根据传递给构造函数的数据实例对其进行增量

class Node<E> {
     private E data;
     private Node<E> next;
     Node(E data, Node<E> next) {
         this.data = data;
         this.next = next;
     }
     // .. getter-setters
}
类节点{
私人电子数据;
私有节点下一步;
私有静态整数全局计数;
私有静态int计数,FloatCount;
静止的{
GlobalCount=IntCount=FloatCount=0;
}
节点(E数据,节点下一个){
这个数据=数据;
this.next=next;
GlobalCount++;
if(整数的数据实例)
IntCount++;
else if(浮点数据实例)
FloatCount++;
}
}
目前,这将只跟踪整数和浮点的计数。如果要跟踪的类型数量增加,上述修改将不是一个好的解决方案


我想知道是否有更好的方法来实现这一点。

您可以使用以下
HashMap

class Node<E> {
    private E data;
    private Node<E> next;
    private static int GlobalCount;
    private static int IntCount, FloatCount;

    static {
        GlobalCount = IntCount = FloatCount = 0;
    }

    Node(E data, Node<E> next) {
        this.data = data;
        this.next = next;
        GlobalCount++;

        if(data instanceof Integer) 
            IntCount++;
        else if (data instanceof Float) 
            FloatCount++;
    }
}
static HashMap refCount=new HashMap();
节点(E数据,节点下一个)
{
这个数据=数据;
this.next=next;
GlobalCount++;
类类型=data.getClass();
整数countObj=refCount.get(类型);
整数计数=1;
if(countObj!=null)
{
count=countObj.intValue();
计数++;
}
refCount.put(类型、计数);
}

您可以使用以下
HashMap

class Node<E> {
    private E data;
    private Node<E> next;
    private static int GlobalCount;
    private static int IntCount, FloatCount;

    static {
        GlobalCount = IntCount = FloatCount = 0;
    }

    Node(E data, Node<E> next) {
        this.data = data;
        this.next = next;
        GlobalCount++;

        if(data instanceof Integer) 
            IntCount++;
        else if (data instanceof Float) 
            FloatCount++;
    }
}
static HashMap refCount=new HashMap();
节点(E数据,节点下一个)
{
这个数据=数据;
this.next=next;
GlobalCount++;
类类型=data.getClass();
整数countObj=refCount.get(类型);
整数计数=1;
if(countObj!=null)
{
count=countObj.intValue();
计数++;
}
refCount.put(类型、计数);
}

不要将计数器设为“静态”

我的数据结构类

static HashMap<Class, Integer> refCount = new HashMap<Class, Integer>();

Node(E data, Node<E> next) 
{
    this.data = data;
    this.next = next;
    GlobalCount++;

    Class type = data.getClass();

    Integer countObj = refCount.get(type);
    int count = 1;
    if(countObj != null)
    {
        count = countObj.intValue();
        count++;
    }
    refCount.put(type, count); 
}
显示计数器特定于LinkedList实例

为什么。。。我理解,对于普通对象类中的计数器,您将使用静态计数器。“静态”计数器通常会在对象的所有实例中引用,因此所有增量或减量都会影响同一个计数器

当实例化任何类型的泛型类时,“static”变量绑定到
GenericsClass
的实例,而不是
GenericsClass
。因此在我的例子中,
静态int size
将是LinkedList所有实例中的常见int变量,而不管它是
LinkedList
还是
LinkedList

因此,在使用基于泛型的数据结构时,使用常规实例变量作为计数器是正确的

希望这有帮助


约翰

不要使计数器“静止”

我的数据结构类

static HashMap<Class, Integer> refCount = new HashMap<Class, Integer>();

Node(E data, Node<E> next) 
{
    this.data = data;
    this.next = next;
    GlobalCount++;

    Class type = data.getClass();

    Integer countObj = refCount.get(type);
    int count = 1;
    if(countObj != null)
    {
        count = countObj.intValue();
        count++;
    }
    refCount.put(type, count); 
}
显示计数器特定于LinkedList实例

为什么。。。我理解,对于普通对象类中的计数器,您将使用静态计数器。“静态”计数器通常会在对象的所有实例中引用,因此所有增量或减量都会影响同一个计数器

当实例化任何类型的泛型类时,“static”变量绑定到
GenericsClass
的实例,而不是
GenericsClass
。因此在我的例子中,
静态int size
将是LinkedList所有实例中的常见int变量,而不管它是
LinkedList
还是
LinkedList

因此,在使用基于泛型的数据结构时,使用常规实例变量作为计数器是正确的

希望这有帮助


John

应该制作HashMapstatic@TejasP:更正。谢谢。HashMap应该制作好static@TejasP:更正。谢谢
2
4