我们如何访问多维数组

时间:2022-04-22 21:35:44

How can we access to a multiple dimension array without even knowing it ! For example :

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

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" } };

I thought it was 2 dimensional but it's not 2 (which i'm not happy with that syntax of java) and i need to get all the information of the inputs array object. How am i suppose to solve this ? Is there any method for transforming a multidimensional array into one dimension ?

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

P.S.: The Output should be { abc, abc, big,superbig,23123123,a} (it's the biggest string of each line of the object !)

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

2 个解决方案

#1


3  

You can easily flatten any-dimensional array to single dimension, at runtime, using reflection. You can use it to compute find longest strings as you required with your edit.

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

Working runnable example program:

工作可运行的示例程序:

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);
    }
}

Outputs:

输出:

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

#2


2  

You can use recursion to achieve this easily.

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

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;
}

#1


3  

You can easily flatten any-dimensional array to single dimension, at runtime, using reflection. You can use it to compute find longest strings as you required with your edit.

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

Working runnable example program:

工作可运行的示例程序:

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);
    }
}

Outputs:

输出:

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

#2


2  

You can use recursion to achieve this easily.

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

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;
}