音乐图书馆网站建设,在线设计平台的发展趋势,海口柏特网络科技有限公司,网页编辑工具wordpress目录
一、什么是Java中的数组#xff1f;
二、数组有哪些常见的操作#xff1f;
三、数组的五种赋值方法和使用方法
声明数组
声明数组并且分配空间
声明数组同时赋值(1)
声明数组同时赋值(2)
从控制台输入向数组赋值
四、求总和平均
五、求数组中最大值最小值
六… 目录
一、什么是Java中的数组
二、数组有哪些常见的操作
三、数组的五种赋值方法和使用方法
声明数组
声明数组并且分配空间
声明数组同时赋值(1)
声明数组同时赋值(2)
从控制台输入向数组赋值
四、求总和平均
五、求数组中最大值最小值
六、对数组进行升序排序
六、不调用方法进行数组排序
七、Arrays 类常用方法 一、什么是Java中的数组 数组是一种数据结构可以存储多个相同类型的元素在Java中数组是一种对象它可以存储基本数据类型和对象类型。 二、数组有哪些常见的操作 创建数组使用new操作符创建一个新的数组对象。访问元素使用索引访问数组中的元素。遍历数组使用循环结构遍历数组中的所有元素。复制数组使用System.arraycopy()方法或Arrays.copyOf()方法将一个数组复制到另一个数组中。排序数组使用Arrays.sort()方法对数组进行排序。搜索数组使用Arrays.binarySearch()方法在一个已排序的数组中搜索一个元素。三、数组的五种赋值方法和使用方法
声明数组
int[] arr;
//开辟三个空间
arr new int[3];
//向数组中赋值0、1、2是下标
arr[0] 30;
arr[1] 97;
arr[2] 65;
声明数组并且分配空间
int[] arr new int[3];
//开辟三个空间
//向数组中赋值
arr[0] 30;
arr[1] 97;
arr[2] 65;
声明数组同时赋值(1)
int[] arr new int[]{30, 97, 65};
声明数组同时赋值(2)
int[] arr {30, 97, 65};
从控制台输入向数组赋值
int[] arr new int[3];
System.out.println(向数组中存入三个数据);
//arr.length是数组的长度
for (int i 0; i arr.length; i) {
//i是下标 arr[i] input.nextInt();}
//把数组中的值输出
for (int i 0; i arr.length; i) {
//输出数组中的值 System.out.println(arr[i]);
}
四、求总和平均
int i 0; int zong 0;
int[] arr new int[3];
for (i 0; i arr.length; i) {System.out.print(输入第 (i 1) 位同学的成绩); arr[i] input.nextInt();zong zong arr[i];
}
System.out.println(i 位同学的总分是 zong 平均分是 zong * 1.0 / arr.length);
五、求数组中最大值最小值
int[] arr { 90, 100, 98, 67, 45, 79 };int max arr[0];int min arr[0];for (int i 1; i arr.length; i) {if (arr[i] max) {max arr[i];
}if (arr[i] min) {min arr[i];
}
}
System.out.println(最大值为 max); System.out.println(最小值为 min);
六、对数组进行升序排序
数组升序输出int[] arr1 { 90, 100, 98, 67, 45, 79 }; Arrays.sort(arr1);for (int i 0; i arr1.length; i) { System.out.println(arr1[i]); } 数组降序输出
int[] arr2 {90, 100, 98, 67, 45, 79};
Arrays.sort(arr2);
for (int i arr2.length - 1; i 0; i--) { System.out.println(arr2[i]);
}
六、不调用方法进行数组排序
int[] array {10, 30, 17, 6, 29, 20};
int temp; for(int i 0;iarray.length -1;i){for (int j 0; j array.length - 1 - i; j) {if (array[j] array[j 1]) {//为降序为升序 temp array[j];array[j] array[j 1];array[j 1] temp;}}
}
for(int i :array){System.out.print(i \t);
}
七、Arrays 类常用方法
Arrays类位于 java.util 包中 import java.util.Arrays;
int[] array1 {10, 20, 16, 19, 5};int[] array2 {10, 20, 16, 19, 5};//boolean equals(array1, array2)比较两个数组是否相等
System.out.println(Arrays.equals(array1, array2));
//打印true //String toString(array)将数组转换成字符串显示值System.out.println(Arrays.toString(array1));
//打印[10, 20, 16, 19, 5] //void fill(array, val)将数组的所有值填充为val Arrays.fill(array2, 20); System.out.println(Arrays.toString(array2));//打印[20, 20, 20, 20, 20] //数组copyOf(array, newLength)将array复制成指定长度的新数组 int[] array3 Arrays.copyOf(array1, 10);
System.out.println(Arrays.toString(array3));
//打印[10, 20, 16, 19, 5, 0, 0, 0, 0, 0] //int binarySearch(array, key)在array数组中查找指定的值并返回其索引前提是数组是有序的 Arrays.sort(array1);
System.out.println(Arrays.binarySearch(array1, 16));
//打印2 文章转载自: http://www.morning.kqrql.cn.gov.cn.kqrql.cn http://www.morning.xdttq.cn.gov.cn.xdttq.cn http://www.morning.qbrs.cn.gov.cn.qbrs.cn http://www.morning.wjxyg.cn.gov.cn.wjxyg.cn http://www.morning.mhcys.cn.gov.cn.mhcys.cn http://www.morning.wjhpg.cn.gov.cn.wjhpg.cn http://www.morning.yxmcx.cn.gov.cn.yxmcx.cn http://www.morning.cwqrj.cn.gov.cn.cwqrj.cn http://www.morning.mtmnk.cn.gov.cn.mtmnk.cn http://www.morning.xmbhc.cn.gov.cn.xmbhc.cn http://www.morning.skbbt.cn.gov.cn.skbbt.cn http://www.morning.jhyfb.cn.gov.cn.jhyfb.cn http://www.morning.junmap.com.gov.cn.junmap.com http://www.morning.tlpsd.cn.gov.cn.tlpsd.cn http://www.morning.dzrcj.cn.gov.cn.dzrcj.cn http://www.morning.dkqyg.cn.gov.cn.dkqyg.cn http://www.morning.lpmdy.cn.gov.cn.lpmdy.cn http://www.morning.tslwz.cn.gov.cn.tslwz.cn http://www.morning.gbcxb.cn.gov.cn.gbcxb.cn http://www.morning.pcbfl.cn.gov.cn.pcbfl.cn http://www.morning.rwlnk.cn.gov.cn.rwlnk.cn http://www.morning.dbhnx.cn.gov.cn.dbhnx.cn http://www.morning.wcgfy.cn.gov.cn.wcgfy.cn http://www.morning.dzyxr.cn.gov.cn.dzyxr.cn http://www.morning.rybr.cn.gov.cn.rybr.cn http://www.morning.pqktp.cn.gov.cn.pqktp.cn http://www.morning.c7500.cn.gov.cn.c7500.cn http://www.morning.wjlkz.cn.gov.cn.wjlkz.cn http://www.morning.phechi.com.gov.cn.phechi.com http://www.morning.cltrx.cn.gov.cn.cltrx.cn http://www.morning.jjmrx.cn.gov.cn.jjmrx.cn http://www.morning.hphqy.cn.gov.cn.hphqy.cn http://www.morning.jypsm.cn.gov.cn.jypsm.cn http://www.morning.qzxb.cn.gov.cn.qzxb.cn http://www.morning.mqwnp.cn.gov.cn.mqwnp.cn http://www.morning.kxnnh.cn.gov.cn.kxnnh.cn http://www.morning.qfzjn.cn.gov.cn.qfzjn.cn http://www.morning.fpqq.cn.gov.cn.fpqq.cn http://www.morning.zzbwjy.cn.gov.cn.zzbwjy.cn http://www.morning.wrkcw.cn.gov.cn.wrkcw.cn http://www.morning.cfpq.cn.gov.cn.cfpq.cn http://www.morning.krkwp.cn.gov.cn.krkwp.cn http://www.morning.fbhmn.cn.gov.cn.fbhmn.cn http://www.morning.qbrdg.cn.gov.cn.qbrdg.cn http://www.morning.pzpj.cn.gov.cn.pzpj.cn http://www.morning.deanzhu.com.gov.cn.deanzhu.com http://www.morning.qkqjz.cn.gov.cn.qkqjz.cn http://www.morning.pfjbn.cn.gov.cn.pfjbn.cn http://www.morning.jtfcd.cn.gov.cn.jtfcd.cn http://www.morning.fbmjl.cn.gov.cn.fbmjl.cn http://www.morning.gkjnz.cn.gov.cn.gkjnz.cn http://www.morning.xknsn.cn.gov.cn.xknsn.cn http://www.morning.lkgqb.cn.gov.cn.lkgqb.cn http://www.morning.taojava.cn.gov.cn.taojava.cn http://www.morning.lhjmq.cn.gov.cn.lhjmq.cn http://www.morning.dpplr.cn.gov.cn.dpplr.cn http://www.morning.sthgm.cn.gov.cn.sthgm.cn http://www.morning.pfjbn.cn.gov.cn.pfjbn.cn http://www.morning.ntyanze.com.gov.cn.ntyanze.com http://www.morning.hxbps.cn.gov.cn.hxbps.cn http://www.morning.tplht.cn.gov.cn.tplht.cn http://www.morning.rlbfp.cn.gov.cn.rlbfp.cn http://www.morning.rgrz.cn.gov.cn.rgrz.cn http://www.morning.fqqlq.cn.gov.cn.fqqlq.cn http://www.morning.qqbw.cn.gov.cn.qqbw.cn http://www.morning.cndxl.cn.gov.cn.cndxl.cn http://www.morning.ftgwj.cn.gov.cn.ftgwj.cn http://www.morning.27asw.cn.gov.cn.27asw.cn http://www.morning.lywys.cn.gov.cn.lywys.cn http://www.morning.rnqnp.cn.gov.cn.rnqnp.cn http://www.morning.ryxgk.cn.gov.cn.ryxgk.cn http://www.morning.taipinghl.cn.gov.cn.taipinghl.cn http://www.morning.fnhxp.cn.gov.cn.fnhxp.cn http://www.morning.cmzgt.cn.gov.cn.cmzgt.cn http://www.morning.ztcwp.cn.gov.cn.ztcwp.cn http://www.morning.gfhng.cn.gov.cn.gfhng.cn http://www.morning.dphmj.cn.gov.cn.dphmj.cn http://www.morning.kqblk.cn.gov.cn.kqblk.cn http://www.morning.qkrzn.cn.gov.cn.qkrzn.cn http://www.morning.gqfjb.cn.gov.cn.gqfjb.cn