package com.atguigu.java; public class code01 { public static void main(String[] args) { double[] list1 = new double[]{1, 2, 3, 4, 5}; //1数组的遍历 for (int i = 0; i < list1.length; i++) { System.out.print(list1[i] +" "); } //2.1找出数组中的最大值 double max = list1[0]; int indexOfMax = 0; for (int i = 1; i < list1.length; i++){ if (list1[i] > max){ max = list1[i]; indexOfMax = i; } } System.out.println(max);//5.0 System.out.println(indexOfMax);//4 //2.2找出数组中的最小值 double min = list1[0]; int indexOfMin = 0; for (int i = 1; i < list1.length; i++){ if (list1[i] < min){ min = list1[i]; indexOfMin = i; } } System.out.println(min);//1.0 System.out.println(indexOfMin);//0 //3打乱数组中的值 for (int i = list1.length-1; i >0; i--){ //随机生成一个下标 0 <= j <= i int j = (int)(Math.random()*(i-0+1)); //交换list1[i] 和 list[j] 的值 double temp = list1[i]; list1[i] = list1[j]; list1[j] = temp; } //遍历数组 for (double e: list1){ System.out.print(e + ","); } //4向前(向后)依次移动 double t = list1[0]; for (int i = 1; i < list1.length; i++){ list1[i-1] = list1[i]; } list1[list1.length - 1] = t; System.out.println(); //5复制数组内容 double[] list2 = new double[5]; //5.1用循环语句逐个复制 for (int i = 0; i < list1.length; i++){ list2[i] = list1[i]; } //5.2使用Sysyem类中的静态方法arraycopy System.arraycopy(list1,2,list2,0,3); //遍历list2 for (double e: list2){ System.out.print(e + ","); } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)