//求峰值privatestaticvoidgetPeakValue(int[] a){List list =newArrayList<>();for(int i =1; i < a.length -1; i++){if(a[i]> a[i -1]&& a[i]> a[i +1]){
list.add(a[i]);}}System.out.print("峰值为:");
list.forEach(l ->System.out.print(l +" "));}
2、自定义倒序
//自定义倒序算法publicstaticvoidreverseOrder(int[] a){int[] b =newint[a.length];for(int i = a.length -1, j =0; i >=0; i--, j++){
b[j]= a[i];}System.out.print("倒序后:");for(int i =0; i < b.length; i++){System.out.print(b[i]+" ");}}
3、冒泡排序
//冒泡排序publicstaticvoidbubbleSorting(int[] a){for(int j =0; j < a.length -1; j++){for(int i =0; i < a.length -1; i++){if(a[i]> a[i +1]){//中间量int flag = a[i];
a[i]= a[i +1];
a[i +1]= flag;}}}System.out.print("冒泡排序后:");for(int i : a){System.out.print(i+" ");}}
//求1-18之间的偶数为(用标记标签跳出for循环)privatestaticvoidoutFor(){List<Integer> list =newArrayList<>();boolean flag =true;while(flag){int n =20;A:for(int i =1; i < n; i++){if(i %2==0){
list.add(i);continue;}//i=18的时候,跳出循环if(i ==18){breakA;}}
flag =false;}Iterator<Integer> iterator = list.iterator();System.out.print("1-18之间的偶数为:");while(iterator.hasNext()){System.out.print(iterator.next()+" ");}}