Java 是否按字母顺序放置从数组接收的字符而不使用排序函数?

Java 是否按字母顺序放置从数组接收的字符而不使用排序函数?,java,arrays,string,ascii,frequency,Java,Arrays,String,Ascii,Frequency,所以,基本上,我用java创建了一个算法,从字符串中提取字符并计算其频率。现在,我需要按字母顺序打印 例如: A频率:3 d频率:1 l频率:5 有什么建议吗?这是我到目前为止所做的 int[] charCounts(String userSort) { int[] counts = new int[256]; char[] c = userSort.toCharArray(); for (int i=0;i<c.length;++

所以,基本上,我用java创建了一个算法,从字符串中提取字符并计算其频率。现在,我需要按字母顺序打印

例如: A频率:3 d频率:1 l频率:5

有什么建议吗?这是我到目前为止所做的

     int[] charCounts(String userSort) {
        int[] counts = new int[256];
        char[] c = userSort.toCharArray();
        for (int i=0;i<c.length;++i) {
            counts[c[i]]++;
        }
        return counts;
}
int[]字符数(字符串用户排序){
int[]计数=新的int[256];
char[]c=userSort.toCharArray();

对于(int i=0;i你已经有了80%了。你已经有了一个类似于的排序映射。你正在使用分布式-聚集技术。我猜你没有意识到,在分发中,你已经对字符进行了排序。你只需要重新收集它们以进行输出。分发和聚集可以是一种排序形式。但是,它没有使用排序函数本身

我不会给出代码,因为你已经有了一个良好的开端,你只是在征求建议

首先,您已经完成的分发部分:

ASCII只有128个字符。但是,Java无论如何都不使用ASCII。即便如此,将此练习的范围限制在块加上C1控件和拉丁语-1补充块似乎是合理的,这就是您的
新int[256]
所做的

那么,这些块中的字符是按您想要的顺序排列的吗?这不是与
计数中的顺序相同吗?您已将字符作为计数分布到有序的框中。
计数[c[i]+

聚会:

您只需按照所需的顺序再次收集字符,并以所需的格式写出它们。循环
计数
就可以做到这一点。您的问题标题要求将字符按顺序排列,这样就意味着输出的字符数相同。您可以将字符数转换为重复字符。如果您的目标是就是简单地把它们打印出来,你可以在一个循环中这样做;否则,你必须这样做


顺便说一句,这种顺序被称为词典,因为它是基于一个字符集或编码定义的。字母意味着选择的符号序列(在数学意义上)或特定自然语言的特定书写系统,其中一些字母被流行的惯例或语言学院指定为“字母表”。例如,拉丁字母的丹麦字母:ABCDEFGHIJKLMNOPQRSTUVWXYZÆØ。请参阅。

将结果存储在有序的映射中,如。在映射中迭代键将按排序顺序输出,无需额外处理

这需要对代码进行少量修改。您将返回一个数组,而不是返回一个数组:

返回的
将像映射本身一样进行排序

如果无法使用所提供的特定函数,可以在调用映射后将结果添加到映射中,然后像前面一样在映射上迭代:

 int[] counts = charCounts(...);
 TreeMap<Character, Integer> map = new TreeMap<>();
 for(char c = 0; c < counts.length; c++)
      map.put(c, counts[c]);
int[]计数=字符计数(…);
TreeMap map=newtreemap();
for(字符c=0;c
首先,您希望对提供的字符串中的字符进行排序,一种不使用任何API排序方法的简单方法是使用两个for循环。当然,您需要将提供的字符串分解为字符数组。假设提供的字符串是
“这是我要排序的字符串”

现在,使用两个for循环遍历字符数组,并操纵该数组的元素,使最小的字符值位于开始位置,而较大的值逐渐位于结束位置。这种类型的排序称为a,如下所示:

注意:是的……下面的代码中有很多注释来解释发生了什么。这些注释太多了,简直是一目了然 凌乱。这就是编辑器的优点,你可以很容易地删除它 如果你不想要的话就给他们

//提供的要排序的字符串。
String suppliedString=“这是我要排序的字符串”;
//删除所有空白。我们不需要它们
//由于我们的目标是分类和
//获取角色引用。如果您还想
//处理空白,然后注释代码行
//下面。
suppliedString=suppliedString.replace(“,”);
//将提供的字符串转换为字符数组。
char[]charArray=suppliedString.toCharArray();
//声明一个字符变量以保存当前
//正在处理的字符数组元素值。
char tempChar;
//使用两个字符遍历字符数组
//FOR循环,以便创建一个字符串
//将最小字符值保留为最大字符值
//字符值。
for(int i=0;i for(Character c : charCounts(...).keySet()) ...
 int[] counts = charCounts(...);
 TreeMap<Character, Integer> map = new TreeMap<>();
 for(char c = 0; c < counts.length; c++)
      map.put(c, counts[c]);
String suppliedString = "This is my string to sort";
char[] charArray = suppliedString.toCharArray();
// The supplied String to sort.
String suppliedString = "this is my string to sort";
// Remove all whitespaces. We don't need them for 
// this excercise since our goal is to sort and 
// get character occurrences. If you want to also
// process whitespaces then comment the code line
// below.
suppliedString = suppliedString.replace(" ", "");

// Convert the supplied string to a character array.
char[] charArray = suppliedString.toCharArray();
// Declare a Character variable to hold the current
// Character Array element value being processed.
char tempChar;
// Iterate through the character array with two
// FOR loops so as to create a string which will
// hold the least character values to the greatest
// character values.
for (int i = 0; i < charArray.length; i++) {
    for (int j = 0; j < charArray.length; j++) {
        // Is the current Array element value in 
        // charArray[i] less than the what is in
        // the current Array element for charArray[j]?
        if (charArray[i] < charArray[j]) {
            // Yes it is...
            // Hold our current character element value.
            tempChar = charArray[i];
            // Now make the Array element at index i hold
            // what is in Array element at index j.
            charArray[i] = charArray[j];
            // Make the Array element at index j hold what
            // was originally in the Array element at index i.
            charArray[j] = tempChar;
        }
        // No it's not so let's continue iterations through 
        // the character array using the index place-holder 
        // j to see if there are still more character Array 
        // element values less than what is currently in the 
        // Character Array index place-holder i location.
    }
    // continue iterations through the character array 
    // using the index place-holder i to see if there 
    // are still more character Array element values less
    // that what might be in the Character Array index place
    // -holder j location.
}

//==============================================
// For your project you don't need this little
// section. I just added it so you can see what 
// the sort looks like.
// Now use yet another FOR loop to convert the 
// the sorted Character Array (charArray[]) back
// to a sorted string.
// Declare and initialize a String variable to 
// Null String (""). This variable will hold the
// new Sorted String.
String sortedString = "";
for (int i = 0; i < charArray.length; i++) {
    sortedString+= charArray[i];
}

// Display the sorted String. If you don't
// want spaces in your sort then use: 
// System.out.println(sortedString.trim());
// Spaces have the least value (32) so they
// will almost always be at the beginning of
// the sorted string.
System.out.println("Sorted String: -->   " + sortedString + "\n");
//==============================================

// Now that the Character Array is sorted let's
// use yet another couple FOR loops to figure out
// the occurrences of each character. We'll use our 
// same String variable (sortedString) to hold our 
// display text to console. (Note: There's a lot of 
// ways to do this sort of thing in Java)
int counter; // counter used to keep track of char occurrences.
sortedString = "";
for (int i = 0; i < charArray.length; i++) {
    counter = 0; // new character. Make sure counter is zeroed
    // Iterate through the entire array and count
    // those that are the same.   
    for (int j = 0; j < charArray.length; j++) {
        if (charArray[i] == charArray[j]) {
            counter++;
        }
    }
    // Make sure we don't place duplicate character/frequencies
    // into the string we're creating.
    if (!sortedString.contains("Char: " + charArray[i])) {
        // Add the current character and occurrence
        // to our string variable.
        if (sortedString.equals("")) {
            sortedString+= "Char: " + charArray[i] + " - Freq: " + counter; 
        } 
        else {
            sortedString+= " || Char: " + charArray[i] + " - Freq: " + counter; 
        }
    }
}
// Display the sorted characters and their occurrences.
System.out.println(sortedString);
LinkedHashMap<Character, Integer> charToCount = new LinkedHashMap<>();
charToCount.put('a', 0);
charToCount.put('b', 0);
charToCount.put('c', 0);
charToCount.put('d', 0);
charToCount.put('e', 0);
String inputToSort = ...
for (char c : inputToSort.toCharArray()) {
    // Increase counter by one
    charToCount.put(c, charToCount.get(c) + 1);
}
StringBuilder sb = new StringBuilder();
// Every character
for (Entry<Character, Integer> entry : charToCount.entrySet()) {
    // How often it occurred
    for (int i = 0; i < entry.getValue(); i++) {
        sb.append(entry.getKey());
    }
}
String output = sb.toString();