数组元素反转

1581152650601

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
49
50
51
52
53
54
55
56
57
58

public class test019 {
public static void main(String[] args) {
int[] array = {1,234,12,4,124,21,315,15,1235,461,235,1,34632,135,124151,32515,612,351,35};

//数组排序
arraySort(array);
//数组遍历输出
arrayOut(array);
//数组反转
funFZ(array);
//数组遍历输出
arrayOut(array);


}

//冒泡法排序
public static void arraySort(int[] array)
{
for(int i = 0;i < array.length - 1;i++)
{
for(int j = 0;j < array.length - (i + 1);j++)
{
if(array[j] < array[j + 1] )
{
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}

//数组遍历输出
public static void arrayOut(int[] array)
{
for (int i : array) {
System.out.print(i + " " );
}
System.out.println("");
System.out.println("===================================");
}

//数组反转
public static void funFZ(int[] array)
{
int max = array.length - 1;
int min = 0;

for(;min < max;min++,max--)
{
int temp = array[min];
array[min] = array[max];
array[max] = temp;
}
}
}