In this Java example we will see how to Reverse String Array. the example below shows how to sort an array of String in Java using Arrays and Collections classes.
There are basically two ways we can Reverse String Array, first is to use temporary array and manually loop over the elements of an Array and swap them or second way is to use Arrays and Collections classes. This example uses the second approach.
/** * Created by codebind.com. */ import java.util.Collections; import java.util.List; import java.util.Arrays; public class ReverseStringArray { public static void main(String args[]){ //String array String[] strNames = new String[]{"Max", "Tom", "Mark", "John"}; //create a list from String array List<String> list = Arrays.asList(strNames); //reverse the list using Collections.reverse method Collections.reverse(list); //convert the list back to String array strNames = (String[]) list.toArray(); System.out.println("reversed String array "); //print the reversed String array for(int i=0; i < strNames.length; i++){ System.out.println(strNames[i]); } } } /* Output : reversed String array John Mark Tom Max */
- Java Simple Programs And Examples
- Java Example – Java Hello World Example
- Java Example – Math and Arithmetic Operators in Java
- Java Example – Variables and Types in Java
- Java Example – Scanner class and Getting User Input using Java
- Java Example – The If-Else If Statement, Nested If Statements, Logical Operators
- Java Example – Arrays in Java Example
- Java Example – ListIterator in Java Example
- Java Example – How to get current timestamp using Java
- Java Example – HashSet in Java with Example
- Java Example – How to read file in Java using BufferedReader
- Java Example – How to get Current Directory in Linux/Windows
- Java Example – Program to reverse an array or string
- Java Example – Sort String Array
- Java Example – Comparing two strings
- Java Example – String concatenation (join strings)
- Java Example – Java String Contains example
- Java Conversion
- Java Example – Convert String to int
- Java Example – Convert Date to String
- Java Example – Convert String to Character Array
- Java Example – Convert into string
- Java Example – Convert ArrayList to String Array
- Java Example – Convert Char Array To String
- Java Example – String Array To List
- Java Sorting algorithms & Techniques
- Java Example – Bubble Sort Algorithm
- Java Example – Insertion Sort Algorithm
- Java Example – Selection Sort Algorithm
- Java Example – Quick Sort Algorithm
- Java Example – Merge Sort Algorithm
- Java Handling Files
- Java I/O – Check If File Path Absolute or not
- Java I/O – Get file name and file path
- Java I/O – Get parent directory of the file or directory
- Java I/O – How to write to a file using BufferedWriter
- Java I/O – How to write to a file using FileOutputStream
- Java I/O – Check file permission and Set file permission
- Java I/O – Read File Using Java BufferedInputStream
- Java I/O – How to create a file in Java
Leave a Reply