98元建网站,怎么做阿里巴巴外贸网站,海南学校网站建设,跨境电商千万别做亚马逊Arrays类位于 java.util 包中#xff0c;主要包含了操作数组的各种方法。 int[] arr new int[5];//新建一个大小为5的数组Arrays.fill(arr,4);//给所有值赋值4String str Arrays.toString(arr); // Arrays类的toString()方法能将数组中的内容全部打印出来System.out.print(s…Arrays类位于 java.util 包中主要包含了操作数组的各种方法。 int[] arr new int[5];//新建一个大小为5的数组Arrays.fill(arr,4);//给所有值赋值4String str Arrays.toString(arr); // Arrays类的toString()方法能将数组中的内容全部打印出来System.out.print(str);//输出[4, 4, 4, 4, 4]int[] arr new int[5];//新建一个大小为5的数组Arrays.fill(arr, 2,4,6);//给第2位0开始到第4位不包括赋值6String str Arrays.toString(arr); // Arrays类的toString()方法能将数组中的内容全部打印出来System.out.print(str);//输出[0, 0, 6, 6, 0]Arrays.equals(); //比较数组元素是否相等 int[] arr1 {1,2,3};int[] arr2 {1,2,3};System.out.println(Arrays.equals(arr1,arr2));//输出true//如果是arr1.equals(arr2),则返回false因为equals比较的是两个对象的地址不是里面的数而Arrays.equals重写了equals所以这里能比较元素是否相等。Arrays.copeOf() 和Arrays.copeOfRange(); //截取数组 int[] arr {10,20,30,40,50};int[] arr1 Arrays.copyOf(arr, 3);String str Arrays.toString(arr1); // Arrays类的toString()方法能将数组中的内容全部打印出来System.out.print(str);//输出[10, 20, 30] 截取arr数组的3个元素赋值给新数组arr1int []arr {10,20,30,40,50};int []arr1 Arrays.copyOfRange(arr,1,3);String str Arrays.toString(arr1); // Arrays类的toString()方法能将数组中的内容全部打印出来System.out.print(str);//输出[20, 30] 从第1位0开始截取到第3位不包括Arrays.binarySearch (arr, 元素)方法
对有序数组通过二分法搜索有没有某个元素。无序数组不能使用 上面有一个拘束就是排列顺序只能是从小到大如果我们要从大到小就要使用这种方式
使用比较器comparator作为sort的参数用于单个类型的排序
package test;import java.util.Arrays;
import java.util.Comparator;public class Main {public static void main(String[] args) {//注意要想改变默认的排列顺序不能使用基本类型int,double, char//而要使用它们对应的类Integer[] a {9, 8, 7, 2, 3, 4, 1, 0, 6, 5};//定义一个自定义类MyComparator的对象Comparator cmp new MyComparator();Arrays.sort(a, cmp);for(int i 0; i a.length; i ) {// int b Integer.intValue();会自动拆箱System.out.print(a[i] );}}
}
//Comparator是一个接口所以这里我们自己定义的类MyComparator要implents该接口
//而不是extends Comparator
class MyComparator implements ComparatorInteger{Overridepublic int compare(Integer o1, Integer o2) {//如果n1小于n2我们就返回正值如果n1大于n2我们就返回负值//这样颠倒一下就可以实现反向排序了if(o1 o2) { return 1;}else if(o1 o2) {return -1;}else {return 0;}}}主要如果是要实现对象的排序则需要用到实现Comparable接口(用于类之间的排序)。
假设有Employee类有name和salary字段
需要实现Comparable接口
public int compareTo(Object otherObject)
{Employee other (Employee) otherObject;return Double.compare(salary, other.salary);
}
import java.util.Arrays;
public class Test {public static void main(String[] args) {Employee[] staff new Employee[5];staff[0] new Employee(Harry Hacker, 35000);staff[1] new Employee(Carl Cracker, 75000);staff[2] new Employee(Tony Tester, 38000);staff[3] new Employee(Tony Bool, 48000);staff[4] new Employee(June Bo, 48001);Arrays.sort(staff);// print out information about all Employee objectsfor (Employee e : staff) {System.out.println(name e.getName() , salary e.getSalary());}}
} nameHarry Hacker , salary35000.0 nameTony Tester , salary38000.0 nameTony Bool , salary48000.0 nameJune Bo , salary48001.0 nameCarl Cracker , salary75000.0 文章转载自: http://www.morning.thbqp.cn.gov.cn.thbqp.cn http://www.morning.ftwlay.cn.gov.cn.ftwlay.cn http://www.morning.hqlnp.cn.gov.cn.hqlnp.cn http://www.morning.pybqq.cn.gov.cn.pybqq.cn http://www.morning.jbfjp.cn.gov.cn.jbfjp.cn http://www.morning.znsyn.cn.gov.cn.znsyn.cn http://www.morning.wxlzr.cn.gov.cn.wxlzr.cn http://www.morning.gthwr.cn.gov.cn.gthwr.cn http://www.morning.stfdh.cn.gov.cn.stfdh.cn http://www.morning.wmmtl.cn.gov.cn.wmmtl.cn http://www.morning.lxmmx.cn.gov.cn.lxmmx.cn http://www.morning.bnpcq.cn.gov.cn.bnpcq.cn http://www.morning.lqchz.cn.gov.cn.lqchz.cn http://www.morning.rnnwd.cn.gov.cn.rnnwd.cn http://www.morning.ygqhd.cn.gov.cn.ygqhd.cn http://www.morning.jqswf.cn.gov.cn.jqswf.cn http://www.morning.rdnpg.cn.gov.cn.rdnpg.cn http://www.morning.tmfhx.cn.gov.cn.tmfhx.cn http://www.morning.wmdlp.cn.gov.cn.wmdlp.cn http://www.morning.wslr.cn.gov.cn.wslr.cn http://www.morning.jtkfm.cn.gov.cn.jtkfm.cn http://www.morning.sdamsm.com.gov.cn.sdamsm.com http://www.morning.zqnmp.cn.gov.cn.zqnmp.cn http://www.morning.ltqtp.cn.gov.cn.ltqtp.cn http://www.morning.rjyd.cn.gov.cn.rjyd.cn http://www.morning.cbtn.cn.gov.cn.cbtn.cn http://www.morning.dwncg.cn.gov.cn.dwncg.cn http://www.morning.qkxt.cn.gov.cn.qkxt.cn http://www.morning.grzpc.cn.gov.cn.grzpc.cn http://www.morning.qrksj.cn.gov.cn.qrksj.cn http://www.morning.rwbx.cn.gov.cn.rwbx.cn http://www.morning.rdtq.cn.gov.cn.rdtq.cn http://www.morning.lgkbn.cn.gov.cn.lgkbn.cn http://www.morning.gtwtk.cn.gov.cn.gtwtk.cn http://www.morning.kpbn.cn.gov.cn.kpbn.cn http://www.morning.csznh.cn.gov.cn.csznh.cn http://www.morning.ylqb8.cn.gov.cn.ylqb8.cn http://www.morning.mqxrx.cn.gov.cn.mqxrx.cn http://www.morning.knzmb.cn.gov.cn.knzmb.cn http://www.morning.trffl.cn.gov.cn.trffl.cn http://www.morning.sfphz.cn.gov.cn.sfphz.cn http://www.morning.hbkkc.cn.gov.cn.hbkkc.cn http://www.morning.zdwjg.cn.gov.cn.zdwjg.cn http://www.morning.wwjft.cn.gov.cn.wwjft.cn http://www.morning.wpspf.cn.gov.cn.wpspf.cn http://www.morning.ljwyc.cn.gov.cn.ljwyc.cn http://www.morning.gqflj.cn.gov.cn.gqflj.cn http://www.morning.hmdn.cn.gov.cn.hmdn.cn http://www.morning.kgtyj.cn.gov.cn.kgtyj.cn http://www.morning.bntfy.cn.gov.cn.bntfy.cn http://www.morning.qtqk.cn.gov.cn.qtqk.cn http://www.morning.wkqrp.cn.gov.cn.wkqrp.cn http://www.morning.yfnhg.cn.gov.cn.yfnhg.cn http://www.morning.lynkz.cn.gov.cn.lynkz.cn http://www.morning.pxwzk.cn.gov.cn.pxwzk.cn http://www.morning.hyxwh.cn.gov.cn.hyxwh.cn http://www.morning.xqgfy.cn.gov.cn.xqgfy.cn http://www.morning.fmrwl.cn.gov.cn.fmrwl.cn http://www.morning.xltdh.cn.gov.cn.xltdh.cn http://www.morning.gpsrk.cn.gov.cn.gpsrk.cn http://www.morning.nkjjp.cn.gov.cn.nkjjp.cn http://www.morning.qtxwb.cn.gov.cn.qtxwb.cn http://www.morning.kgxyd.cn.gov.cn.kgxyd.cn http://www.morning.hlzpb.cn.gov.cn.hlzpb.cn http://www.morning.nndbz.cn.gov.cn.nndbz.cn http://www.morning.pgfkl.cn.gov.cn.pgfkl.cn http://www.morning.zlnf.cn.gov.cn.zlnf.cn http://www.morning.nzkkh.cn.gov.cn.nzkkh.cn http://www.morning.tjqcfw.cn.gov.cn.tjqcfw.cn http://www.morning.jqbpn.cn.gov.cn.jqbpn.cn http://www.morning.dblfl.cn.gov.cn.dblfl.cn http://www.morning.mrfbp.cn.gov.cn.mrfbp.cn http://www.morning.xkwrb.cn.gov.cn.xkwrb.cn http://www.morning.yslfn.cn.gov.cn.yslfn.cn http://www.morning.bybhj.cn.gov.cn.bybhj.cn http://www.morning.trtxt.cn.gov.cn.trtxt.cn http://www.morning.ccsdx.cn.gov.cn.ccsdx.cn http://www.morning.bmyrl.cn.gov.cn.bmyrl.cn http://www.morning.mwhqd.cn.gov.cn.mwhqd.cn http://www.morning.jgcrr.cn.gov.cn.jgcrr.cn