java—将字符串转换为具有不同长度的多维数组

kxe2p93d  于 2021-06-27  发布在  Java
关注(0)|答案(4)|浏览(442)

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

  1. start /wait java -jar testMSMWithIndex.jar Foursquare_weather_day_root-type_type 0,1,2-3

我正在考虑传递参数“0,1,2-3”,然后将其转换为具有不同长度元素的多维数组(在本例中为{0}、{1}、{2,3})。
请注意,{0,null},{1,null},{2,3}}对我的问题不起作用。
你们知道如何开发一个方法或者直接从args获取数组吗?
我真的很感激你能提供的任何帮助。

y0u0uwnf

y0u0uwnf1#

很难说已经有什么东西可以帮你做到这一点,所以你必须自己解析这个字符串。像这样的事情可以做到:

  1. public static int[][] parseRaggedArrayFromString(String s) throws NumberFormatException
  2. {
  3. String[] ss = s.split(",");
  4. int[][] result = new int[ss.length][];
  5. for (int i = 0; i < ss.length; ++i) {
  6. if (!ss[i].contains("-")) {
  7. result[i] = new int[1];
  8. result[i][0] = Integer.parseInt(ss[i]);
  9. } else {
  10. String[] range = ss[i].split("-", 2);
  11. int lo = Integer.parseInt(range[0]);
  12. int hi = Integer.parseInt(range[1]);
  13. int size = hi - lo + 1;
  14. result[i] = new int[size > 0 ? size : 1];
  15. int j = 0;
  16. do {
  17. result[i][j] = lo;
  18. ++lo;
  19. ++j;
  20. } while (lo <= hi);
  21. }
  22. }
  23. return result;
  24. }
展开查看全部
g2ieeal7

g2ieeal72#

我真的认为当你创建一个类型为 Object 因为多维数组只能容纳相同长度的数组( int[][] ). 然后通过强制转换从数组中创建和检索值。。。我在这里试着发挥创造力,适应你的要求。。

  1. public class x {
  2. public static void main(String[] args) {
  3. Object[] arguments = new Object[args.length];
  4. // Then make a loop to capture arguments in array..
  5. // or add manually
  6. arguments[0] = new String[]{args[0]};
  7. arguments[1] = new String[]{args[1],args[2]};
  8. //Then retrieve info from object later by casting
  9. System.out.println(java.util.Arrays.toString((String[]) arguments[1]));
  10. }
  11. }
  12. ...

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

iecba09b

iecba09b3#

当我等待答案时,我找到了解决问题的方法。
这里的相关信息是,我们不需要在其示例化中设置第二个数组维度。
代码如下:

  1. // INPUT string = "2-3,1,4-5"
  2. private static String[][] featuresConversion(String string) {
  3. String[] firstLevel = string.split(","); // 1st lvl separator
  4. String[][] features = new String[firstLevel.length][]; // Sets 1st lvl length only
  5. int i = 0;
  6. for (String element : firstLevel) {
  7. features[i++] = element.split("-");
  8. }
  9. return features;
  10. }

我要谢谢大家。所有建议的解决方案也很好!

展开查看全部
e4eetjau

e4eetjau4#

这基本上是一个分裂 , 以及 - . 从那里我们只需要处理数据。代码中的注解。

  1. /**
  2. *
  3. * @author sedj601
  4. */
  5. public class Main
  6. {
  7. public static void main(String[] args)
  8. {
  9. String input = "0,1,2-3";
  10. String[] firstArray = input.split(",");//Split on ,.
  11. String[][] outputArray = new String[firstArray.length][];//The array that will be holding the output
  12. //Used to process the firstArray
  13. for (int i = 0; i < firstArray.length; i++) {
  14. if (firstArray[i].length() > 1) {//If the lenght is greater than one. split on -.
  15. String[] secondArray = firstArray[i].split("-");
  16. int arrayLength = Integer.parseInt(secondArray[1]) - Integer.parseInt(secondArray[0]) + 1;//Subtract the two numbers and add one to get the lenght of the array that will hold these values
  17. String[] tempArray = new String[arrayLength];
  18. int increment = 0;//Keeps up with the tempArray index.
  19. for (int t = Integer.parseInt(secondArray[0]); t <= Integer.parseInt(secondArray[1]); t++) {//loop from the first number to the last number inclusively.
  20. tempArray[increment++] = Integer.toString(t);//Add the data to the array.
  21. }
  22. outputArray[i] = tempArray;//Add the array to the output array.
  23. }
  24. else {//If the lenght is 1, creat an array and add the current data.
  25. String[] tempArray = new String[1];
  26. tempArray[0] = firstArray[i];
  27. outputArray[i] = tempArray;
  28. }
  29. }
  30. //Print the output.
  31. for (String[] x : outputArray) {
  32. for (String y : x) {
  33. System.out.print(y + " ");
  34. }
  35. System.out.println();
  36. }
  37. }
  38. }

输出:

  1. --- exec-maven-plugin:1.5.0:exec (default-cli) @ JavaTestingGround ---
  2. 0
  3. 1
  4. 2 3
  5. ------------------------------------------------------------------------
  6. BUILD SUCCESS
  7. ------------------------------------------------------------------------
  8. Total time: 1.194 s
  9. Finished at: 2021-01-08T00:08:15-06:00
  10. ------------------------------------------------------------------------
展开查看全部

相关问题