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;
    }
}
有帮助吗?

解决方案

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;
    }
}

其他提示

You want to return an int, but you are passing an Integer[] as argument. You basically have 4 options

  1. keep the method as it is (suggested..). Now, if you are bent upon keeping the return type as private void, then

  2. Create an object of FrequenceyPrint in main(), call findMax() using the object's reference, pass an Integer/object which encapsulates an int/Integer as argument . change the object referenced by the reference in findMax, your main() will get back the data

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top