Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 字符[]的数组,在数组中查找一些字母_Java_Arrays_For Loop_While Loop_Char - Fatal编程技术网

Java 字符[]的数组,在数组中查找一些字母

Java 字符[]的数组,在数组中查找一些字母,java,arrays,for-loop,while-loop,char,Java,Arrays,For Loop,While Loop,Char,我必须做一个方法,找出像{'d','e','f'}这样的char[]数组是否在像{'a','b','c','d','e','f','x','r'}这样的char[]数组的内部。。。所以,我希望我的“x”在里面,而cicle在找到一封信时会升起。例如,当my For cycle找到第一个字母my x=1时,如果For cycle找到第二个字母my x=x+1,依此类推。。。所以我的x应该等于数组{'d','e','f'}的长度。而如果我必须在另一个数组{'a','d','z','x'}中找到像{'

我必须做一个方法,找出像{'d','e','f'}这样的char[]数组是否在像{'a','b','c','d','e','f','x','r'}这样的char[]数组的内部。。。所以,我希望我的“x”在里面,而cicle在找到一封信时会升起。例如,当my For cycle找到第一个字母my x=1时,如果For cycle找到第二个字母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; 
} 
}
这是我写入if(char1.find2(char3)=l)时终端上的错误:

数组中搜索
subs
的字符:

int find(char[] array, char[] subs)
该方法返回匹配的字符数。

需要注意的是,数组中字符的顺序并不重要。例如,当
数组
声明为
{f'、'd'、'e'}
并且
子类
{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));
    }
}

当你在第一个字母之后找到一个匹配的字母时,你会增加
x
,你找不到任何第二个字母,因此
x
的初始值为1。基本上,代码似乎完全按照我的预期工作,但你的需求很难理解。您希望
{'a',d',z',x'}
的返回值是什么?那么
{'a','d','e','f','d','e','f'}
呢?@可能的工作:批评原始缩进是一回事,但没有必要以冒犯的方式这样做。@JonSkeet不认为这是冒犯。如果我在一个我必须维护的代码中看到这样的东西,我会对作者非常生气。“有什么不对劲,但是……”你怎么知道有什么不对劲?你收到错误信息了吗?什么信息?另外,您的示例使用的变量和函数没有向我们展示任何定义。当我们看不到所有的代码时,很难说代码出了什么问题,我们也不知道症状是什么。谢谢回复,我刚刚编辑了代码。但是顺序对我来说是必要的,如果我有{'d','e','f',我想搜索“def”,如果在char数组中,如果有id“fed”,那就不好了。在main方法中,我想比较我的变量x(在本例中找到)是否等于subs的长度:)很好,然后在我的代码中做这些小改动。:)
int find(char[] array, char[] subs)
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