Java:统计字符串数组首尾字符重复次数的问题
Hey there! Let's break down how to correctly merge your start and end character counts, and fix those incorrect results you're seeing.
First, let's clarify the core problem: you have two arrays tracking how often each character appears as the first character of strings, and how often they appear as the last character. To merge them, you just need to add the counts for the same character across both arrays—since each index in the array maps to a specific character (e.g., index 97 = 'a' in ASCII).
Common Mistakes That Cause Wrong Results
Before diving into the fix, let's cover the most likely issues that messed up your stats:
- Forgetting to check if strings are
nullor empty before accessing their first/last characters (this can throw exceptions or count invalid "characters"). - Merging arrays incorrectly (e.g., looping over the wrong range, or not adding corresponding indices).
Corrected Code with Merge Logic
Here's a complete, fixed version of your method, with clear comments explaining each step:
public static int[] countChar(String[] str) { // Use 256 to cover all ASCII characters (adjust if you only need letters!) int[] beginCounts = new int[256]; int[] endCounts = new int[256]; // Step 1: Count first characters for (String s : str) { // Skip null or empty strings to avoid errors and bad counts if (s == null || s.isEmpty()) { continue; } char firstChar = s.charAt(0); beginCounts[firstChar]++; } // Step 2: Count last characters for (String s : str) { if (s == null || s.isEmpty()) { continue; } char lastChar = s.charAt(s.length() - 1); endCounts[lastChar]++; } // Step 3: Merge the two count arrays int[] count = new int[256]; for (int i = 0; i < count.length; i++) { // Add counts for the same character (same index) count[i] = beginCounts[i] + endCounts[i]; } return count; }
If You Only Need to Count Letters (a-z/A-Z)
If you don't need to track all ASCII characters, you can optimize the arrays to size 26 (for lowercase letters) and normalize characters to avoid case sensitivity:
public static int[] countLetterOnly(String[] str) { int[] beginCounts = new int[26]; int[] endCounts = new int[26]; for (String s : str) { if (s == null || s.isEmpty()) continue; // Convert to lowercase to treat 'A' and 'a' as the same char first = Character.toLowerCase(s.charAt(0)); if (first >= 'a' && first <= 'z') { beginCounts[first - 'a']++; } } for (String s : str) { if (s == null || s.isEmpty()) continue; char last = Character.toLowerCase(s.charAt(s.length() - 1)); if (last >= 'a' && last <= 'z') { endCounts[last - 'a']++; } } // Merge int[] count = new int[26]; for (int i = 0; i < 26; i++) { count[i] = beginCounts[i] + endCounts[i]; } return count; }
Key Takeaways
- Always validate your input strings to avoid
NullPointerExceptionorStringIndexOutOfBoundsException. - Merge by adding values at the same index in both arrays—each index represents a unique character.
- Adjust the array size based on the character set you care about (ASCII vs. only letters) to save memory.
内容的提问来源于stack exchange,提问作者BJulie

