首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将字符串转换为不同长度的多维数组

将字符串转换为不同长度的多维数组
EN

Stack Overflow用户
提问于 2021-01-08 12:52:50
回答 4查看 81关注 0票数 0

我需要通过命令行执行一段代码,该代码将提供一个多维数组,其中的元素长度不一定相等。

执行字符串如下:

代码语言:javascript
复制
start /wait java -jar testMSMWithIndex.jar Foursquare_weather_day_root-type_type 0,1,2-4

我正在考虑传递参数0,1,2-4,然后将其转换为多维数组,在本例中具有不同长度的元素,即{{0}, {1}, {2, 4}}

请注意,{{0, null}, {1, null}, {2, 4}}不能解决我的问题。

你们知道如何开发一种方法,或者直接从args中获取数组吗?

我真的很感谢你能提供的任何帮助。

EN

回答 4

Stack Overflow用户

发布于 2021-01-08 13:52:07

我怀疑是否已经有任何东西可以为您做这件事,所以您必须自己解析这个字符串。类似下面这样的代码就可以做到:

代码语言:javascript
复制
public static int[][] parseRaggedArrayFromString(String s)
        throws NumberFormatException {
    String[] ss = s.split(",");
    int[][] result = new int[ss.length][];
    for (int i = 0; i < ss.length; ++i) {
        if (!ss[i].contains("-")) {
            result[i] = new int[1];
            result[i][0] = Integer.parseInt(ss[i]);
        } else {
            String[] range = ss[i].split("-", 2);
            int lo = Integer.parseInt(range[0]);
            int hi = Integer.parseInt(range[1]);
            int size = hi - lo + 1;
            result[i] = new int[size > 0 ? size : 1];
            int j = 0;
            do {
                result[i][j] = lo;
                ++lo;
                ++j;
            } while (lo <= hi);
        }
    }
    return result;
}
票数 2
EN

Stack Overflow用户

发布于 2021-01-08 14:17:13

这基本上是,-的分裂。从那里开始,只是处理数据。代码中的注释。

代码语言:javascript
复制
/**
 * @author sedj601
 */
public class Main {
    public static void main(String[] args) {
        String input = "0,1,2-3";

        String[] firstArray = input.split(",");//Split on ,.
        String[][] outputArray = new String[firstArray.length][];//The array that will be holding the output

        //Used to process the firstArray
        for (int i = 0; i < firstArray.length; i++) {
            if (firstArray[i].length() > 1) {//If the lenght is greater than one. split on -.
                String[] secondArray = firstArray[i].split("-");
                //Subtract the two numbers and add one to get the lenght of the array that will hold these values
                int arrayLength = Integer.parseInt(secondArray[1]) - Integer.parseInt(secondArray[0]) + 1;
                String[] tempArray = new String[arrayLength];
                int increment = 0;//Keeps up with the tempArray index.
                //loop from the first number to the last number inclusively.
                for (int t = Integer.parseInt(secondArray[0]); t <= Integer.parseInt(secondArray[1]); t++) {
                    tempArray[increment++] = Integer.toString(t);//Add the data to the array.
                }
                outputArray[i] = tempArray;//Add the array to the output array.
            } else {//If the lenght is 1, creat an array and add the current data.
                String[] tempArray = new String[1];
                tempArray[0] = firstArray[i];
                outputArray[i] = tempArray;
            }
        }

        //Print the output.
        for (String[] x : outputArray) {
            for (String y : x) {
                System.out.print(y + " ");
            }
            System.out.println();
        }
    }
}

输出:

代码语言:javascript
复制
--- exec-maven-plugin:1.5.0:exec (default-cli) @ JavaTestingGround ---
0 
1 
2 3 
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time:  1.194 s
Finished at: 2021-01-08T00:08:15-06:00
------------------------------------------------------------------------
票数 1
EN

Stack Overflow用户

发布于 2021-01-08 13:16:53

我真的认为在创建Object类型的数组(不是一个好主意)时,这是可能的,因为多维数组只能保存相同长度的数组(int[][])。然后从数组中创建和检索值。我在这里试着变得有创意,并接受你的要求..

代码语言:javascript
复制
public class x {
  public static void main(String[] args) {
    Object[] arguments = new Object[args.length];
// Then make a loop to capture arguments in array..
// or add manually
    arguments[0] = new String[]{args[0]};
    arguments[1] = new String[]{args[1],args[2]};
//Then retrieve info from object later by casting
    System.out.println(java.util.Arrays.toString((String[]) arguments[1]));
  }
}
...

不过,请考虑使用集合...

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65623636

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档