且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

常用API-StringBuffer、Math、随机数、日期时间、Arrays

更新时间:2022-09-16 11:22:19



  1. public class Test_Interface 
  2.     public static void main(String[] args) 
  3.     { 
  4.         /*************StringBuffer,改变字符串自身***********************************************/ 
  5.         StringBuffer strb = new StringBuffer();// 创建对象 
  6.         strb.append(62).append("YES").append(true);// 添加字符串 
  7.         System.out.println(strb); 
  8.          
  9.         strb.insert(strb.length(), 'f'); // 索引位置插入字符 
  10.         System.out.println(strb); 
  11.          
  12.         strb.delete(89); // 删除包括8,不包括9的字符串 
  13.         System.out.println(strb); 
  14.          
  15.         strb.deleteCharAt(8);// 删除该位置的字符 
  16.         System.out.println(strb); 
  17.          
  18.         strb.reverse();// 字符串倒置 
  19.         System.out.println(strb); 
  20.          
  21.         String s-s-rb = strb.substring(26);// 截取包括2,包括6的字符串,生成新字符串 
  22.         System.out.println(s-s-rb); 
  23.          
  24.         strb.setCharAt(7'9');// 设置索引位置的字符 
  25.         System.out.println(strb); 
  26.          
  27.         strb.replace(0, strb.length() - 1"AS");// 替换某段字符串,包括头不包括尾 
  28.         System.out.println(strb); 
  29.          
  30.         strb.capacity(); 
  31.         System.out.println(strb.capacity()); // 计算数组的长度,默认加16个字符的缓冲区, 
  32.         /* StringBuffer( String s ); 除了按照s的大小分配空间外,再分配16个 字符的缓冲区 */ 
  33.         strb.setLength(2); 
  34.         System.out.println(strb);// 重设长度,截取,超过则不影响原字符串 
  35.          
  36.         /**********************常用数学API****************************************************/ 
  37.          
  38.         System.out.println(Math.abs(-56)); 
  39.         System.out.println(Math.max(-23, -2)); 
  40.         System.out.println(Math.min(-23, -2)); 
  41.         System.out.println(Math.pow(10, -2));//求幂 
  42.         System.out.println(Math.sqrt(100));//求平方根 
  43.         System.out.println(Math.round(2.6));// 原数加0.5,然后floor 
  44.         System.out.println(Math.floor(-1.1));// 小于或等于该整数 -1.1 => -2 , 1.1 => 1 
  45.         System.out.println(Math.random());//0-1之间的随机数 
  46.         System.out.println(Math.PI); // PI 
  47.          
  48.         /***********************生成随机数***************************************************/ 
  49.          
  50.         Random r = new Random(); 
  51.         System.out.println(r.nextInt(10000) % 35 + 1); // 创建对象,括号内代表生成随机数的范围,包括头不包括尾 
  52.          
  53.         /***********************日期时间API***************************************************/ 
  54.          
  55.         Calendar ca = Calendar.getInstance();// 创建日历对象,需要调用方法得到 
  56.         int month = ca.get(Calendar.MONTH);// 得到Caledar的属性值,获得日期、时间等,其中月份从0开始计数 
  57.         System.out.println(month); 
  58.         System.out.println(ca.toString());// 直接tostring输出会显示内存地址和hash码,需要去格式化 
  59.          
  60.         Date date = new Date();// 先创建时间对象,需要导入date类 
  61.         SimpleDateFormat sday = new SimpleDateFormat("yy-MM-dd HH:mm:ss"); // 格式化时间,顶部需要导入simple类 
  62.         String sdate = sday.format(date); // 对date用方法format格式成sday模板 
  63.         System.out.println(sdate); 
  64.          
  65.         /********************增强型for循环(for each)******************************************************/ 
  66.         Collection<String> alist = new ArrayList<String>(); 
  67.         alist.add("AAA"); 
  68.         alist.add("BBB"); 
  69.         alist.add("CCC"); 
  70.         alist.add("DDD"); 
  71.         alist.add("EEE"); 
  72.          
  73.         System.out.println(alist); 
  74.          
  75.         for (String newlist : alist) // 增强型for循环,将alist容器内元素依次给newlist输出 
  76.         { 
  77.             System.out.println(newlist); 
  78.         } 
  79.          
  80.         /***********************迭代器***************************************************/ 
  81.          
  82.         Iterator<String> diedai = alist.iterator(); // 迭代器,容器没有get方法,需要迭代器循环输出 
  83.          
  84.         while (diedai.hasNext())// 判断游标下一处是否有元素 
  85.         { 
  86.             String ss = diedai.next();// 取得游标下一处元素,如果diedai类型不一致需要强制转换 
  87.             System.out.println(ss); 
  88.         } 
  89.          
  90.         /*******************HashSet容器*******************************************************/ 
  91.         HashSet<Stud> hset = new HashSet<Stud>(); // 自定义类型的HashSet容器的判断去重复,原类型添加equals和 
  92.                                                   // hash方法重写 
  93.         hset.add(new Stud("HH"26)); 
  94.         hset.add(new Stud("LL"35)); 
  95.         hset.add(new Stud("HH"26)); 
  96.          
  97.         System.out.println(hset); 
  98.          
  99.         Iterator<Stud> ite = hset.iterator(); // 迭代器 循环获取容器元素并输出 
  100.         while (ite.hasNext()) 
  101.         { 
  102.             Stud ss = ite.next(); 
  103.             System.out.println(ss); 
  104.         } 
  105.          
  106.         /**************************************************************************/ 
  107.         ArrayList<Integer> utils = new ArrayList<Integer>();  //utils  工具 
  108.         utils.add(53); 
  109.         utils.add(5); 
  110.         utils.add(2); 
  111.         utils.add(37); 
  112.         utils.add(25); 
  113.          
  114.         System.out.println(utils); 
  115.         System.out.println(utils.size());//计算容器大小 
  116.          
  117.         Collections.sort(utils);//对原元素从小到大排序,保存于原容器内  
  118.         System.out.println(utils); 
  119.          
  120.         int pos = Collections.binarySearch(utils,36);  //运用二分法查找该对象的位置,找不到则返回 -(插入点)-1 
  121.         System.out.println(pos); 
  122.          
  123.         /**************************************************************************/ 
  124.         int[] array = new int[]{137,54,76};  
  125.         int[] newArr = Arrays.copyOf(array, 6);//运行结果:[1, 3, 7, 4, 6, 0],位数不够时,默认加上并填充默认值  
  126.         int[] newArr3 = Arrays.copyOf(array, 3);//运行结果:[1, 3, 7]  
  127.         int[] newArr2 = Arrays.copyOfRange(array, 29);//运行结果:[7, 4, 6, 0, 0, 0, 0]  
  128.         Arrays.sort(array);//对数组进行排序,从小到大排序  
  129.           
  130.         System.out.println(Arrays.toString(array));//直接打印数组,显示结果:[1, 3, 4, 5, 6, 7, 7]  
  131.         int pos = Arrays.binarySearch(array, 7);//二分法查找对应的int数在数组中的位置,使用前必须sort排序,找不到则返回-1  
  132.         System.out.println(pos);//pos=5  
  133.     } 

 





本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1186831,如需转载请自行联系原作者