我必须做一个方法来查找char []的数组,如{'d','e','f'}是否在char []的内部,如{'a','b ','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
答案 0 :(得分:1)
The following implementation在subs
中搜索array
的字符:
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