【问题标题】:finding specific combination of chars one after another in 2d array java在二维数组java中一个接一个地找到特定的字符组合
【发布时间】:2016-08-29 14:29:28
【问题描述】:

我想在由 a 到 z 的随机字母组成的大二维数组中找到 char 字母的组合。 字母组合首先如下:字母'a',紧跟在这个'l'之后的第二个字母和之后的第三个字母将再次是'a'。 如果找到了这些字母的组合,那么我想将它出现的数量增加 1。所以最后我会确切地知道这些特定字母按此顺序随机生成了多少次。

    char[][] x = new char[1000][1100]; 
    int amountofAla = 0;    

    for (int i=0; i<x.length; i++) {
        for (int r=0; r<x[i].length; r++)  {
            x[i][r] = abc.charAt((int)(Math.random()*26)) ;
        }
    } // my 2d tab with random generated letters was made here

    // below is my attempt to find combination of letters 'a'+'l'+'a'
    for (int i=0; i<x.length; i++) 
    {
    for (int w=0; w<x.length; w++) 
        {
        if (x[i][w] ==('a') && x[i][w+1] == 'l' && x[i][w+2] == 'a')
            amountofAla = amountofAla+1 ;
        else;
            }
    }
    System.out.println("occurance= " + amountofAla);

我按此顺序查找字母的尝试给出了错误的数字。我打印了标签并手动搜索了ala,数字总是错误的。

    // print array for refference 
    for (int i=0; i<x.length; i++) {
        for (int r=0; r<x[i].length; r++) {
            System.out.print( x [i] [r]);
        } System.out.println( );
    }

提前感谢您对给定问题的任何想法。

【问题讨论】:

  • 提示:不要像这样创建/测试代码。相反:首先编写一个单元测试,定义一个包含 known 内容的数组。然后使用该数组作为您的实际代码的输入,该代码应该找到您想要找到的任何内容。尝试调试一个复杂的算法,同时让每个新的运行都使用不同的输入数据......这就像浪费大量时间
  • 代码几乎没有任何问题。但你可能最终会出现 ArrayIndexOutOfBound。你在 for 循环中的条件应该是 for (int w=0; w&lt;x.length-2; w++)
  • 到目前为止你得到的结果是多少?
  • 我的代码似乎没有找到所有组合。我在 txt 文件中使用 ctrl f 手动检查。下面使用正则表达式的解决方案似乎效果很好。谢谢你们。

标签: java arrays char


【解决方案1】:

如果您将二维数组的“行”从 char[] 转换为字符串,则无需自己处理匹配。找出“ala”出现次数的两种可能性:

首先使用正则表达式

public static void main (String []args){

    char[][] x = new char[10][11]; 

    int amountofAlaTotal = 0;
    String abc = "abcdefghijklmnopqrstuvwxyz";
    for (int i=0; i<x.length; i++) {
        for (int r=0; r<x[i].length; r++)  {
            x[i][r] = abc.charAt((int)(Math.random()*26)) ;
        }
    }         
    for (int i=0; i<x.length; i++) {            
        String str = String.valueOf(x[i]); //convert a char array to a string
        Pattern p = Pattern.compile("ala");
        Matcher m = p.matcher(str);
        int amountofAlaPerLine = 0;
        while (m.find()){
            amountofAlaPerLine +=1;
        }

        if(amountofAlaPerLine>0){
            System.out.println("str: "+str );
            System.out.println("index: " + i );
            System.out.println("matches: "+amountofAlaPerLine);
        }
        amountofAlaTotal += amountofAlaPerLine;

    }
    System.out.println("total: "+amountofAlaTotal );
}

或第二次使用来自 Apache Commons Lang 的 StringUtils.countMatches

public static void main (String []args){

    char[][] x = new char[10][11]; 
    int amountofAlaPerLine = 0;
    int amountofAlaTotal = 0;
    String abc = "abcdefghijklmnopqrstuvwxyz";
    for (int i=0; i<x.length; i++) {
        for (int r=0; r<x[i].length; r++)  {
            x[i][r] = abc.charAt((int)(Math.random()*26)) ;
        }
    }         
    for (int i=0; i<x.length; i++) {            
        String str = String.valueOf(x[i]); //convert a char array to a string
        amountofAlaPerLine = StringUtils.countMatches(str, "ala"); //count matches
        if(amountofAlaPerLine>0){
            System.out.println("str: "+str );
            System.out.println("index: " + i );
            System.out.println("matches: "+amountofAlaPerLine);
        }
        amountofAlaTotal += amountofAlaPerLine;

    }
    System.out.println("total: "+amountofAlaTotal );
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 2012-01-29
    • 2014-04-19
    • 2017-10-12
    • 1970-01-01
    • 2018-01-10
    • 1970-01-01
    相关资源
    最近更新 更多