Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用正则表达式将字符串按列解析为列表以模拟Java中的表_Java_Regex_String_List_Parsing - Fatal编程技术网

使用正则表达式将字符串按列解析为列表以模拟Java中的表

使用正则表达式将字符串按列解析为列表以模拟Java中的表,java,regex,string,list,parsing,Java,Regex,String,List,Parsing,我所拥有的: //This is pseudocode so it's not valid new list of list called columnList String rows[]= text.split("\\r?\\n"); for each row in rows: i=0 new splitRow = row split by comma for each element in splitRow: columnList(i

我所拥有的:

 //This is pseudocode so it's not valid

 new list of list called columnList
 String rows[]= text.split("\\r?\\n");
 for each row in rows:
     i=0
     new splitRow = row split by comma
     for each element in splitRow:
         columnList(i).add(element)
         i++
我有一根像这样的线

"a, b, c\nd, e, f\ng, h, i"
每一行代表一个新行,每一个逗号代表一个不同的列

我想做什么:

 //This is pseudocode so it's not valid

 new list of list called columnList
 String rows[]= text.split("\\r?\\n");
 for each row in rows:
     i=0
     new splitRow = row split by comma
     for each element in splitRow:
         columnList(i).add(element)
         i++
我正在尝试将其传递到列表中,使其看起来像这样:

[ [a,d,g], [b,e,h], [c,f,i] ]
a   b   c
d   e   f
g   h   i
这样它就模仿了一张桌子,一张看起来像这样的桌子:

[ [a,d,g], [b,e,h], [c,f,i] ]
a   b   c
d   e   f
g   h   i
换句话说,我正在为每一列列做一个列表

我知道怎么做:

 //This is pseudocode so it's not valid

 new list of list called columnList
 String rows[]= text.split("\\r?\\n");
 for each row in rows:
     i=0
     new splitRow = row split by comma
     for each element in splitRow:
         columnList(i).add(element)
         i++
我知道如果我列出行,我可以这样做:

String rows[]= text.split("\\r?\\n");
并以一组行结束:

{"a, b, c", "d, e, f", "g, h, i"}
然后将每个字符串格式化为一个列表,但是如何使用带有1或没有循环的正则表达式来实现上面所述的目标呢?我正在尝试
O(n)
O(nlogn)
算法,其中
n
是行数。谢谢:)

我目前正在做的事情:

 //This is pseudocode so it's not valid

 new list of list called columnList
 String rows[]= text.split("\\r?\\n");
 for each row in rows:
     i=0
     new splitRow = row split by comma
     for each element in splitRow:
         columnList(i).add(element)
         i++
这是我提出的算法,但它非常慢,需要嵌套循环。我认为这不是很有效

更多信息:

 //This is pseudocode so it's not valid

 new list of list called columnList
 String rows[]= text.split("\\r?\\n");
 for each row in rows:
     i=0
     new splitRow = row split by comma
     for each element in splitRow:
         columnList(i).add(element)
         i++
我用Java来做这些

我不能使用任何哈希结构,所以没有字典


很抱歉,如果我的格式奇怪地正式,我过去在清楚地表达我的问题时遇到了问题。

我认为应该采取以下步骤:

  • 替换原始字符串中的“\n”
  • 将“”拆分为字符串[]
  • 将一维数组转换为二维数组
  • 有些东西看起来像:

            String s = "a, b, c\nd, e, f\ng, h, i";
            String[] elements = s.replace("\n", ",").split(",");
            String[][] table = new String[3][3];
            for (int i = 0; i < 3; i++) {
                for(int j = 0; j < 3; j++){
                    table[i][j] = elements[(j * 3) + i];
                }
            }
    
    String s=“a,b,c\nd,e,f\ng,h,i”;
    String[]elements=s.replace(“\n”,“,”).split(“,”);
    字符串[][]表=新字符串[3][3];
    对于(int i=0;i<3;i++){
    对于(int j=0;j<3;j++){
    表[i][j]=元素[(j*3)+i];
    }
    }
    

    希望对您有所帮助。

    使用嵌套循环的解决方案。如果你担心它不是O(n),不要担心:它是O(n)。所需时间始终与输入元素的数量成正比

    具体示例#1:在3行3列输入中,总共有9个元素。外部循环执行3次。内部循环对每个循环执行3次,总共执行9次。9个输入=9个内部循环执行

    具体示例#2:现在让我们假设您给它一个5行7列的输入。你一共有35件物品。外部循环执行五次。内部循环对每个循环执行七次,总共5x7=35次。35个输入=35个内部循环执行

    摘要:按c列输入r行如何?总输入项目=r x c。外部循环执行r次,内部循环对每个循环执行c次。因此,rxc是总的内部循环迭代。r x c输入项=r x c内部循环迭代

    无论输入元素的总数是多少,内部循环执行的次数都是如此之多。这实际上就是O(n)的定义。你可以试试下面的代码

        String text = "a, b, c\nd, e, f\ng, h, i";
        Pattern pattern = Pattern.compile("(.+)(?:\r?\n|$)");
        Matcher matcher = pattern.matcher(text);
        List<List<String>> result = Lists.newArrayList();
        while (matcher.find()){
            List<String> row = Splitter.on(",").trimResults().splitToList(matcher.group(1));
            result.add(row);
        }
    
        System.out.println(result);
    

    希望有帮助。

    @dasblinkenlight哦,是的,对不起,我会编辑的now@shmosel我现在要用谷歌搜索番石榴,我可能还应该提到,我给出的输入是一个例子,我的很多测试都是相似的,但有些测试的行数和列数不同,我不能在
    3
    @shmosel中硬编码,不管最后的部分,我可能会找到一些我喜欢你的方法的长度,但如果可能的话,我会尝试不使用嵌套循环。我试图得到一个O(n)的情况谢谢,我现在就试试这个嘿,我试过你的代码,看起来它是以行输出的,但我要求列,谢谢你想是的,但在我的情况下,n是行数。对不起,我不清楚,我将编辑我的问题