Reversing elements of an array in Java
Reversing elements of an array in Java. In this Article i will show you how to reverse the Element in Java , and print it out .
In the sample program below, I will create a program to request the user to input a String Value , and then i will pass the User String input into the Function and split it into a separate single word or a single element inside an array.
Then i will grab the element value in the Array that i have just split , inside the For Loop. and pass the Value into a n Array List.The last step is to convert ArrayList<String>back to String[]array[].
Refer the Step by step below on Reversing elements of an Array in Java
- First I have created a class call “reverseString”.
- As in this program we are using the input Scanner object to request the user to input their string value ,we will need to import java.util.*;
- The next step is to create the reverse method. First we will need to declare an Array List” ArrayList
“ i named it inputValue1 - Then the Method will get the user input through String inputString inside the reverseText(String inputString)method
- After the Method receive the String from the User , it will split it into individual string element array and then passed it into String[]reverseInput
- Subsequently i created a for loop to loop back ward of every single element inside the “reverseInput” array.
- During the Looping process , the program will pass the value inside“reverseInput” into the ArrayList inputValue1.add(reverseInput[i]);
- After that i need to convert the ArrayList back to Normal Array and pass all the element values into a new Array i name it reverses
- The Final step is to return the Value through “reverses”
- Lets go back to our main program,create two new Variable called String Input_1; static String [] reverseOuput ; that we will need to pass through some value later in the program.
- Create a new Scanner object i called it “inputScan”
- Create a Print out to ask the User to input a value
- Scan the User input and call the reverseText()method that we created beforehand
- Pass the User Input into the Method Parentheses, and then assign the return value to the static String [] reverseOuput ; variable
- Create a For Loop to loop through every single Element inside the Array and print all the Element out one by one
- Always remember to close your scanner
package staticpack; import java.util.*; public class reverseString { // Public Scanner Object static String Input_1; static String [] reverseOuput ; public static void main(String[] args) { // Declare a Scanner Object Scanner inputScan = new Scanner(System.in); // Require the User to input a Value. System.out.println("Please enter the Item Name"); Input_1 = inputScan.nextLine(); reverseOuput =reverseText(Input_1); //Print out all the arrays for ( int i=0 ;i<reverseOuput.length;i++){ System.out.println(reverseOuput[i] ); } System.out.print(reverseOuput.length); inputScan.close(); } public static String[] reverseText(String inputString){ ArrayList<String> inputValue1= new ArrayList<String>(); String[]reverseInput = inputString.split("\\s+"); int arrayLength = reverseInput.length; for (int i = arrayLength-1;i>=0;i-- ) { inputValue1.add(reverseInput[i]); } String reverses[] = new String[inputValue1.size()]; reverses= inputValue1.toArray(reverses); return reverses ; } }
or you can use the Sample Program below
package reverveInput; import java.util.*; public class reverseInputElement { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Please enter a String :"); String original = scan.nextLine(); while(original.isEmpty()|| original==null){ System.out.println(" Please enetr a string null String are not accepted"); original = scan.nextLine(); } scan.close(); reverseInputElement reversechar = new reverseInputElement(); String reverseCharacter = reversechar.reverseChars(original); System.out.println(reverseCharacter); } private String reverseChars(String originalString){ String reverses =""; for(int i =originalString.length()-1; i >=0; i--){ reverses= reverses + originalString.charAt(i); } return reverses; } }
Well that wraps up the topic on Reversing elements of an array in Java Check out Static Rules and How to Access a Static Method in Java here.
If you happen to still have any problem about JAVA, refer here