问题
I have to do a method that finds if an array of char[ ] like {'d','e','f' } is inside of an other array of char[ ] like {'a','b','c','d','e','f','x','r'} ... So , i want that my "x" inside while cicle raises when a letter is found . For example , when my for cycle finds the 1st letter my x= 1 , if for cycle finds the 2nd letter my x = x+1 and so on ... So my x should be equal to the length of the array {'d','e','f'} . Whereas if i have to find an array like {'d','e','f'} in an other array like {'a','d','z','x'} my x is 1 so it is not equal to the length of the array {'d','e','f'}, There is something wrong but I can't find the mistake .
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;
}
}
and the main method is :
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");
}
}
this is the error on the terminal when i write 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
回答1:
The following implementation searches for characters of subs
in array
:
int find(char[] array, char[] subs)
The method returns the number of characters that were matched.
It's important to note that the order of the characters in the arrays doesn't matter. For instance, when array
is declared as { 'f','d','e' }
and subs
is { 'd','e','f' }
, the number of matches returned by this method is 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));
}
}
Outputs:
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
来源:https://stackoverflow.com/questions/27758365/array-of-char-find-some-letter-in-an-array