字符数组[],发现在一个数组一些信 [英] Array of Char[ ] , find some letter in an array

查看:105
本文介绍了字符数组[],发现在一个数组一些信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做到这一点找到一个方法,如果字符数组[]像{'D','E','F'}为char类型的其它阵列内部的[]像{'A','B ','C','D','E','F','X','R'} ...所以,我想,我的X内,而当一个字母被发现cicle提高。例如,当我对循环查找第一个字母我的X = 1,如果发现周期第二封信我X = X + 1等等......所以我的x应等于数组的长度{'D ','E','F'}。而如果我有在其他数组一样找到像{'D','E','F'}数组{'A','D','Z','X'}我x是1,因此不等于数组{'D','E','F'}的长度,
也有一些是错误的,但我无法找到的错误。

 公共类Stringa
{
私人的char []数组1;
公众诠释find2(的char []潜艇)
{
    INT X = 1;
    的for(int i = 0; I< this.lunghezza();我++)// lunghezza()发现的长度
    {
        如果(this.array1 [Ⅰ] ==潜艇[0])
        {
            对于(INT K = 1; K< subs.length; k ++)
            {
                而(this.array1 [I + K] ==潜艇[K])
                {
                    X = X + 1; ;
                }
            }
        }
    }
    返回X;
}
}

和主要的方法是:

 公共类StringaTester
   {
     公共静态无效的主要(字串[] 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 =新Stringa(CHAR1);
      Stringa prova1 =新Stringa(CHAR2);
      Stringa prova3 =新Stringa(CHAR3);      INT L = prova3.lunghezza(); //这个发现子的legth(现在3)
      如果(char1.find2(CHAR3)== L)//我想比较2字符,但此处是错误
                                   //如果我写如果(prova.find2(CHAR3)== L)
                                   //没有任何错误
      的System.out.println(子里面);
      其他
      的System.out.println(子不里);
   }
 }

这是在终端上的错误,当我写如果(char1.find2(CHAR3)== 1):

  StringaTester.java:20:错误:无法找到符号
 如果(char1.find2(CHAR3)== L)
             ^
 符号:方法find2(的char [])
 位置:char类型的变量CHAR1 []
 1个错误


解决方案

下面的实现的搜索在阵列潜艇字符:

  INT找到(的char []数组,的char []潜艇)

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

要注意的是,字符数组中的顺序并不重要这一点很重要。例如,当阵列声明为 {'F','D','E'} 潜艇 {'D','E','F'} ,该方法返回匹配的数量是< STRONG> 3

 进口的java.util。*;
进口的java.lang。*;
进口java.io. *;类Ideone
{
    公共静态INT查找(的char []数组,CHAR []潜艇)
    {
        INT发现= 0;
        为(中间体X = 0; X&下; subs.length; X ++)
        {
            对于(INT Y = 0; Y&LT; array.length; Y ++)
            {
                如果(潜艇[X] ==数组[Y])
                {
                    发现++;                    // Y为元素的原始阵列中找到的索引
                    //所以它不会再次找到我们,必须清除这个元素。
                    的char [] = smaller_array新的char [array.length-1];
                    的for(int i = 0; I&LT; array.length,我++)
                    {
                        如果(ⅰ&下; y)的
                            smaller_array [I] =阵列[我]                        如果(我== Y)
                         继续;                        如果(ⅰ&GT; y)的
                            smaller_array [I-1] =阵列[I];
                    }                    阵列= smaller_array;
                    打破;
                }
            }
        }
        返回找到;
    }    公共静态无效的主要(字串[] args)抛出java.lang.Exception的
    {
        的char [] =子{'D','E','F'};        的char []数组1 = {'A​​','B','C','D','E','F','X','R'};
        的System.out.println(匹配与数阵列#1:+查找(数组1,次));        的char []数组2 = {'G','E','H','我','D','K','X','F'};
        的System.out.println(匹配与数阵列#2:+查找(数组2,分));        的char [] = ARRAY3 {'D'};
        的System.out.println(匹配与数阵列#3:+查找(ARRAY3,分));        的char [] = array4 {'D','D','D'};
        的System.out.println(匹配与数阵列#4:+查找(array4,分));        的char [] = array5 {'D','E','F'};
        的System.out.println(匹配与数阵列#5:+查找(array5,分));        的char [] array6 = {'F','D','E'};
        的System.out.println(匹配与数阵列#6:+查找(array6,分));        的char [] array7 = {'A​​','B','C','G','H','我','J','K'};
        的System.out.println(匹配与数阵列#7:+查找(array7,分));
    }
}

输出

 与数组#匹配数1:3
与阵列#2场数:3
与阵列#3场数:1
与阵列#4场数:1
与阵列#5场比赛数量:3
与阵列#6场数:3
与阵列#7场数:0

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

解决方案

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

这篇关于字符数组[],发现在一个数组一些信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