通过两个嵌套循环,实现将数组中的元素倒置。Java实现如下:
package test; import java.util.Scanner; public class test15 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub //Scanner scn =new Scanner(System.in); int[] in ={3,1,2,9,4}; sort(in); //System.out.println(String.valueOf(in)); for(int x:in){ System.out.print(x); } } public static void sort(int[] in){ int temp; for(int i=in.length-1; i>=0; i--){ for(int j=0; j<=i-1; j++){ temp =in[j]; in[j]=in[j+1]; in[j+1]=temp; } } } }输出结果:
49213原文链接:https://www.f2er.com/javaschema/284429.html