简体   繁体   English

java泛型编译错误,堆栈上的泛型类

[英]java generics compile error, generic class on the stack

I'm not sure why this doesn't work in Java: 我不确定为什么这在Java中不起作用:

import java.util.Map;

public class FreqCounter<T,R> {
    private Map<T, Integer> hist;
    private R item;
    public FreqCounter (final R item_) {
        item = item_;
    }
    public T getMostFrequentElement() {
        T most_frequent_element = T();
        Integer highestcount = 0;
        for(T t : item) {
            Integer count = hist.get(t);
            if(count == null) {
                hist.put(t, 1);
            }
            else {
                hist.put(t, count + 1);
            }
            if(count + 1 > highestcount) {
                most_frequent_element = t;
                highestcount = count + 1;
            }
        }
        return most_frequent_element;
    }
}


class HelloWorld {
    public static void main(String[] args) {
        String s = "aaabbcccc";
        FreqCounter<Character, Integer> counter = new FreqCounter<Character, Integer>(s);
    }
}

Problem lines: 问题线:

 1. T most_frequent_element = T();
 2. for(T t : item)
 3. FreqCounter<Character, Integer> counter = new FreqCounter<Character, Integer>(s);
  1. Cannot find symbol: method T() 找不到符号:方法T()
  2. required: array or java.lang.Iterable, found: R required:array或java.lang.Iterable,found:R
  3. Required java.lang.Integer Found: java.lang.String reason: actual argument java.lang.String cannot be converted to java.lang.Integer by method invocation conversion 需要java.lang.Integer发现:java.lang.String原因:实际参数java.lang.String无法通过方法调用转换转换为java.lang.Integer

What I was trying to do was make a class that could count how many times an element in an iterable container shows up. 我试图做的是创建一个可以计算可迭代容器中元素出现次数的类。 Originally I just wanted to make it to count characters in a string but I thought I could make it more general. 最初我只是想让它计算一个字符串中的字符,但我想我可以使它更通用。 I think some of this would work in C++? 我认为其中一些可以在C ++中使用?

Also, does FreqCounter<Character, Integer> counter = new FreqCounter<Character, Integer>(s) ; 另外, FreqCounter<Character, Integer> counter = new FreqCounter<Character, Integer>(s) ; need to be "newed" as opposed to declared on the stack? 需要被“新化”而不是在堆栈上声明?

T is a Generic type, not a real one, and one of the limitations of generics is that you cannot instantiate a new one (which is what I think you were trying to do here). T是泛型类型,而不是真实类型,泛型的一个限制是你不能实例化一个新的(这是我认为你在这里尝试做的)。

What you can do though is assign, call methods in, keep references too, duplicate references too, etc. 你可以做的是分配,调用方法,保留引用,重复引用等。

What you probably actually wanted to do was pull the set of T s out of the keySet of the Map . 你可能真正想要做的是从MapkeySet中拉出一组T s。

T t = null;
int count = 0;
for (Entry<T, Integer> e: hist.entrySet()) {
   if (e.getValue() > count) {
      count = e.getValue();
      t = e.getKey();
   }
}
return t;

Java Generics provide a lot of the same functionality that C++ templates do, but they work in quite a different way. Java Generics提供了许多与C ++模板相同的功能,但它们以完全不同的方式工作。 Quite apart from anything else you only have one ArrayList class no matter how many different ways you instantiate one. 除了其他任何东西,你只有一个ArrayList类,无论你实例化多少种不同的方法。 The generics are used for compiler time type checking and then erased and are not present at all during run time. 泛型用于编译器时间类型检查,然后擦除,并且在运行时根本不存在。

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

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