Java char数组空索引检测异常问题求助
Let's break down what's going on here and fix your problem step by step—your empty index check isn't actually failing, the issue lies in your loop logic and variable management.
First: Your "Empty Index" Check Works Fine
When you initialize a char[] with new char[N], every unused element defaults to '\u0000' (the null character). Your check getColorAt(f, arrayOfCorrect) != c1 (where c1 = '\u0000') is correctly identifying filled vs. empty positions. The confusion comes from broken counting logic elsewhere in your code.
The Real Problems Causing Wrong Output
Counter Never Resets
Yourcountervariable accumulates across all loops without being reset. This leads to false triggers for yourif (counter == 4)condition, which is not how you should count "close" matches anyway.Nested Loop Logic Is Flawed
The loopfor (int n = -i; n < 4 - i; n++)creates redundant checks and doesn't account for already matched "correct" elements, leading to duplicate or incorrect counting.Incorrect "Close" Match Condition
Checking ifcounter == 4to incrementclosemakes no logical sense—closeshould count elements that exist in both arrays but are in the wrong position, not when an arbitrary counter hits 4.
Fixed Code with Clear Explanations
Here's a revised version of your code that correctly counts exact ("correct") and misplaced ("close") matches, with proper empty index handling:
public class TestArrayChecker { public static void main(String[] args) { char array1[] = new char[] {'F', 'P', 'S', 'R'}; char array2[] = new char[] {'S', 'P', 'O', 'R'}; char emptyChar = '\u0000'; char arrayOfCorrect[] = new char[array1.length]; int correct = 0; int close = 0; int correctIndex = 0; // First pass: count exact position matches (correct) for (int i = 0; i < array1.length; i++) { if (array1[i] == array2[i]) { correct++; arrayOfCorrect[correctIndex] = array1[i]; correctIndex++; } } // Create copies to mark used elements (avoid duplicate counting) char[] tempArray1 = array1.clone(); char[] tempArray2 = array2.clone(); // Mark exact matches as "used" so we don't count them again for close matches for (int i = 0; i < tempArray1.length; i++) { if (tempArray1[i] == tempArray2[i]) { tempArray1[i] = emptyChar; tempArray2[i] = emptyChar; } } // Second pass: count misplaced matches (close) for (int i = 0; i < tempArray1.length; i++) { if (tempArray1[i] == emptyChar) { continue; // Skip already matched correct elements } for (int j = 0; j < tempArray2.length; j++) { if (tempArray2[j] == emptyChar) { continue; // Skip already used elements } if (tempArray1[i] == tempArray2[j]) { close++; tempArray2[j] = emptyChar; // Mark as used to avoid duplicates break; } } } // Output results System.out.println("Correct matches: " + correct); System.out.println("Close matches: " + close); System.out.print("Correct elements in position: "); for (char c : arrayOfCorrect) { if (c != emptyChar) { System.out.print(c + " "); } } System.out.println(); } public static char getColorAt(int index, char array[]) { return array[index]; } }
Key Improvements:
- Uses cloned arrays to mark already matched elements, preventing duplicate counting
- Separates logic for exact vs. misplaced matches for clarity
- Properly skips empty/unused array positions using your existing
'\u0000'check - Eliminates the confusing
countervariable and replaces it with logical match counting
Why Your Original Code Seemed Broken
Your empty index check was working, but the unreset counter and flawed loop structure created misleading output. For example, your console never showed "We got here!" because the loop logic never reached the empty positions before hitting the incorrect counter == 4 condition.
内容的提问来源于stack exchange,提问作者Shodmoth

