Java泛型,将嵌套列表转换为嵌套数组

标签 java arrays list generics multidimensional-array

我必须连接一些使用嵌套数组的旧代码,因此我必须将我的嵌套列表转换为嵌套数组。

我心想,这样的功能应该已经在一些现有的库中实现了。我在 Google/StackOverflow 上进行了搜索,但没有任何结果。我检查了 Apache Commons 和 Guava,但没有找到等效的功能。

这是我的代码,我担心这可以用更简单的方式完成。热烈欢迎任何想法、建议和改进。

@SuppressWarnings("rawtypes")
public static <E> Class getNestedListClass(List<List<E>> list){
    if (list == null) {
        return null;
    }

    for (final List<E> innerList: list) {
        if (innerList == null || innerList.isEmpty()) {
            continue;
        }
        return innerList.get(0).getClass();
    }
    return null;
}

@SuppressWarnings({ "unchecked", "rawtypes" })
public static <E> E[][] convertNestedList(List<List<E>> list){
    final Class<?> clazz = getNestedListClass(list);
    if (clazz == null) {
        return null;
    }
    final E[][] result = (E[][]) Array.newInstance(clazz, list.size(), 0);

    for (int i = 0; i < list.size(); i++) {    
        final List<E> innerList = list.get(i);
        if (innerList == null) {
            continue;
        }
        result[i] = innerList.toArray((E[]) Array.newInstance(clazz, innerList.size()));
    }
    return result;
}


@Test
public void testConvertNestedList() {
    final List<List<String>> list = new ArrayList<List<String>>();
    list.add(new ArrayList<String>());
    list.get(list.size() - 1).add("0.0");
    list.get(list.size() - 1).add("0.1");
    list.get(list.size() - 1).add("0.2");
    list.add(new ArrayList<String>());
    list.get(list.size() - 1).add("1.0");
    list.get(list.size() - 1).add("1.1");
    list.get(list.size() - 1).add("1.2");
    list.get(list.size() - 1).add("1.3");
    list.add(new ArrayList<String>());
    list.get(list.size() - 1).add("2.0");
    list.get(list.size() - 1).add("2.1");

    final String[][] array = convertNestedList(list);

    final String[][] expected = new String[][] { 
            { "0.0", "0.1", "0.2" }, 
            { "1.0", "1.1", "1.2", "1.3" },
            { "2.0", "2.1" } };
    assertArrayEquals(expected, array);

}

EDIT 添加了一些测试用例:

@Test
public void testConvertNestedListCaseNull() throws NetLineMarketException {
    List<List<Object>> list = null;
    Object[][] array = ArrayUtils.convertNestedList(list);
    Object[][] expected = null;
    assertArrayEquals(expected, array);
}

@Test
public void testConvertNestedListCaseNullElement() throws NetLineMarketException {
    List<List<Object>> list = new ArrayList<List<Object>>();
    list.add(null);
    Object[][] array = ArrayUtils.convertNestedList(list);
    Object[][] expected = new Object[][] { null };
    assertArrayEquals(expected, array);
}

@Test
public void testConvertNestedListCaseNullElement2() throws NetLineMarketException {
    List<List<Object>> list = new ArrayList<List<Object>>();
    list.add(new ArrayList<Object>());
    list.get(0).add(null);
    Object[][] array = ArrayUtils.convertNestedList(list);
    Object[][] expected = new Object[][] { { null } };
    assertArrayEquals(expected, array);
}

@Test
public void testConvertNestedListCaseEmpty() throws NetLineMarketException {
    List<List<Object>> list = new ArrayList<List<Object>>();
    Object[][] array = ArrayUtils.convertNestedList(list);
    Object[][] expected = new Object[][] { };
    assertArrayEquals(expected, array);
}

@Test
public void testConvertNestedListCaseString() throws NetLineMarketException {
    final String string = "string";
    List<List<Object>> list = new ArrayList<List<Object>>();
    list.add(new ArrayList<Object>());
    list.get(0).add(string);
    Object[][] array = ArrayUtils.convertNestedList(list);
    Object[][] expected = new Object[][] { { string } };
    assertArrayEquals(expected, array);
}

@Test(expected=MyException.class)
public void testConvertNestedListCaseMix() throws NetLineMarketException {
    final String string = "string";
    List<List<Object>> list = new ArrayList<List<Object>>();
    list.add(new ArrayList<Object>());
    list.get(0).add(string);
    list.get(0).add(Boolean.TRUE);
    Object[][] array = ArrayUtils.convertNestedList(list);
}

最佳答案

您的代码存在一些问题。例如:

  • List<Object>(null)转换为 null而不是 Object[] { null }
  • List<Object>("string")转换为 String[] { "string" }而不是 Object[] { "string" }
  • List<Object>("string", Boolean.TRUE)产生运行时错误。

我的建议:

public static <T> T[][] toArray(List<List<T>> list, Class<T> clazz) {
    if (list == null) {
        return null;
    }
    List<T[]> temp = new ArrayList<>();
    @SuppressWarnings("unchecked")
    T[] inner = (T[]) Array.newInstance(clazz, 0);
    for (List<T> sublist : list) {
        temp.add(sublist == null ? null : sublist.toArray(inner));
    }
    @SuppressWarnings("unchecked")
    T[][] outer = (T[][]) Array.newInstance(inner.getClass(), temp.size());
    return temp.toArray(outer);
}

关于Java泛型,将嵌套列表转换为嵌套数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10794108/

相关文章:

java - 数组的 Jackson-CSV 架构

java - 将 1 个数组列表分配给其他数组与将 1 个数组列表添加到另一个空数组列表以使它们都包含相同的对象之间有什么区别

Java - PaintComponent 导致我的程序变慢

java - 在 JPA 中链接不同实体类型的最佳方法

arrays - 无法使用类型为 'append' 的参数列表调用 '(T)'

java - 如何使用Java将整数拆分为数组?

c++ - 从 char 到 const* char 的无效转换,代码有什么问题?

python - 将列表转换为字符串列表

list - 如何将元组添加到 List 并避免 java.lang.UnsupportedOperationException

c++ - 按 STL - C++ 中的成绩对学生列表进行排序?