Invert array output
Preface:This method comes from the B station UP master
question:Invert the numbers 1,2,3,4,5 output 5,4,3,2,1
Input parameters: 1, 2, 3, 4, 5
Code Example
public static void main(String[] args) { //parameter int[] array = {1,2,3,4,5}; //Calling method int[] a = a(array); //The result output of the call (a); } public static int[] a(int[] array){ //Declare a dynamic initialization array; receive inverted array int[] res = new int[]; /* Loop through the parameter array i: For normal initialization statement, we know the array size and end with 0. j: Find the maximum length of dynamically initialized array i: Step size increases by itself every time j: Step length is self-decreasing */ for (int i = 0,j=-1; i < ; i++,j--) { /* Parameter array value Assign to declare dynamically initialize the array The first time loop i:0 j:4 and so on i:1 j:3 */ res[j] = array[i]; } return res; }
Output result: 5, 4, 3, 2, 1
Java implements reverse string output
【Title Description】
Write a function to store an input string in reverse order, and input and output the inverted string in the main function.
【enter】
One line of characters
【Output】
String after reverse order
【Sample input】
123456abcdef
【Sample output】
fedcba654321
【Problem Solving Ideas】
First, define two String strings a and b, where a is used to store the string in the positive order and b is used to store the reverse order string. Then define a character array c, and then call the toCharArray() method in the class library to convert the string into character numbers. Then, use a loop to assign the character array inversely to the string b, and then output the string b.
【source code】
import ; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(); String a = (); String b = ""; char[] c = (); for(int i = -1;i >= 0;i--) { b = b + c[i]; } (b); (); } }
Summarize
This is the end of this article about Java inverting array output. For more related Java inverting array output, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!