2014-02-13 59 views
0

我试着写了下面的方法:如何判断数组中的项目是否也是数组?

public static long[] deepDoubleToLong(double... original) 
{ 
    long[] ret = new long[original.length]; 
    for(int i = 0; i < ret.length; i++) 
     if (original[i] instanceof double[]) 
      ret[i] = deepDoubleToLong((double[])original[i]); 
     else 
      ret[i] = (long)original[i]; 
    return ret; 
} 

位我得到这个编译错误:

Uncompilable source code - unexpected type 
    required: reference 
    found: double 
    at ArrayConversion.deepDoubleToLong(ArrayConversion.java:5) 

怎么回事,如果不是这样,我可以告诉我们,如果一个项目是一个数组?

+3

它会是一个数组? 'original'是一个'double []'。 –

+0

@SotiriosDelimanolis这是问题的另一部分...:/ – Supuhstar

+0

@Supuhstar:它的写法,元素不可能是一个数组。 –

回答

0

我怀疑你正在寻找我的Rebox类。

这个问题(正如开头的注释所描述的那样)出现在您调用带有数组参数的可变参数方法时。这会导致数组被封装并在第一个参数中显示为一个基元数组 - 或类似的东西。

无论如何 - 使用它 - 它做你所需要的。

/** 
* Can rebox a boxed primitive array into its Object form. 
* 
* Generally I HATE using instanceof because using it is usually 
* an indication that your hierarchy is completely wrong. 
* 
* Reboxing - however - is an area I am ok using it. 
* 
* Generally, if a primitive array is passed to a varargs it 
* is wrapped up as the first and only component of an Object[]. 
* 
* E.g. 
* 
* public void f(T... t) {}; 
* f(new int[]{1,2}); 
* 
* actually ends up calling f with t an Object[1] and t[0] the int[]. 
* 
* This unwraps it and returns the correct reboxed version. 
* 
* In the above example it will return an Integer[]. 
* 
* Any other array types will be returned unchanged. 
* 
* @author OldCurmudgeon 
*/ 
public class Rebox { 
    public static <T> T[] rebox(T[] it) { 
    // Default to return it unchanged. 
    T[] result = it; 
    // Special case length 1 and it[0] is primitive array. 
    if (it.length == 1 && it[0].getClass().isArray()) { 
     // Which primitive array is it? 
     if (it[0] instanceof int[]) { 
     result = rebox((int[]) it[0]); 
     } else if (it[0] instanceof long[]) { 
     result = rebox((long[]) it[0]); 
     } else if (it[0] instanceof float[]) { 
     result = rebox((float[]) it[0]); 
     } else if (it[0] instanceof double[]) { 
     result = rebox((double[]) it[0]); 
     } else if (it[0] instanceof char[]) { 
     result = rebox((char[]) it[0]); 
     } else if (it[0] instanceof byte[]) { 
     result = rebox((byte[]) it[0]); 
     } else if (it[0] instanceof short[]) { 
     result = rebox((short[]) it[0]); 
     } else if (it[0] instanceof boolean[]) { 
     result = rebox((boolean[]) it[0]); 
     } 
    } 
    return result; 
    } 

    // Rebox each one separately. 
    private static <T> T[] rebox(int[] it) { 
    T[] boxed = makeTArray(it.length); 
    for (int i = 0; i < it.length; i++) { 
     boxed[i] = (T) Integer.valueOf(it[i]); 
    } 
    return boxed; 
    } 

    private static <T> T[] rebox(long[] it) { 
    T[] boxed = makeTArray(it.length); 
    for (int i = 0; i < it.length; i++) { 
     boxed[i] = (T) Long.valueOf(it[i]); 
    } 
    return boxed; 
    } 

    private static <T> T[] rebox(float[] it) { 
    T[] boxed = makeTArray(it.length); 
    for (int i = 0; i < it.length; i++) { 
     boxed[i] = (T) Float.valueOf(it[i]); 
    } 
    return boxed; 
    } 

    private static <T> T[] rebox(double[] it) { 
    T[] boxed = makeTArray(it.length); 
    for (int i = 0; i < it.length; i++) { 
     boxed[i] = (T) Double.valueOf(it[i]); 
    } 
    return boxed; 
    } 

