我们如何访问多维数组

时间:2015-06-05 17:56:20

标签: java

我们如何在不知情的情况下访问多维数组! 例如:

Object[][] inputs = { new Object[] { "a", "ab", "abc" },
            new Object[] { "abc", "ab", "a" },
            new Object[] { "big", new Object[] { "ab", "a", }, "t" },
            new Object[] { "big", new Object[] { "ab", "a", new Object[] { "superbig", "a" } }, "tiny" },
            new Object[] { 123, 23123123, "a" },
            new Object[] { null, new Object[] {}, "a" } };

我认为它是二维的,但它不是2(我对java的语法不满意),我需要获取输入数组对象的所有信息。 我怎么想解决这个问题?有没有将多维数组转换为一维的方法?

P.S。:输出应该是{abc,abc,big,superbig,23123123,a}(它是对象每行的最大字符串!)

2 个答案:

答案 0 :(得分:3)

您可以在运行时使用反射轻松地将任意维数组展平为单维。您可以使用它来计算查找所需的最长字符串。

使用可运行的示例程序:

import java.lang.reflect.*;
import java.util.*;

public class Program {

    private static Object[] toSingleDimension(Object array) {
        ArrayList<Object> arrayList = new ArrayList<>();
        toSingleDimensionRecursive(arrayList, array);
        return arrayList.toArray();
    }

    private static void toSingleDimensionRecursive(ArrayList<Object> output, Object object) {
        if (object == null) {
            output.add(null);
        } else if (!object.getClass().isArray()) {
            output.add(object);
        } else {
            int length = Array.getLength(object);
            for (int i = 0; i < length; ++i) {
                Object value = Array.get(object, i);
                toSingleDimensionRecursive(output, value);
            }
        }
    }

    private static String findLongestString(Object[] array) {
        if (array == null) {
            return null;
        }
        String result = null;
        for (Object object : array) {
            if (object != null) {
                String text = object.toString();
                if (result == null) {
                    result = text;
                } else if (text != null && text.length() > result.length()) {
                    result = text;
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {
        Object[][] inputs = {new Object[]{"a", "ab", "abc"},
            new Object[]{"abc", "ab", "a"},
            new Object[]{"big", new Object[]{"ab", "a",}, "t"},
            new Object[]{"big", new Object[]{"ab", "a", new Object[]{"superbig", "a"}}, "tiny"},
            new Object[]{123, 23123123, "a"},
            new Object[]{null, new Object[]{}, "a"}};

//      Object[] data = toSingleDimension(inputs);
//      System.out.println(Arrays.asList(data));

        ArrayList<String> longestStrings = new ArrayList<>();
        for (Object input : inputs) {
            Object[] array = toSingleDimension(input);
            String longest = findLongestString(array);
            if (longest != null) {
                longestStrings.add(longest);
            }
        }
        System.out.println("Longest strings of arrays: " + longestStrings);
    }
}

输出:

Longest strings of arrays: [abc, abc, big, superbig, 23123123, a]

答案 1 :(得分:2)

您可以使用递归来轻松实现此目的。

public List<Object> convert(Object input) {
    List<Object> objectList = new ArrayList<>();
    if (input instanceof Object[]) {
        for (Object object : (Object[]) input) {
            objectList.addAll(convert(object)); // Instead of addAll here, just add max element. I am a little too lazy to implement that...:)
        }
    } else {
        objectList.add(input);
    }
    return objectList;
}