【问题标题】:Array of Char[ ] , find some letter in an arrayChar[ ] 的数组,在数组中找到某个字母
【发布时间】:2015-03-01 17:47:49
【问题描述】:

我必须做一个方法来判断一个像 {'d','e','f'} 这样的 char[ ] 数组是否在另一个像 {'a','b 这样的 char[ ] 数组中','c','d','e','f','x','r'} ... 所以,我希望我的“x”在里面,而当找到一个字母时 cicle 会升起。例如,当 for 循环找到第一个字母 my x= 1 时,如果 for 循环找到第二个字母 my x = x+1 等等......所以我的 x 应该等于数组的长度 {'d ','e','f'} 。而如果我必须在像 {'a','d','z','x'} 这样的另一个数组中找到像 {'d','e','f'} 这样的数组,我的 x 是 1 所以它不等于数组 {'d','e','f'} 的长度, 有问题,但我找不到错误。

public class Stringa 
{
private char[] array1 ;
public int find2(char[]subs)
{
    int x = 1 ;  
    for(int i=0 ; i<this.lunghezza(); i++ ) // lunghezza() find the length
    {
        if(this.array1[i] == subs[0])
        {
            for ( int k = 1 ; k< subs.length ; k++)
            { 
                while (this.array1[i+k]== subs[k]) 
                {
                    x = x+1; ;
                }
            }
        }
    }
    return x; 
} 
}

主要方法是:

  public class StringaTester
   {
     public static void main(String[] args)
     {
      char[] char1 = {'a','b','c','d','e','f','g','e','r','t'};
      char[] char2 = {'a','b','c','d','e','f','g','e','r','t'};
      char[] char3 = {'d','e','f'};
      Stringa prova = new Stringa(char1);
      Stringa prova1 = new Stringa(char2);
      Stringa prova3 = new Stringa(char3);

      int l = prova3.lunghezza(); // this find the legth of the substring ( Now 3 )
      if(char1.find2(char3) == l ) // I want to compare 2 char , but is here the error
                                   // if i write  if(prova.find2(char3) == l )
                                   // there aren't any errors
      System.out.println(" substring is inside"); 
      else
      System.out.println("substring is not inside");    
   }
 }

这是我写 if(char1.find2(char3) == l) 时终端上的错误:

 StringaTester.java:20: error: cannot find symbol
 if(char1.find2(char3) == l )
             ^
 symbol:   method find2(char[])
 location: variable char1 of type char[]
 1 error

【问题讨论】:

  • 好吧,每当您在第一个字母之后找到匹配的字母时,您都会增加 x - 您没有找到任何第二个字母,因此 x 有其原始字母值为 1。
  • 基本上,代码似乎完全按照我的预期工作,但您的要求很难理解。您希望{'a', 'd', 'z', 'x' } 的返回值是多少? {'a', 'd', 'e', 'f', 'd', 'e', 'f' }呢?
  • @MightyPork:批评原始缩进是一回事 - 但没有必要以冒犯的方式这样做。
  • @JonSkeet 不要认为这是冒犯性的。如果我在我必须维护的代码中看到类似的东西,我会更生作者的气。
  • "有问题但是..." 你怎么知道有问题?您收到错误消息吗?讯息是什么?此外,您的示例使用了您没有向我们展示任何定义的变量和函数。当我们看不到所有代码时,很难说代码出了什么问题,而且我们不知道症状是什么。

标签: java arrays for-loop while-loop char


【解决方案1】:

The following implementationarray中搜索subs的字符:

int find(char[] array, char[] subs)

该方法返回匹配的字符数。

请务必注意,数组中字符的顺序无关紧要。比如array声明为{ 'f','d','e' }subs{ 'd','e','f' },则该方法返回的匹配数为3

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{
    public static int find(char[] array, char[] subs)
    {
        int found = 0;
        for (int x = 0; x < subs.length; x++)
        {
            for (int y = 0; y < array.length; y++)
            {
                if (subs[x] == array[y])
                {
                    found++;

                    // Y is the index of the element found in the original array
                    // we must erase this element so it's not found again.
                    char[] smaller_array = new char[array.length-1];
                    for (int i = 0; i < array.length; i++)
                    {
                        if (i < y)
                            smaller_array[i] = array[i];

                        if (i == y)
                         continue;

                        if (i > y)
                            smaller_array[i-1] = array[i];
                    }

                    array = smaller_array;
                    break;
                }
            }
        }


        return found;
    }

    public static void main (String[] args) throws java.lang.Exception
    {
        char[] sub = { 'd','e','f' };

        char[] array1 = { 'a','b','c','d','e','f','x','r' };
        System.out.println("Number of matches with array #1: " + find(array1, sub));

        char[] array2 = { 'g','e','h','i','d','k','x','f' };
        System.out.println("Number of matches with array #2: " + find(array2, sub));

        char[] array3 = { 'd' };
        System.out.println("Number of matches with array #3: " + find(array3, sub));

        char[] array4 = { 'd','d','d' };
        System.out.println("Number of matches with array #4: " + find(array4, sub));

        char[] array5 = { 'd','e','f' };
        System.out.println("Number of matches with array #5: " + find(array5, sub));

        char[] array6 = { 'f','d','e' };
        System.out.println("Number of matches with array #6: " + find(array6, sub));

        char[] array7 = { 'a','b','c','g','h','i','j','k' };
        System.out.println("Number of matches with array #7: " + find(array7, sub));
    }
}

输出

Number of matches with array #1: 3
Number of matches with array #2: 3
Number of matches with array #3: 1
Number of matches with array #4: 1
Number of matches with array #5: 3
Number of matches with array #6: 3
Number of matches with array #7: 0

【讨论】:

  • 感谢回复,我刚刚编辑了代码。但是对我来说顺序是必不可少的,如果我有 {'d','e','f'} 我想搜索 if "def" if in a array of char ,如果有 id "fed" 那就不好了.在主要方法中,我想比较我的变量 x(在这种情况下找到)是否等于 subs 的长度 :)
  • 太好了,那么只需在我的代码中做这些小改动。 :)
猜你喜欢
  • 2012-01-23
  • 2016-03-07
  • 1970-01-01
  • 1970-01-01
  • 2015-01-31
  • 1970-01-01
  • 1970-01-01
  • 2019-09-26
  • 1970-01-01
相关资源
最近更新 更多