常用API-StringBuffer、Math、随机数、日期时间、Arrays
publicclassTest_Interface{publicstaticvoidmain(String[]args){/*************StringBuffer,改变字符串自身***********************************************/StringBufferstrb=newStringBuffer();//创建对象strb.append(62).append("YES").append(true);//添加字符串System.out.println(strb);strb.insert(strb.length(),'f');//索引位置插入字符System.out.println(strb);strb.delete(8,9);//删除包括8,不包括9的字符串System.out.println(strb);strb.deleteCharAt(8);//删除该位置的字符System.out.println(strb);strb.reverse();//字符串倒置System.out.println(strb);String***b=strb.substring(2,6);//截取包括2,包括6的字符串,生成新字符串System.out.println(***b);strb.setCharAt(7,'9');//设置索引位置的字符System.out.println(strb);strb.replace(0,strb.length()-1,"AS");//替换某段字符串,包括头不包括尾System.out.println(strb);strb.capacity();System.out.println(strb.capacity());//计算数组的长度,默认加16个字符的缓冲区,/*StringBuffer(Strings);除了按照s的大小分配空间外,再分配16个字符的缓冲区*/strb.setLength(2);System.out.println(strb);//重设长度,截取,超过则不影响原字符串/**********************常用数学API****************************************************/System.out.println(Math.abs(-56));System.out.println(Math.max(-23,-2));System.out.println(Math.min(-23,-2));System.out.println(Math.pow(10,-2));//求幂System.out.println(Math.sqrt(100));//求平方根System.out.println(Math.round(2.6));//原数加0.5,然后floorSystem.out.println(Math.floor(-1.1));//小于或等于该整数-1.1=>-2,1.1=>1System.out.println(Math.random());//0-1之间的随机数System.out.println(Math.PI);//PI/***********************生成随机数***************************************************/Randomr=newRandom();System.out.println(r.nextInt(10000)%35+1);//创建对象,括号内代表生成随机数的范围,包括头不包括尾/***********************日期时间API***************************************************/Calendarca=Calendar.getInstance();//创建日历对象,需要调用方法得到intmonth=ca.get(Calendar.MONTH);//得到Caledar的属性值,获得日期、时间等,其中月份从0开始计数System.out.println(month);System.out.println(ca.toString());//直接tostring输出会显示内存地址和hash码,需要去格式化Datedate=newDate();//先创建时间对象,需要导入date类SimpleDateFormatsday=newSimpleDateFormat("yy-MM-ddHH:mm:ss");//格式化时间,顶部需要导入simple类Stringsdate=sday.format(date);//对date用方法format格式成sday模板System.out.println(sdate);/********************增强型for循环(foreach)******************************************************/Collection<String>alist=newArrayList<String>();alist.add("AAA");alist.add("BBB");alist.add("CCC");alist.add("DDD");alist.add("EEE");System.out.println(alist);for(Stringnewlist:alist)//增强型for循环,将alist容器内元素依次给newlist输出{System.out.println(newlist);}/***********************迭代器***************************************************/Iterator<String>diedai=alist.iterator();//迭代器,容器没有get方法,需要迭代器循环输出while(diedai.hasNext())//判断游标下一处是否有元素{Stringss=diedai.next();//取得游标下一处元素,如果diedai类型不一致需要强制转换System.out.println(ss);}/*******************HashSet容器*******************************************************/HashSet<Stud>hset=newHashSet<Stud>();//自定义类型的HashSet容器的判断去重复,原类型添加equals和//hash方法重写hset.add(newStud("HH",26));hset.add(newStud("LL",35));hset.add(newStud("HH",26));System.out.println(hset);Iterator<Stud>ite=hset.iterator();//迭代器循环获取容器元素并输出while(ite.hasNext()){Studss=ite.next();System.out.println(ss);}/**************************************************************************/ArrayList<Integer>utils=newArrayList<Integer>();//utils工具utils.add(53);utils.add(5);utils.add(2);utils.add(37);utils.add(25);System.out.println(utils);System.out.println(utils.size());//计算容器大小Collections.sort(utils);//对原元素从小到大排序,保存于原容器内System.out.println(utils);intpos=Collections.binarySearch(utils,36);//运用二分法查找该对象的位置,找不到则返回-(插入点)-1System.out.println(pos);/**************************************************************************/int[]array=newint[]{1,3,7,5,4,7,6};int[]newArr=Arrays.copyOf(array,6);//运行结果:[1,3,7,4,6,0],位数不够时,默认加上并填充默认值int[]newArr3=Arrays.copyOf(array,3);//运行结果:[1,3,7]int[]newArr2=Arrays.copyOfRange(array,2,9);//运行结果:[7,4,6,0,0,0,0]Arrays.sort(array);//对数组进行排序,从小到大排序System.out.println(Arrays.toString(array));//直接打印数组,显示结果:[1,3,4,5,6,7,7]intpos=Arrays.binarySearch(array,7);//二分法查找对应的int数在数组中的位置,使用前必须sort排序,找不到则返回-1System.out.println(pos);//pos=5}}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。