Java 两种类型的Arraylist:如何确定第一种类型?

Java 两种类型的Arraylist:如何确定第一种类型?,java,arraylist,Java,Arraylist,我是ArrayList的新手,想问一下,我如何知道传入参数数组的a是否属于哪种类型(String、Int、Double…),以及我如何在以下方法中处理 public static <T> Pair<T, Integer> mode(T items[]) 或 在方法mode()中,如何确定第一种类型是否为Integer、Double和String 公共类对{ 私人X优先; 二等兵; 公共对(X,Y,Y){ 第一个=x; 这个秒=y; } 公共X getFirst(){

我是ArrayList的新手,想问一下,我如何知道传入参数数组的a是否属于哪种类型(String、Int、Double…),以及我如何在以下方法中处理

public static <T> Pair<T, Integer> mode(T items[]) 

在方法
mode()
中,如何确定第一种类型是否为Integer、Double和String

公共类对{
私人X优先;
二等兵;
公共对(X,Y,Y){
第一个=x;
这个秒=y;
}
公共X getFirst(){
先把这个还给我;
}
公共Y getSecond(){
把这个还给我;
}
公共布尔等于(对象o){
如果(!(对的o实例)){
返回false;
}
对p=(对)o;
返回
这个。第一。等于(p。第一)&&
这个秒等于(p秒);
}
公共字符串toString(){
返回String.format((%s,%s)”,第一个,第二个);
}
}
导入java.util.ArrayList;
公共课模式{
公共静态对模式(T项[])
{
返回
}
}

您可以尝试instanceOf来检查对象的类型。对于泛型,您可以使用Integer、Double对象并对此进行检查。。只是一个黑客:)快乐的编码。。 编辑:附加我的一个代码供您参考

if (objectVal instanceof Double) {
                errors.add(CommonValidatorUtil.isNegative(field.getName(), (Double) field.get(obj)));
            } else if (objectVal instanceof Long) {
                errors.add(CommonValidatorUtil.isNegative(field.getName(), (Long) field.get(obj)));
            } else if (objectVal instanceof String) {
                errors.add(CommonValidatorUtil.isNegative(field.getName(), Double.valueOf((String) field.get(obj))));
            } else if (objectVal instanceof Integer) {
                errors.add(CommonValidatorUtil.isNegative(field.getName(), (Integer) field.get(obj)));
            }

要实现这一点,您实际上不需要知道数组元素的类型。请注意,您只需要计算每个元素出现的次数,而不管它们是什么类型

这是使用泛型的优点之一:您可以创建可以透明地与许多不同类型一起使用的代码

类似这样的方法会奏效:

public static <T> Pair<T, Integer> mode(T items[]) {
    // If the array is empty, return a dummy pair object
    if (items.length == 0) {
        return new Pair(null, 0);
    }

    // Create a map to store the elements count
    Map<T, Integer> countFromItem = new HashMap<>();
    // For each item in the array
    for (T item : items) {
        // Get the current count
        Integer count = countFromItem.get(item);
        // If there is no current count
        if (count == null) {
            // Set the count to 0
            count = 0;
        }
        // Add 1 to the item current count
        countFromItem.put(item, count + 1);
    }

    // After we found correct count for each element
    T mode =  null;
    int maxCount = 0;
    // Go through each entry (element: count) in the map
    for (Map.Entry<T, Integer> entry : countFromItem.entrySet()) {
        // If the this entry count is greater than the greatest count until now
        if (entry.getValue() > maxCount) {
            // This entry element is the mode
            mode = entry.getKey();
            // This entry count is the maxCount
            maxCount = entry.getValue();
        }
    }
    return new Pair(mode, maxCount);
}
公共静态对模式(T项[]){
//如果数组为空,则返回一个伪对对象
如果(items.length==0){
返回新对(空,0);
}
//创建一个映射来存储元素计数
Map countFromItem=新HashMap();
//对于数组中的每个项
对于(T项:项){
//获取当前计数
整数计数=countFromItem.get(item);
//如果没有当前计数
如果(计数=null){
//将计数设置为0
计数=0;
}
//将1添加到项目当前计数
countFromItem.put(项目,计数+1);
}
//在我们找到每个元素的正确计数之后
T模式=空;
int maxCount=0;
//浏览地图中的每个条目(元素:count)
对于(Map.Entry:countFromItem.entrySet()){
//如果此条目计数大于目前为止的最大计数
if(entry.getValue()>maxCount){
//此条目元素是模式
mode=entry.getKey();
//此项计数为最大计数
maxCount=entry.getValue();
}
}
返回新对(模式,最大计数);
}

我尝试了smth like(items[]instanceof String),但有些地方不对劲尝试(items[0]instanceof String)我理解您的做法,但我已经在没有映射的情况下完成了smth like。谢谢
public class Pair<X,Y>{
  private X first;
  private Y second;

  public Pair(X x, Y y){
    this.first = x;
    this.second = y;
  }

  public X getFirst(){
    return this.first;
  }
  public Y getSecond(){
    return this.second;
  }

  public boolean equals(Object o){
    if(!(o instanceof Pair)){
      return false;
    }
    Pair p = (Pair) o;
    return
      this.first.equals(p.first) &&
      this.second.equals(p.second);
  }

  public String toString(){
    return String.format("(%s,%s)",first,second);
  }

}

import java.util.ArrayList;

public class Mode {

    public static <T> Pair<T, Integer> mode(T items[])
    {
        return 
    }



}
if (objectVal instanceof Double) {
                errors.add(CommonValidatorUtil.isNegative(field.getName(), (Double) field.get(obj)));
            } else if (objectVal instanceof Long) {
                errors.add(CommonValidatorUtil.isNegative(field.getName(), (Long) field.get(obj)));
            } else if (objectVal instanceof String) {
                errors.add(CommonValidatorUtil.isNegative(field.getName(), Double.valueOf((String) field.get(obj))));
            } else if (objectVal instanceof Integer) {
                errors.add(CommonValidatorUtil.isNegative(field.getName(), (Integer) field.get(obj)));
            }
public static <T> Pair<T, Integer> mode(T items[]) {
    // If the array is empty, return a dummy pair object
    if (items.length == 0) {
        return new Pair(null, 0);
    }

    // Create a map to store the elements count
    Map<T, Integer> countFromItem = new HashMap<>();
    // For each item in the array
    for (T item : items) {
        // Get the current count
        Integer count = countFromItem.get(item);
        // If there is no current count
        if (count == null) {
            // Set the count to 0
            count = 0;
        }
        // Add 1 to the item current count
        countFromItem.put(item, count + 1);
    }

    // After we found correct count for each element
    T mode =  null;
    int maxCount = 0;
    // Go through each entry (element: count) in the map
    for (Map.Entry<T, Integer> entry : countFromItem.entrySet()) {
        // If the this entry count is greater than the greatest count until now
        if (entry.getValue() > maxCount) {
            // This entry element is the mode
            mode = entry.getKey();
            // This entry count is the maxCount
            maxCount = entry.getValue();
        }
    }
    return new Pair(mode, maxCount);
}