    private static <T> T[] rebox(char[] it) { 
    T[] boxed = makeTArray(it.length); 
    for (int i = 0; i < it.length; i++) { 
     boxed[i] = (T) Character.valueOf(it[i]); 
    } 
    return boxed; 
    } 

    private static <T> T[] rebox(byte[] it) { 
    T[] boxed = makeTArray(it.length); 
    for (int i = 0; i < it.length; i++) { 
     boxed[i] = (T) Byte.valueOf(it[i]); 
    } 
    return boxed; 
    } 

    private static <T> T[] rebox(short[] it) { 
    T[] boxed = makeTArray(it.length); 
    for (int i = 0; i < it.length; i++) { 
     boxed[i] = (T) Short.valueOf(it[i]); 
    } 
    return boxed; 
    } 

    private static <T> T[] rebox(boolean[] it) { 
    T[] boxed = makeTArray(it.length); 
    for (int i = 0; i < it.length; i++) { 
     boxed[i] = (T) Boolean.valueOf(it[i]); 
    } 
    return boxed; 
    } 

    // Trick to make a T[] of any length. 
    // Do not pass any parameter for `dummy`. 
    // public because this is potentially re-useable. 
    public static <T> T[] makeTArray(int length, T... dummy) { 
    return Arrays.copyOf(dummy, length); 
    } 
} 

虽然我可能是错的。

使用方法如下:

public StringBuilder add(StringBuilder s, T... values) { 
    // Remember to rebox it in case it's a primitive array. 
    for (T v : Rebox.rebox(values)) { 
    add(s, v); 
    } 
    return s.append(fin()); 
} 

在回答你的问题标题我怎么能知道在数组中的项也是一个数组? - 使用it[i].getClass().isArray()

1

如果您更改参数类型Object... original,用Class#isArray(),像这样:

if (original[i].getClass().isArray()) 
+0

'original [i]'是'double'类型的。 –

0

使用变量参数运算符(...)本身是创建一个阵列的本地您方法(在这种情况下称为“原始”),所以传递给它的任何东西都将成为一个数组。那么你是否打算将多维和单维数组传递给此方法,然后让方法区分这些类型?如果你没有在你的类中声明任何多维数组,那么检查它们将是完全没有必要的。如果你确实需要单击和多重输入,我可能会建议重载方法,并让方法参数自己做分离。 因此,像:

public static long[] doubleToLong(double[][] original, int index){ 
//your conversion logic here that will type cast your second dimension array 
long[] ret = new long[original.length];  
for(int i = 0; i < ret.length; i++) 
    ret[i] = (long)original[index][i]; 
return ret; 
} 

public static long[] doubleToLong(double[] original){ 
//your conversion logic here that type casts a single dimension array 
long[] ret = new long[original.length];  
for(int i = 0; i < ret.length; i++) 
    ret[i] = (long)original[i]; 
return ret; 
} 

已编译对我来说,看它是否确实为你,也测试,以确保它你想要做什么。但方法参数将排序通过哪些数组是单一的,哪些是多维的。

希望它有帮助!快乐的编码!

+0

>“您是否计划将多维和单维数组传递给此方法,然后让方法区分这些类型?”是的,我是,但我希望这可能更通用:允许任何人调用该方法传递一个'n'维数组并将其作为不同类型返回 – Supuhstar

0

这里是为我工作的解决方案:

import java.util.Arrays; 

public class ArrayConversion 
{ 
    public static Object[] deepToDouble(Object[] original) 
    { 
     Object[] ret = new Object[original.length]; 
     for(int i = 0; i < ret.length; i++) 
      if (original[i] instanceof Object[]) 
       ret[i] = deepToDouble((Object[])original[i]); 
      else 
       ret[i] = 
       (
        original[i] instanceof Number 
         ? ((Number)original[i]).doubleValue() 
         : Double.NaN 
       ); 
     return ret; 
    } 

    public static void main(String... args) 
    { 
     Object[] test = new Object[]{1, new Object[]{1, 2, 3}, 3}; 
     System.out.println(Arrays.deepToString(test)); 
     System.out.println(Arrays.deepToString(deepToDouble(new Object[]{1, new Object[]{1, 2, 3}, 3}))); 
    } 
} 

,输出是:

[1, [1, 2, 3], 3] 
[1.0, [1.0, 2.0, 3.0], 3.0] 

我知道它仍然是松散类型为Object,但它现在双打的数组,这是我的最终目标

相关问题