数组逆序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | /** * 数组逆序: * 将一个数组中的索引进行逆序排序显示 * 操作步骤: * 1、建立一个数组 * 2、对数组进行遍历 * 3、对数组中国索引进行互换 * 4、遍历数组显示互换之后数据 */ public class LoopTest5 { public static void main(String[] args) { int [] arr = { 123 , 321 , 456 , 324 , 567 , 543 , 190 , 987 }; //调用数组逆序方法 test1(arr); //看到数组的元素,遍历 runTest(arr); } /** * 定义方法:数显数组逆序 * 1、返回值,无返回值 * 2、参数,数组即使参数 * **/ public static void test1( int [] arr){ //for的第一项,定义2个变量,最后,两个变量++ -- for ( int min = 0 , max = arr.length- 1 ; min<max ; min++, max--){ //定义一个中间变量,保存min索引 int tem = arr[min]; //max索引上的元素,赋值给min索引 arr[min] = arr[max]; //临时变量,保存的数字,赋值到max索引 arr[max] = tem; } } //按照[]方式输出遍历数组数信息 public static void runTest( int [] arr){ System.out.print( "[" ); //for循环 for ( int i = 0 ; i < arr.length; i++){ //判断数组是否是最后一位,是则打印],否则打印, if (i==arr.length- 1 ){ System.out.print(arr[i] + "]" ); } else { System.out.print(arr[i] + "," ); } } } } |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步