【问题标题】:How do you split a string into a 2d array and access each grouping based on row?如何将字符串拆分为二维数组并根据行访问每个分组?
【发布时间】:2012-04-12 16:37:05
【问题描述】:

我想知道如何以与下面实例化一维数组相同的方式自动分隔字符串并将其分配给二维数组的特定行:

public class TestArray {
  public static void main (String [] args) {

    String file1description= "We went to the mall yesterday.";
    String[] file1tags;
    String delimiter= " ";
    file1tags = file1description.split (delimiter);
    for (int i = 0; i < file1tags.length; i++) {
      System.out.println (file1tags[i]);
    }
  }
}

如果有更简单的方法请分享。如您所知,我是一个新手,但我愿意学习。在此示例中,每个单词由分隔符分隔并自动存储到“file1tags”数组中。如何使用 2d 数组执行此操作,以便可以调用具有相同功能的多个数组?提前致谢!

【问题讨论】:

    标签: java arrays string split


    【解决方案1】:
    public static String [][] to2dim (String source, String outerdelim, String innerdelim) {
    
        String [][] result = new String [source.replaceAll ("[^" + outerdelim + "]", "").length () + 1][]; 
        int count = 0;
        for (String line : source.split ("[" + outerdelim + "]"))
        {
            result [count++] = line.split (innerdelim);
        }
        return result;
    }
    
    public static void show (String [][] arr)
    {
        for (String [] ar : arr) {
            for (String a: ar) 
                System.out.print (" " + a);
            System.out.println ();
        }
    }   
    
    public static void main (String args[])
    {
        show (to2dim ("a b c \n d e f \n g h i", "\n", " "));
    }
    

    新手友好:

    public static String [][] to2dim (String source, String outerdelim, String innerdelim) {
        // outerdelim may be a group of characters
        String [] sOuter = source.split ("[" + outerdelim + "]"); 
        int size = sOuter.length;
        // one dimension of the array has to be known on declaration:
        String [][] result = new String [size][]; 
        int count = 0;
        for (String line : sOuter)
        {
            result [count] = line.split (innerdelim);
            ++count;
        }
        return result;
    }
    

    【讨论】:

    • 非常感谢,但是有新手版本吗?
    • @user1299661:是的,现在有,而且有更正的原件。例如,“[ \t]”是空白或制表符的正则表达式 - 一组可能的分隔符可以这样定义。
    • 非常感谢您的时间和耐心!非常感谢!
    • 我无法编译您的代码。发现 3 个错误:错误:令牌上的语法错误,应改为 ClassHeader 错误:令牌“;”上的语法错误,{ 此令牌后预期错误:语法错误,插入“}”完成ClassBody
    • 嗯,你必须提供一个类头(我建议:public class To2DimStringArray {),在此之前一个import java.util.*;,如果你使用所谓的新手代码,主要和显示方法,以及类的右大括号。
    【解决方案2】:

    这是一个简单的例子

    
    String example = "a b c \n d e f \n g h i";
    String[] rows = example.split("\n");
    for (int i = 0; i < rows.length; i++) {
      String[] columns = rows[i].split(" ");
      for (int j = 0; j < columns.length; j++) {
        //do something
      }
    } 
    

    【讨论】:

    • 对不起,它被我的不足绊倒了!不知道我必须对它们进行 html 编码
    • 变量行在哪里被实例化了?
    • sry 在我之前的编辑中我将行更改为行,但我没有在任何地方进行更改
    • 非常感谢您的时间和耐心!
    【解决方案3】:
    String example = "a b c \n d e f \n g h i";
    String[] rows = example.split("\n");
    String[][] table;
    for (int i = 0; i < rows.length; i++) {
      table[i] = rows[i].split(" ");
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-25
      • 2016-10-14
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多