Java 如何连接两个参数化类型的数组(在guava中)
我在寻找一种简单的方法来迭代两个数组。 由于预计阵列不会很大,我想我可以直接将它们连接起来 不幸的是,调用番石榴看起来很可怕:Java 如何连接两个参数化类型的数组(在guava中),java,arrays,collections,guava,Java,Arrays,Collections,Guava,我在寻找一种简单的方法来迭代两个数组。 由于预计阵列不会很大,我想我可以直接将它们连接起来 不幸的是,调用番石榴看起来很可怕: Class<? extends MyInterface>[] a2 = ... Class<? extends MyInterface>[] a1 = ... ObjectArrays.concat(a1, a2, (Class<Class<? exte
Class<? extends MyInterface>[] a2 = ...
Class<? extends MyInterface>[] a1 = ...
ObjectArrays.concat(a1, a2,
(Class<Class<? extends MyInterface>>) MyInterface.class.getClass());
Class不使用ObjectArrays
您可以组合数组.asList
和Iterables.concat
。这样,您就不需要提供类名
Iterables.concat(Arrays.asList(a1), Arrays.asList(a2))
如果使用静态导入,它的可读性将大大提高:
import static com.google.common.collect.Iterables.concat;
import static java.util.Arrays.asList;
...
concat(asList(a1), asList(a2))
您可以将Arrays.asList
和Iterables.concat
组合起来,而不是使用ObjectArrays
。这样,您就不需要提供类名
Iterables.concat(Arrays.asList(a1), Arrays.asList(a2))
如果使用静态导入,它的可读性将大大提高:
import static com.google.common.collect.Iterables.concat;
import static java.util.Arrays.asList;
...
concat(asList(a1), asList(a2))
最后我写了一些自己的东西
有一种主要方法可以完成所有工作:
@SuppressWarnings("unchecked")
private static <T> T[] mergeInternal(@Nonnull T[] first,
@Nonnull T[] second,
@Nullable T[] third,
@Nullable T[] fourth,
@Nullable T[] fifth,
@Nullable T[] sixth) {
int overallLength = first.length + second.length;
if (third != null) {
overallLength += third.length;
}
if (fourth != null) {
overallLength += fourth.length;
}
if (fifth != null) {
overallLength += fifth.length;
}
if (sixth != null) {
overallLength += sixth.length;
}
Object[] joinedArray = (Object[]) Array.newInstance(first.getClass().getComponentType(), overallLength);
System.arraycopy(first, 0, joinedArray, 0, first.length);
System.arraycopy(second, 0, joinedArray, first.length, second.length);
int copyTargetPosition = first.length + second.length;
if (third != null) {
System.arraycopy(third, 0, joinedArray, copyTargetPosition, third.length);
copyTargetPosition += third.length;
}
if (fourth != null) {
System.arraycopy(fourth, 0, joinedArray, copyTargetPosition, fourth.length);
copyTargetPosition += fourth.length;
}
if (fifth != null) {
System.arraycopy(fifth, 0, joinedArray, copyTargetPosition, fifth.length);
copyTargetPosition += fifth.length;
}
if (sixth != null) {
System.arraycopy(sixth, 0, joinedArray, copyTargetPosition, sixth.length);
}
return (T[]) joinedArray;
}
@SuppressWarnings(“未选中”)
私有静态T[]mergeInternal(@Nonnull T[]first,
@非空T[]秒,
@可为空的T[]第三,
@可为空的T[]第四,
@可为空的T[]第五,
@可为空的T[]第六个){
int overallLength=first.length+second.length;
如果(第三个!=null){
总长度+=第三个长度;
}
如果(第四个!=null){
总长度+=第四长度;
}
如果(第五个!=null){
总长度+=第五长度;
}
如果(第六个!=null){
总长度+=第六长度;
}
Object[]joinedArray=(Object[])Array.newInstance(first.getClass().getComponentType(),总长度);
System.arraycopy(第一个,0,joinedArray,0,第一个.长度);
System.arraycopy(秒,0,joinedArray,first.length,second.length);
int copyTargetPosition=first.length+second.length;
如果(第三个!=null){
System.arraycopy(第三,0,joinedArray,copyTargetPosition,第三,长度);
copyTargetPosition+=第三个长度;
}
如果(第四个!=null){
System.arraycopy(第四,0,联合阵列,copyTargetPosition,第四,长度);
copyTargetPosition+=第四个长度;
}
如果(第五个!=null){
System.arraycopy(第五,0,联合阵列,copyTargetPosition,第五,长度);
copyTargetPosition+=第五个长度;
}
如果(第六个!=null){
System.arraycopy(第六个,0,联合阵列,copyTargetPosition,第六个长度);
}
返回(T[])联合数组;
}
..然后,对于参数(2..6)的每个组合都有一个输入方法,如下所示:
public static <T> T[] merge(@Nonnull T[] first, @Nonnull T[] second) {
Preconditions.checkNotNull(first);
Preconditions.checkNotNull(second);
return mergeInternal(first, second, null, null, null, null);
}
public static <T> T[] merge(@Nonnull T[] first, @Nonnull T[] second, @Nonnull T[] third)
...
public static <T> T[] merge(@Nonnull T[] first, @Nonnull T[] second, @Nonnull T[] third, @Nonnull T[] fourth)
publicstatict[]合并(@Nonnull T[]第一,@Nonnull T[]第二){
先决条件。checkNotNull(第一个);
先决条件。checkNotNull(第二个);
返回mergeInternal(第一、第二、null、null、null、null);
}
公共静态T[]合并(@Nonnull T[]第一,@Nonnull T[]第二,@Nonnull T[]第三)
...
公共静态T[]合并(@Nonnull T[]第一,@Nonnull T[]第二,@Nonnull T[]第三,@Nonnull T[]第四)
等等
我认为一个人很少需要合并6个以上的数组,如果你需要,你可以很容易地扩展给定的想法。我最后写了一些我自己的东西
有一种主要方法可以完成所有工作:
@SuppressWarnings("unchecked")
private static <T> T[] mergeInternal(@Nonnull T[] first,
@Nonnull T[] second,
@Nullable T[] third,
@Nullable T[] fourth,
@Nullable T[] fifth,
@Nullable T[] sixth) {
int overallLength = first.length + second.length;
if (third != null) {
overallLength += third.length;
}
if (fourth != null) {
overallLength += fourth.length;
}
if (fifth != null) {
overallLength += fifth.length;
}
if (sixth != null) {
overallLength += sixth.length;
}
Object[] joinedArray = (Object[]) Array.newInstance(first.getClass().getComponentType(), overallLength);
System.arraycopy(first, 0, joinedArray, 0, first.length);
System.arraycopy(second, 0, joinedArray, first.length, second.length);
int copyTargetPosition = first.length + second.length;
if (third != null) {
System.arraycopy(third, 0, joinedArray, copyTargetPosition, third.length);
copyTargetPosition += third.length;
}
if (fourth != null) {
System.arraycopy(fourth, 0, joinedArray, copyTargetPosition, fourth.length);
copyTargetPosition += fourth.length;
}
if (fifth != null) {
System.arraycopy(fifth, 0, joinedArray, copyTargetPosition, fifth.length);
copyTargetPosition += fifth.length;
}
if (sixth != null) {
System.arraycopy(sixth, 0, joinedArray, copyTargetPosition, sixth.length);
}
return (T[]) joinedArray;
}
@SuppressWarnings(“未选中”)
私有静态T[]mergeInternal(@Nonnull T[]first,
@非空T[]秒,
@可为空的T[]第三,
@可为空的T[]第四,
@可为空的T[]第五,
@可为空的T[]第六个){
int overallLength=first.length+second.length;
如果(第三个!=null){
总长度+=第三个长度;
}
如果(第四个!=null){
总长度+=第四长度;
}
如果(第五个!=null){
总长度+=第五长度;
}
如果(第六个!=null){
总长度+=第六长度;
}
Object[]joinedArray=(Object[])Array.newInstance(first.getClass().getComponentType(),总长度);
System.arraycopy(第一个,0,joinedArray,0,第一个.长度);
System.arraycopy(秒,0,joinedArray,first.length,second.length);
int copyTargetPosition=first.length+second.length;
如果(第三个!=null){
System.arraycopy(第三,0,joinedArray,copyTargetPosition,第三,长度);
copyTargetPosition+=第三个长度;
}
如果(第四个!=null){
System.arraycopy(第四,0,联合阵列,copyTargetPosition,第四,长度);
copyTargetPosition+=第四个长度;
}
如果(第五个!=null){
System.arraycopy(第五,0,联合阵列,copyTargetPosition,第五,长度);
copyTargetPosition+=第五个长度;
}
如果(第六个!=null){
System.arraycopy(第六个,0,联合阵列,copyTargetPosition,第六个长度);
}
返回(T[])联合数组;
}
..然后,对于参数(2..6)的每个组合都有一个输入方法,如下所示:
public static <T> T[] merge(@Nonnull T[] first, @Nonnull T[] second) {
Preconditions.checkNotNull(first);
Preconditions.checkNotNull(second);
return mergeInternal(first, second, null, null, null, null);
}
public static <T> T[] merge(@Nonnull T[] first, @Nonnull T[] second, @Nonnull T[] third)
...
public static <T> T[] merge(@Nonnull T[] first, @Nonnull T[] second, @Nonnull T[] third, @Nonnull T[] fourth)
publicstatict[]合并(@Nonnull T[]第一,@Nonnull T[]第二){
先决条件。checkNotNull(第一个);
先决条件。checkNotNull(第二个);
返回mergeInternal(第一、第二、null、null、null、null);
}
公共静态T[]合并(@Nonnull T[]第一,@Nonnull T[]第二,@Nonnull T[]第三)
...
公共静态T[]合并(@Nonnull T[]第一,@Nonnull T[]第二,@Nonnull T[]第三,@Nonnull T[]第四)
等等
我认为一个人很少需要合并6个以上的数组,如果需要,您可以轻松地扩展给定的想法。为什么首先要使用数组?数组和泛型总是相处不好。更喜欢集合。是的,特别是在注释中。你首先为什么要使用数组?数组和泛型总是相处不好。喜欢收藏。是的,特别是在注释方面。我完全支持静态导入。不过还是有点令人失望--commons ArrayUtils.addAll()会更好,但我不会为了一个实用性而添加它。很好的尝试,我完全支持sta