按字母顺序显示 ArrayList
Displaying an ArrayList alphabetically
我写了一个 class,它接受一个字符串,计算字符串中每个字母的出现次数,然后打印每个字母的出现次数。我希望它按字母顺序显示,但不知道该怎么做。
import java.util.ArrayList; // import the ArrayList class
class CryptCmd {
public static void CryptCmd(String str) {
ArrayList<String> occurs = new ArrayList<>();
final int MAX_CHAR = 256;
// Create an array of size 256 i.e. ASCII_SIZE
int[] count = new int[MAX_CHAR];
int len = str.length();
// Initialize count array index
for (int i = 0; i < len; i++)
count[str.charAt(i)]++;
// Create an array of given String size
char[] ch = new char[str.length()];
for (int i = 0; i < len; i++) {
ch[i] = str.charAt(i);
int find = 0;
for (int j = 0; j <= i; j++) {
// If any matches found
if (str.charAt(i) == ch[j])
find++;
}
if (find == 1)
occurs.add("Number of Occurrence of " + str.charAt(i) + " is: " + count[str.charAt(i)] + "\n");
}
System.out.println(String.join("",occurs));
int total = 0;
for(int i = 0; i < str.length(); i++) {
if(str.charAt(i) != ' ')
total++;
}
System.out.println("Total chars is " + total);
}
}
到目前为止,打印按照找到字母的顺序显示,即
"Hello" =
Number of Occurrence of H is: 1
Number of Occurrence of e is: 1
Number of Occurrence of l is: 2
Number of Occurrence of o is: 1
Total chars is 5
所需的输出是这样的,按字母顺序排列,即
"Hello" =
Number of Occurrence of e is: 1
Number of Occurrence of H is: 1
Number of Occurrence of l is: 2
Number of Occurrence of o is: 1
Total chars is 5
它 returns 因为 H
在你的情况下是大写,而在 ASCII ordering 之前是小写字母。
A - 65 - 起始于
a - 97 - 开始于
就是这个原因。
维护一个案例就妥当了。否则你必须自己写 comparator
order 类似于 this.
这是代码
import java.util.*; // import the ArrayList class
class CryptCmd1 {
public static void main(String[] args) {
CryptCmd("Hello");
}
public static void CryptCmd(String str) {
ArrayList<String> occurs = new ArrayList<>();
final int MAX_CHAR = 256;
// Create an array of size 256 i.e. ASCII_SIZE
int[] count = new int[MAX_CHAR];
int len = str.length();
// Initialize count array index
for (int i = 0; i < len; i++)
count[str.charAt(i)]++;
// Create an array of given String size
char[] ch = new char[str.length()];
for (int i = 0; i < len; i++) {
ch[i] = str.charAt(i);
int find = 0;
for (int j = 0; j <= i; j++) {
// If any matches found
if (str.charAt(i) == ch[j])
find++;
}
String outString = "Number of Occurrence of " + str.charAt(i) + " is: " + count[str.charAt(i)] + "\n";
if (find == 1)
occurs.add(outString.toLowerCase());
}
Collections.sort(occurs);
System.out.println(String.join("",occurs));
int total = 0;
for(int i = 0; i < str.length(); i++) {
if(str.charAt(i) != ' ')
total++;
}
System.out.println("Total chars is " + total);
}
}
我已经使用collections
class对数组进行排序并满足您的要求
打印之前只需添加以下排序逻辑
Collections.sort(occurs, (a,b) -> a.compareToIgnoreCase(b));
我写了一个 class,它接受一个字符串,计算字符串中每个字母的出现次数,然后打印每个字母的出现次数。我希望它按字母顺序显示,但不知道该怎么做。
import java.util.ArrayList; // import the ArrayList class
class CryptCmd {
public static void CryptCmd(String str) {
ArrayList<String> occurs = new ArrayList<>();
final int MAX_CHAR = 256;
// Create an array of size 256 i.e. ASCII_SIZE
int[] count = new int[MAX_CHAR];
int len = str.length();
// Initialize count array index
for (int i = 0; i < len; i++)
count[str.charAt(i)]++;
// Create an array of given String size
char[] ch = new char[str.length()];
for (int i = 0; i < len; i++) {
ch[i] = str.charAt(i);
int find = 0;
for (int j = 0; j <= i; j++) {
// If any matches found
if (str.charAt(i) == ch[j])
find++;
}
if (find == 1)
occurs.add("Number of Occurrence of " + str.charAt(i) + " is: " + count[str.charAt(i)] + "\n");
}
System.out.println(String.join("",occurs));
int total = 0;
for(int i = 0; i < str.length(); i++) {
if(str.charAt(i) != ' ')
total++;
}
System.out.println("Total chars is " + total);
}
}
到目前为止,打印按照找到字母的顺序显示,即
"Hello" =
Number of Occurrence of H is: 1
Number of Occurrence of e is: 1
Number of Occurrence of l is: 2
Number of Occurrence of o is: 1
Total chars is 5
所需的输出是这样的,按字母顺序排列,即
"Hello" =
Number of Occurrence of e is: 1
Number of Occurrence of H is: 1
Number of Occurrence of l is: 2
Number of Occurrence of o is: 1
Total chars is 5
它 returns 因为 H
在你的情况下是大写,而在 ASCII ordering 之前是小写字母。
A - 65 - 起始于
a - 97 - 开始于
就是这个原因。
维护一个案例就妥当了。否则你必须自己写 comparator
order 类似于 this.
这是代码
import java.util.*; // import the ArrayList class
class CryptCmd1 {
public static void main(String[] args) {
CryptCmd("Hello");
}
public static void CryptCmd(String str) {
ArrayList<String> occurs = new ArrayList<>();
final int MAX_CHAR = 256;
// Create an array of size 256 i.e. ASCII_SIZE
int[] count = new int[MAX_CHAR];
int len = str.length();
// Initialize count array index
for (int i = 0; i < len; i++)
count[str.charAt(i)]++;
// Create an array of given String size
char[] ch = new char[str.length()];
for (int i = 0; i < len; i++) {
ch[i] = str.charAt(i);
int find = 0;
for (int j = 0; j <= i; j++) {
// If any matches found
if (str.charAt(i) == ch[j])
find++;
}
String outString = "Number of Occurrence of " + str.charAt(i) + " is: " + count[str.charAt(i)] + "\n";
if (find == 1)
occurs.add(outString.toLowerCase());
}
Collections.sort(occurs);
System.out.println(String.join("",occurs));
int total = 0;
for(int i = 0; i < str.length(); i++) {
if(str.charAt(i) != ' ')
total++;
}
System.out.println("Total chars is " + total);
}
}
我已经使用collections
class对数组进行排序并满足您的要求
打印之前只需添加以下排序逻辑
Collections.sort(occurs, (a,b) -> a.compareToIgnoreCase(b));