1

我创建了一个 Java 方法,当传递两个字符串数组 x 和 y 时,计算 y 中出现的每个字符串在 x 中出现的次数,并按照字符串出现在 y 中的顺序打印结果。例如,看一下 main 函数,它应该输出为 ab: 2, dc: 1, ef: 0。我的代码没有工作,因为它输出 ab: 1, ab: 2, dc: 3。

public class stringOccurInArray {
    public static void stringOccurInY(String[] x, String[] y) {
        int count = 0;
        for(int i=0; i<x.length; i++)  {
            for(int j=0; j<y.length; j++) {
                if(y[j].contains(x[i])) {
                    count++;
                    System.out.println(y[j] + ": " + count);
                }
            }
        }
        count = 0; // reset the count
    }

    public static void main(String[] args) {
        String[] a = {"ab", "cd", "ab", "dc", "cd"};
        String[] b = {"ab", "dc", "ef"};

        stringOccurInY(a, b);
    }
}
4

6 回答 6

2

有几件事要提。像这样重写代码更容易:

public static void stringOccurInY(String[] x, String[] y) {
    int count = 0;
    for (int i = 0; i < y.length; i++) {          
        for (int j = 0; j < x.length; j++) {
            if (y[i].contains(x[j])) {
                count++;
            }
        }
        System.out.println(y[i] + ": " + count);
        count = 0; // reset the count
    }
}

你应该首先迭代 y 。

您也可以通过 foreach 循环替换迭代。

for (String aY : y) {
    int count = 0;
    for (String aX : x) {
        if (aY.contains(aX)) {
            count++;
        }
    }
    System.out.println(aY + ": " + count);
    //no need to reset the count
}
于 2013-06-23T09:36:47.110 回答
1

Define int j = 0 outside the loop, then move System.out.println(y[j] + ": " + count); to outside the first for loop and reset count to 0 in the first line of the outer for loop.

BTW, why don't you use String#equals?

于 2013-06-23T09:33:43.177 回答
1
public static void stringOccurInY(String[] x, String[] y) {
        int count = 0;
        for(int i=0; i<x.length; i++)  {
            for(int j=0; j<y.length; j++) {
                if(y[j].contains(x[i])) {
                    count++;
                }
            }    
            System.out.println(y[j] + ": " + count);
            count = 0; // reset the count
        }
    }
于 2013-06-23T09:34:03.487 回答
1

进行以下修改以使其正常工作:

  • 切换x和循环 yfor
  • 在外循环中打印以避免重复打印一次。
  • count在外循环内部初始化以避免重复初始化。

    public class stringOccurInArray {
        public static void stringOccurInY(String[] x, String[] y) {
            for(int i=0; i<y.length; i++)  {
                int count = 0;
                for(int j=0; j<x.length; j++) {
                    if(x[j].equals(y[i])) {
                        count++;
                    }
                }
                System.out.println(y[i] + ": " + count);
            }
        }
    
        public static void main(String[] args) {
            String[] a = {"ab", "cd", "ab", "dc", "cd"};
            String[] b = {"ab", "dc", "ef"};
    
            stringOccurInY(a, b);
        }
    }
    
  • 于 2013-06-23T09:35:31.260 回答
    0

    您的代码应该是:

    for(int i=0; i<x.length; i++)  { 
    count = 0;
    for(int j=0; j<y.length; j++) {
    
    于 2013-06-23T09:35:58.847 回答
    0

    您也可以尝试以下代码。完全符合您的要求:

    public static void stringOccurInY(String[] x, String[] y) {
            int count = 0;
            HashMap<String, Integer> map = new HashMap<String, Integer>();
            for(int i=0; i<x.length; i++)  {
                for(int j=0; j<y.length; j++) {
                    if(y[j].contains(x[i])) {
                         if(map.get(y[j]) != null){
                             count = (int) map.get(y[j]);
                             count++;
                             map.put(y[j], count);
                         }else{
                             map.put(y[j], 1);
                         }
    
                    }
                }
    
            }
            System.out.println(map);
        }
    
    于 2013-06-23T09:49:53.420 回答