0

How would I change this to go into the private void? It is currently in the public static void. The main problem is that in the private void it cant read string. I am quite new to programming so please be patient if this is quite easy to do.

import java.util.Arrays;
import java.util.HashMap;

public class FrequencyPrint {
    public static void main(String[] args) {
        String s = "ccrrcdcffcghijk";
        HashMap<Character, Integer> hashMap = new HashMap<Character, Integer>();
        for (int i = 0; i < s.length(); i++) {
            if (hashMap.containsKey(s.charAt(i))) {
                int value = hashMap.get(s.charAt(i));
                hashMap.put(s.charAt(i), ++value);
            } else {
                hashMap.put(s.charAt(i), 1);
            }
        }

        Character keys[] = Arrays.copyOf(hashMap.keySet().toArray(), hashMap
                .keySet().toArray().length, Character[].class);
        Integer values[] = Arrays.copyOf(hashMap.values().toArray(), hashMap
                .values().toArray().length, Integer[].class);


        for (int i = 0; i < keys.length; i++) {
            int x = FrequencyPrint.findmax(values);
            for (int j = 0; j < values[x]; j++) {
                System.out.print(keys[x]);
            }
            values[x] = 0;
        }

    }

    public static int findmax(Integer values[]) {
        int max = 0;
        for (int i = 0; i < values.length; i++) {
            if (values[i] > values[max]) {
                max = i;
            }
        }
        return max;
    }
}
4

2 回答 2

0
import java.util.Arrays;
import java.util.HashMap;

public class FrequencyPrint {
    public static void main(String[] args) {
        FrequencyPrint fP = new FrequencyPrint();
        fP.readStrings();
    }

    private void readStrings() {
        String s = "ccrrcdcffcghijk";
        HashMap<Character, Integer> hashMap = new HashMap<Character, Integer>();
        for (int i = 0; i < s.length(); i++) {
            if (hashMap.containsKey(s.charAt(i))) {
                int value = hashMap.get(s.charAt(i));
                hashMap.put(s.charAt(i), ++value);
            } else {
                hashMap.put(s.charAt(i), 1);
            }
        }

        Character keys[] = Arrays.copyOf(hashMap.keySet().toArray(), hashMap
            .keySet().toArray().length, Character[].class);
        Integer values[] = Arrays.copyOf(hashMap.values().toArray(), hashMap
            .values().toArray().length, Integer[].class);


        for (int i = 0; i < keys.length; i++) {
            int x = this.findmax(values);
            for (int j = 0; j < values[x]; j++) {
                System.out.print(keys[x]);
            }
            values[x] = 0;
        }

    }

    private int findmax(Integer values[]) {
        int max = 0;
        for (int i = 0; i < values.length; i++) {
            if (values[i] > values[max]) {
                max = i;
            }
        }
        return max;
    }
}
于 2014-03-18T16:33:14.710 回答
0

您想返回一个 int,但您将 Integer[] 作为参数传递。你基本上有4个选择

  1. 保持方法不变(建议..)。现在,如果您一心想要将返回类型保持为私有 void,那么

  2. 在 main() 中创建一个 FrequenceyPrint 对象,使用对象的引用调用 findMax(),传递一个封装 int/Integer 作为参数的 Integer/object。更改 findMax 中引用所引用的对象,您的 main() 将取回数据

于 2014-03-18T16:34:43.147 回答