Java array Inversion

Source: Internet
Author: User

There is a need to reverse an array and replace the data processing of the array smaller than 0 with 0.

Public class Test5 {public static void main (String [] args) {int [] array = new int [] {1, 3,-1, 5,-2}; System. out. println ("original array:"); for (int I = 0; I <array. length; I ++) {System. out. print (array [I] + "");} int [] newarray = getArray (array); // process data as small as 0 and process it as 0 for (int I = 0; I <newarray. length; I ++) {if (newarray [I] <0) {newarray [I] = 0 ;}} System. out. println ("\ n in reverse order and the processed array is:"); for (int j = 0; j <newarray. length; j ++) {System. out. print (newarray [j] + "");}} /*** process array inversion * @ param array * @ return */public static int [] getArray (int [] array) {int t [] = new int [array. length]; // open up a new array int count = t. length-1; for (int x = 0; x <t. length; x ++) {t [count] = array [x]; // reverse the array count --;} return t ;}}

In fact, the most critical step is

For (INT x = 0; x <t. length; X ++) {T [count] = array [X]; // array inversion count --;}

Record small knowledge for easy query.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.