gpt4 book ai didi

java - java中原始类型的泛型方法

转载 作者:行者123 更新时间:2023-11-29 05:14:53 24 4
gpt4 key购买 nike

<分区>

这是一个例子:

import java.util.Collection;

/**
* Created by IDEA on 16/11/14.
*/
public class Size
{
public static int size(Iterable<?> data)
{
if (data instanceof Collection) {
return ((Collection<?>) data).size();
}
int counter = 0;
for (Object i : data) {
counter++;
}
return counter;
}

public static int size(int[] data)
{
return data.length;
}

public static int size(double[] data)
{
return data.length;
}

public static int size(float[] data)
{
return data.length;
}

public static int size(short[] data)
{
return data.length;
}

public static int size(boolean[] data)
{
return data.length;
}

public static int size(char[] data)
{
return data.length;
}

public static <T> int size(T[] data)
{
return data.length;
}
}

size 方法对于所有原始数组都是相同的。有没有办法避免这种冗余?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com