Java中如何拆分ArrayList中的两个char数组并按ASCII排序?
Hey there! Let's work through your problem step by step—splitting the arrays from the ArrayList and sorting them as you need.
char Arrays from the ArrayList Since you added randChar1 first and then randChar2 to the ArrayList, you can use the get(index) method to retrieve them directly. The index starts at 0 for the first element:
static void Sort(ArrayList<char[]> al3, char symA) { // Grab the two char arrays from the ArrayList char[] randChar1 = al3.get(0); // This is the first array you added char[] randChar2 = al3.get(1); // This is the second array you added // Rest of your code goes here... }
This works because ArrayList maintains the order of elements exactly as you added them.
For Ascending Order (randChar1)
Java's Arrays.sort() method handles char arrays perfectly for ascending ASCII sorting out of the box. Since char values are inherently compared by their ASCII codes, this will sort your array from the lowest to highest ASCII value:
// Sort randChar1 in ascending ASCII order Arrays.sort(randChar1); // Example: If randChar1 was ['C', 'A', 'B'], it becomes ['A', 'B', 'C']
Don't forget to import java.util.Arrays at the top of your file to use this method!
For Descending Order (randChar2)
There's no built-in Arrays.sort() for descending char arrays, but we have two simple ways to do this:
Option 1: Sort Ascending First, Then Reverse the Array
This is straightforward—sort the array normally, then flip the order with a helper method:
// First sort randChar2 in ascending order Arrays.sort(randChar2); // Then reverse it to get descending order reverseCharArray(randChar2); // Helper method to reverse a char array private static void reverseCharArray(char[] arr) { int start = 0; int end = arr.length - 1; while (start < end) { // Swap elements at start and end char temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } // Example: If randChar2 was ['a', 'c', 'b'], sorted ascending is ['a','b','c'], reversed becomes ['c','b','a']
Option 2: Use a Comparator with Character Wrapper Class
Since we can't use a Comparator directly on primitive char arrays, we can convert the array to Character (the wrapper class) and sort with a custom comparator:
// Convert char array to Character object array Character[] charObjArr = new Character[randChar2.length]; for (int i = 0; i < randChar2.length; i++) { charObjArr[i] = randChar2[i]; } // Sort in descending ASCII order using a lambda comparator Arrays.sort(charObjArr, (a, b) -> b - a); // Convert back to primitive char array if needed for (int i = 0; i < randChar2.length; i++) { randChar2[i] = charObjArr[i]; }
The lambda (a, b) -> b - a tells the sort to place the character with the higher ASCII value first.
Sort Method Putting it all together, here's what your complete Sort method could look like (using the reverse approach for simplicity):
import java.util.ArrayList; import java.util.Arrays; import java.util.Random; public class Test1 { public static void main(String[] args) { char[] randChar1 = new char[3]; char[] randChar2 = new char[3]; char sym = '<'; char[] charCombine = new char[6]; System.out.print("The string generated is: "); for (int i = 0; i < 3; i++) { Random r = new Random(); randChar1[i] = (char) (65 + r.nextInt(26)); System.out.print(randChar1[i]); randChar2[i] = (char) (97 + r.nextInt(26)); System.out.print(randChar2[i]); } System.out.println(); ArrayList<char[]> al = new ArrayList<>(); al.add(randChar1); al.add(randChar2); Sort(al, sym); } static void Sort(ArrayList<char[]> al3, char symA) { // Split the arrays char[] randChar1 = al3.get(0); char[] randChar2 = al3.get(1); // Sort randChar1 ascending Arrays.sort(randChar1); System.out.println("Ascending sorted randChar1: " + new String(randChar1)); // Sort randChar2 descending Arrays.sort(randChar2); reverseCharArray(randChar2); System.out.println("Descending sorted randChar2: " + new String(randChar2)); } private static void reverseCharArray(char[] arr) { int start = 0; int end = arr.length - 1; while (start < end) { char temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } }
内容的提问来源于stack exchange,提问作者Khairul Hahaz

