如何缩短我的两种方法?

时间:2015-11-17 00:14:23

标签: java arrays string methods

所以我写了两个方法,一个用于查找数组中最常用的数字,另一个用于查找数组中最常见的名称。两者都非常复杂。

我理解如何处理每个问题,但我不确定如何压缩我的方法。另外,我不允许使用数组方法

  

首先,最常见的数字方法。

 public static int mostFrequentDigit(int[] a){
    int count0=0, count1=0, count2=0, count3=0, count4=0, count5=0, count6=0, count7=0, count8=0, count9=0;
    for (int i=0;i<a.length;i++){
     int digit=a[i]%10;
     if (digit=0) count0++;
      else if (digit=1) count1++;
      else if (digit=2) count2++;
      else if (digit=3) count3++;
      else if (digit=4) count4++;
      else if (digit=5) count5++;
      else if (digit=6) count6++;
      else if (digit=7) count7++;
      else if (digit=8) count8++;
      else count9++;
    }
    if (count0> count1, count2, count3, count4, count5, count6, count7, count8, count9);
     return count0;
    if (count1>count0, count2, count3, count4, count5, count6, count7, count8, count9);
     return count1;
    if (count2>count0, count1, count3, count4, count5, count6, count7, count8, count9);
     return count2;
    if (count3>count0, count1, count2, count4, count5, count6, count7, count8, count9);
     return count3;
    if (count4>count0, count1, count2, count3, count5, count6, count7, count8, count9);
     return count4;
    if (count5>count0, count1, count2, count3, count4, count6, count7, count8, count9);
     return count5
    if (count6>count0, count1, count2, count3, count4, count5, count7, count8, count9);
     return count6;
    if (count7>count0, count1, count2, count3, count4, count5, count6, count8, count9);
     return count7;
    if (count8>count0, count1, count2, count3, count4, count5, count6, count7, count9);
     return count8;
    else 
     return count9;
}}
  

现在,最常见的名称方法。

public static String mostCommonName (String[] names) {
     int indexOfFrequency = 0;
     int indexOfMostFrequency = 0;
     String mostCommon = "";
     int frequency = 1;
     int mostFrequency = 1;
     for (int index = 0; index < names.length; index ++) {
       indexOfFrequency = index;
      for (int scan = index + 1; scan < names.length; scan ++) {
        if ((names[scan].compareTo(names[index])) == (0)) {
          indexOfFrequency = scan;
          frequency ++;
        }
      }
      if (frequency > mostFrequency) {
        mostFrequency = frequency;
        mostCommon = names[indexOfFrequency];
        indexOfMostFrequency = indexOfFrequency;
      }
      else if (frequency == mostFrequency) {
        if ((names[indexOfFrequency].compareTo(names[indexOfMostFrequency]))   < 0) {
          mostCommon = names[indexOfFrequency];
          indexOfMostFrequency = indexOfFrequency;
        }
        else {
          mostCommon = names[indexOfMostFrequency];
        }
      }
      frequency = 1;
    }
    return mostCommon;
    }

2 个答案:

答案 0 :(得分:0)

如果你只想考虑最不重要的数字,你可以这样做。

int[] a = {1, 2, 3, 4, 5, 6, 1};
int mostCommon = IntStream.of(a)
         // get the lowest digit
        .mapToObj(i -> i % 10)
        // create a mapping of digits => count of digits
        .collect(Collectors.groupingBy(i -> i, Collectors.counting()))
        // take the entries
        .entrySet().stream()
        // sort them by count, descending.
        .sorted(Comparator.comparing(e -> -e.getValue()))
        // give me the first result.
        .findFirst()
        // if there was no result, throw an error.
        .orElseThrow(AssertionError::new)
        // give me the digit.
        .getKey();

System.out.println(mostCommon);

打印

1

如果你想要一个整数的所有数字,你需要写一个方法来返回所有数字的流并使用.flatMap()

对于名称,除了您需要Stream.of(a).collect...

之外,代码是相同的

答案 1 :(得分:0)

让我先回答数字计数问题。您的代码似乎只查看最不重要的数字。您需要在计算每个整数之前找到所有数字。

以下是使用Java 8流的可能解决方案:

Optional<Integer> mostCommonDigit(int[] numbers) {
    return Arrays.stream(numbers)
        .flatMap(this::streamDigits)
        .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
        .entrySet().stream()
        .max(Map.Entry.comparingByValue())
        .map(Map.Entry::getKey);
}

private Stream<Integer> streamDigits(int number) {
    List<Integer> digits = new ArrayList<>();
    while (number > 0) {
        digits.add(number % 10);
        number /= 10;
    }
    return digits.stream();
}

返回Optional以处理空数组。