数组1

来源:互联网 发布:货运数据 量化投资 编辑:程序博客网 时间:2024/05/17 19:59
import java.util.Arrays;/** * 数组使用 * - 排序 * - 拷贝 *  * @author admin * */public class ArrayUse {public void init() {/* * 数组排序 */int[] nums = new int[10];//随机数//System.out.println((int)(Math.random() * 2001)); for(int m = 0; m < nums.length; m++) {nums[m] = (int)(Math.random() * 20);}for(int num : nums) {System.out.print(num + "\t");}System.out.println();/* * 排序算法   * 冒泡 - 相邻比较替换 *///for(int i = 1; i < nums.length; i++) {//for(int j = 0; j < nums.length - i; j++) {//if(nums[j] < nums[j + 1]) {//int temp = nums[j];//nums[j] = nums[j + 1];//nums[j + 1] = temp;//}//}//}////for(int num : nums) {//System.out.print(num + "\t");//}//System.out.println("\n---------------------------------------------------");/* * 选择排序 *- 比较替换 *///for(int i = 1; i < nums.length; i++) {//for(int j = 0; j < i; j++) {//if(nums[i] > nums[j]) {//int temp = nums[j];//nums[j] = nums[i];//nums[i] = temp;//}//}//}//for(int num : nums) {//System.out.print(num + "\t");//}//System.out.println("\n---------------------------------------------------");/* * 数组排序  *  Arrays  此类包含用来操作数组(比如排序和搜索)的各种方法 *sort(array) * binarySearch(array, element) *如果它包含在数组中,则返回搜索键的索引;否则返回 (-(插入点) - 1)。 */Arrays.sort(nums);for(int num : nums) {System.out.print(num + "\t");}System.out.println("\n---------------------------------------------------");//字母之间排序 自然顺序 大写在前String[] strs = {"aa", "aA", "Aa"};Arrays.sort(strs);for(String str : strs) {System.out.print(str + "\t"); }System.out.println("\n---------------------------------------------------");int a = Arrays.binarySearch(strs, "Aa");int b = Arrays.binarySearch(strs, "aa");System.out.print(a);//0System.out.print(b);//-4/* * 跳水10个 打分 去除最低、最高 -- 平均值 */double[] scores = {8, 9, 9.5, 8.6, 8.5, 9.4, 9.8, 7.6, 8.2, 8.8};Arrays.sort(scores);double sum = 0;for(int i = 1; i < scores.length - 1; i++) {sum += scores[i];}System.out.println("此跳得分: " + sum / (scores.length - 2));/** * 数组拷贝 * 数组一旦定义大小以后 无法改变 *///Student[] students = new Student[20];//  students = new Student[25];//统计考试成绩 20分试卷 地上丢了8份试卷int[] ss = new int[20];for(int i = 0; i < ss.length; i++) {ss[i] = 80 + i;}int[] ss_new = new int[28];System.arraycopy(ss, 0, ss_new, 0, ss.length);ss_new[20] = 100;ss_new[27] = 100;for(int s : ss_new) {System.out.print(s + "  ");}} public static void main(String[] args) {new ArrayUse().init();}}
<pre name="code" class="java">/** * 数组 *  * @author admin * */public class Array {/* * 数组 * 数组是一种数据结构、是多个相同类型数据的组合,实现对这些数据的统一管理 *  *数组也为对象--数组属引用类型,数组中的每个元素相当于该对象的成员变量 *可以创建基本类型和引用类型数组 */public void init() {/* * 数组声明    * type[] name;  == type name[]; */int[] nums;String[] names;  //String names[];Student[] students;/* * 数组初始化 * 动态初始化 * 分配空间  new type[num]; * 下标 动态赋值 * arrayName[下标] = value;   下标 0 ~ arrayName.length-1 * 静态初始化 */nums = new int[5];nums[0] = 1;nums[1] = 2;//nums[10] = 11;  //ArrayIndexOutOfBoundsException/* * 数组是引用类型,它的元素相当于类的成员变量,因此数组对象一经创建,其中的 *每个元素也被按照成员变量同样的方式被隐式初始化 *///System.out.println(nums[1]);//System.out.println(nums[3]);//System.out.println(nums[9]);for(int num:nums){System.out.println(num);}names = new String[3];names[0] = "John";names[1] = "Leo";names[2] = "Kitty";//增强for循环for(String name : names) {System.out.println(name); }//基本循环结构for(int i = 0; i < names.length; i++) {System.out.println(names[i]); }students = new Student[30];students[0] = new Student();/** * 静态初始化  * array = {...};  -- 必须在声明时直接赋值 *  array = new type[]{...}; */int[] scores = {80, 88, 99};System.out.println(scores.length); System.out.println(scores[2]);String[] movies = new String[]{"电影1"}; movies = new String[]{"电影2", "电影3"};for(String movie : movies) {System.out.println(movie);}}public static void main(String[] args) {new Array().init();}}


                                             
0 0
原创粉丝点击