数组的一些应用

来源:互联网 发布:怎么编写php木马 编辑:程序博客网 时间:2024/06/06 05:24

数组的一些应用

1.实现数组的倒叙、字符转向数组、数组的冒泡排序

public class ArrayTest {public static void main(String[] args){int[] test = {1,2,4,5,7};//实现数组倒叙for(int i:test){System.out.print(i+" ");}System.out.print("\n");test = ArrayTest.reserve(test);for(int i:test){System.out.print(i+" ");}//实现字符串转向数组String str = "helloworld";char[] data = str.toCharArray();for(int x=0;x<data.length;x++){System.out.print(data[x]+" ");data[x] -= 32;System.out.print(data[x]+" ");}System.out.print(new String(data));//冒泡排序int[] b = {26,15,29,66,99,88,36,77,111,1,6,8,8};for(int i=0;i<b.length-1;i++){for(int j=0;j<b.length-i-1;j++){if(b[j]>b[j+1]){int temp = b[j];b[j] = b[j+1];b[j+1] = temp;}}System.out.print("第"+(i+1)+"次的结果:");for(int k=0;k<b.length;k++){System.out.print(b[k]);}System.out.println();}System.out.print("最后的结果");for(int i:b){System.out.print(i+" ");}}public static int[] reserve(int[] arr){int[] result = new int[arr.length];for(int i=0,j=result.length-1;i<arr.length;i++,j--){result[j] = arr[i];}return result;}}