gpt-4-0314· OpenAI
WildChat-4.8M (AI2)
In Java, I have an array of strings. Generate a method that takes that array of strings…
- User
In Java, I have an array of strings. Generate a method that takes that array of strings as input and sorts it in ascending order.
- Assistant
You can use the
Arrays.sort()method from Java's standard library to sort the array of strings in ascending order. Here's a method that takes an array of strings as input and sorts it:import java.util.Arrays; public class Main { public static void main(String[] args) { String[] arr = {"apple", "banana", "kiwi", "grape", "mango"}; sortArray(arr); for(String s : arr) { System.out.println(s); } } public static void sortArray(String[] array) { Arrays.sort(array); } }This code first imports the necessary
java.util.Arrayspackage for theArrays.sort()method. ThesortArray()method takes an array of strings as input; after it's sorted, you can then print out the sorted array in the main method. This example sorts the input array in ascending order.