Array of Char[ ] ,在数组中找到一些字母

Array of Char[ ] , find some letter in an array

我必须做一个方法来查找像 {'d','e','f' } 这样的 char[ ] 数组是否在另一个 char[ ] 数组中像 {'a','b','c','d','e','f','x','r'} ... 所以,我希望我的 "x" 里面 while cicle 在找到一个字母时升起。例如,当我的 for cycle 找到第一个字母 my x= 1 时,如果 for cycle 找到第二个字母 my x = x+1 等等......所以我的 x 应该等于数组的长度 {'d','e','f'}。而如果我必须在另一个数组如 {'a','d',[=39= 中找到像 {'d','e','f'} 这样的数组],'x'} 我的 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

The following implementationarray中搜索subs的字符:

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

方法returns匹配的字符数。

请务必注意,数组中字符的顺序并不重要。例如,当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