1、定义一个数组,获取数组中的数据中的最大值和最小值以及数组中的偶数和奇数的个数,具体实现代码:
package com.org.improve.book.test.study; public class bubble { /** * ,冒泡排序 */ public static void main(String[] args) { int arr[]={2,-19,0,12,-7}; int temp; int odd=0; int even=0; //循环的次数为n-1 for (int i = 0; i < arr.length-1; i++) { //比较的次数为n-1-i for (int j = 0; j < arr.length-1-i; j++) { if (arr[j]>arr[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } for (int i = 0; i < arr.length; i++) { if (arr[i]%2==0) { even++; }else { odd++; } } System.out.println("数组的偶数个数为:"+even); System.out.println("数组的奇数个数为:"+odd); System.out.println("数组中最小的值是:"+arr[0]); System.out.println("数组中最大的值是:"+arr[arr.length-1]); /*for (int i:arr) { System.out.print(i+" "); }*/ for (int i = 0; i < arr.length; i++) { System.out.print(arr[i]+" "); } } }
2、定义一个数组,将数组中是0的数据删除不要,重新组建新的数组。具体的实现代码:
package com.org.improve.book.test.study; public class lzwCode { public static void main(String[] args) { int tem=0; int oldArr[]={-12,0,5,23,0,-17,8,0,12,-5,0}; for (int i = 0; i < oldArr.length; i++) { if (oldArr[i]==0) { tem++; } System.out.print(oldArr[i]+" "); } System.out.println(" "); int newArr[]=new int[oldArr.length-tem]; //System.out.println("0出现的个数为"+tem+" "); //System.out.println("数组的长度为:"+oldArr.length); //System.out.println(oldArr.length-tem); 除过0剩下的数组长度 int k=0; for (int i = 0; i < oldArr.length; i++) { if (oldArr[i]!=0) { newArr[k++]=oldArr[i]; //System.out.print(oldArr[i]+" "); } } for(int j=0;j<newArr.length;j++){ System.out.print(newArr[j]+" "); } } }