赞
踩
- public static void main(String[] args) {
- //声明一个数组
- int[] arr = new int[]{10,20,30,40,50,60,70,89,90};
- //反转之前遍历
- for (int i = 0; i < arr.length; i++) {
- System.out.print(arr[i]+"\t");
- }
- //方式1反转操作
- for (int i = 0; i < arr.length/2; i++) {
- //交互arr[i]与arr[arr.length-1-i]位置
- int temp = arr[i];
- arr[i] = arr[arr.length-1-i];
- arr[arr.length-1-i]=temp;
- }
- //方式2反转操作
- for (int i = 0, j =arr.length-1;i<j; i++,j--) {
- //交互arr[i]与arr[j]位置
- int temp = arr[i];
- arr[i] =arr[j];
- arr[j] = temp;
- }
- System.out.println();
- //反转之后遍历
- for (int i = 0; i < arr.length; i++) {
- System.out.print(arr[i]+"\t");
- }
- }
- /**
- *定义一个int型的以为数组,包含10个元素,分别赋一些随机整数,
- * 然后求出所有元素的最大值、最小值、总和、平均值
- * @param args
- */
- public static void main(String[] args) {
- //动态初始化方式创建数组
- int[] arr = new int[10];
- //通过循环给数组元素赋值
- for (int i = 0; i < arr.length; i++) {
- arr[i] =(int)(Math.random()*(99-10+1))+10;
- System.out.print(arr[i]+"\t");
- }
- System.out.println();
- //求最大值
- int max = arr[0];
- for (int i = 1; i < arr.length; i++) {
- if(max<arr[i]){
- max = arr[i];
- }
- }
- System.out.println("最大值为:"+max);
- //求最小值
- int min = arr[0];
- for (int i = 1; i < arr.length; i++) {
- if(min>arr[i]){
- min = arr[i];
- }
- }
- System.out.println("最小值为:"+min);
- //求总和
- int sum = 0;
- for (int i = 0; i < arr.length; i++) {
- sum += arr[i];
- }
- System.out.println("总和:"+sum);
- //求平均值
- int avgValue = sum/arr.length;
- System.out.println("平均值:"+avgValue);
-
- }
- /**
- *定义一个数组,求去掉最高分,去掉最低分后的平均分
- * @param args
- */
- public static void main(String[] args) {
- int[] scores = {5,4,6,8,9,0,1,2,7,3};
- //声明3个特征值
- int sum = 0;
- int max = scores[0];
- int min = scores[0];
-
- for (int i = 0; i < scores.length; i++) {
- sum += scores[i];
- //用于获取做高分
- if(max < scores[i]){
- max =scores[i];
- }
- //用于获取最低分
- if(min > scores[i]){
- min =scores[i];
- }
- }
- int avg = (sum-max-min)/(scores.length-2);
- System.out.println("去掉一个最高分和去掉一个最低分之后的平均分:" + avg);
-
- }
- /**
- *数组的扩容 定义一组数组 扩容1倍容量,并将10,20,30三个数据添加到数组中
- * @param args
- */
- public static void main(String[] args) {
- int[] arr = {1,2,3,4,5};
- //扩容1倍容量
- // int[] newArr = new int[arr.length*2];
- //或者
- int[] newArr = new int[arr.length << 1];
- //将原有数组中的元素复制到新的数组中
- for (int i = 0; i < arr.length; i++) {
- newArr[i] = arr[i];
- }
- //将10,20,30添加到新的数组中
- newArr[arr.length]=10;
- newArr[arr.length+1]=20;
- newArr[arr.length+2]=30;
- //将新的数组的地址赋值给原有的数组变量
- arr = newArr;
- //遍历查看
- for (int i = 0; i < arr.length; i++) {
- System.out.print(arr[i]+"\t");
- }
- }
- /**
- *数组的缩容
- * @param args
- */
- public static void main(String[] args) {
- int[] arr = {1,2,3,4,5};
- //删除下标为4
- int deleteIndex = 4;
- //方式1 不新建数组 修改下标4 之后的数租的位置(下标)
- for (int i = deleteIndex; i < arr.length - 1; i++) {
- arr[i] = arr[i+1];
- }
- //修改最后元素设置为默认值
- arr[arr.length-1] = 0;
- //遍历缩容后的数组
- for (int i = 0; i < arr.length; i++) {
- System.out.print(arr[i]+"\t");
- }
- }
- /**
- *数组的线性查找(必须排好序)
- * 优点: 简单
- * 缺点:效率低
- * @param args
- */
- public static void main(String[] args) {
- int[] arr = {35,6546,445,5,515,63,65,68};
- //定义一个要查找的目标
- int target = 15;
- //定义一个布尔类型的临时变量若进去for 循环中的if 后改为false
- boolean isFlag = true;
- //查找方式
- for (int i = 0; i < arr.length; i++) {
- if(target==arr[i]){
- System.out.println("找到了"+target+"对应的位置"+i);
- isFlag=false;
- break;
- }
- }
- if(isFlag){
- System.out.println("未找到");
- }
- }
- /**
- *数组的二分查找(必须排好序)
- * 优点: 效率高
- * 缺点:比顺序查找难一点,数组必须有序
- * @param args
- */
- public static void main(String[] args) {
- int[] arr = {2,3,4,5,6,8,9,10,15,18,19};
- //定义一个要查找的目标
- int target = 15;
- int head = 0;//默认的首索引
- int end = arr.length-1;//默认的尾索引
- //判断是否找到了指定元素
- boolean isFlsg = false;
- while(head <= end){
- int middle = (head+end);
- if(target == arr[middle]){
- System.out.println("找到了"+target+"位置在"+middle);
- isFlsg = true;
- break;
- }else if( target > arr[middle]){
- head = middle +1;
- }else {
- end = middle-1;
- }
- }
- if(!isFlsg){
- System.out.println("未找到");
- }
- /**
- *数组的冒泡排序
- * @param args
- */
- public static void main(String[] args) {
- int[] arr = {2,5,52,5689,36,36,18,19};
- //排序之前进行遍历
- for (int i = 0; i < arr.length; i++) {
- System.out.print(arr[i]+ "\t");
- }
- //冒泡排序实现从小到大排序
- for (int j = 0; j < arr.length-1; j++) {
- for (int i = 0; i < arr.length-1-j; i++) {
- if(arr[i]>arr[i+1]){
- //交互arr[i]与arr[i+1]位置
- int temp = arr[i];
- arr[i] = arr[i+1];
- arr[i+1] = temp;
- }
- }
- }
-
- System.out.println();
- //排序之后进行遍历
- for (int i = 0; i < arr.length; i++) {
- System.out.print(arr[i]+ "\t");
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。