[英]Count each element in string array
大家好,這是我不使用任何內置函數就先分割數組的代碼。 一切正常,我的問題在第二部分。
static String[] split(String ss) {
String[] a = new String[1];
String s = "";
for (int i = 0; i < ss.length(); i++) {
if (ss.charAt(i) == ' ') {
s += ", "
} else {
s += ss.charAt(i);
}
for (int j = 0; j < a.length; j++) {
a[j] = "[" + s + "]";
}
}
return a;
}
現在,我需要計算一個單詞中的每個字母,並且在沒有內置功能(例如split,chartoarray等)的情況下也將其給出。 這就是我到目前為止所看到的。
for example String="This is just an example". it should give out
This=4
is=2
..
static String[][] LettersCount(String[] array) {
int count=0;
String [][] a =new String[array.length][array.length];
String s= "" + Arrays.toString(array);
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ' ') {
count = 0;
} else {
count++;
}
畢竟,可以使用具有數值的字符屬性。 讓我們將其用作索引並將計數存儲在array
。 (所以我們將以此方式模擬Map)
int[] counter = new int[256] ;// this will hold count of all letters
counter[(int) character]++; // this is how you do the counting
public class JavaArrayLengthTest {
public static void main(String[] args) {
String[] testArray = { "A", "B", "C" };
int arrayLength = testArray.length;
System.out.println("The length of the array is: " + arrayLength);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.