ArrayUtils常用方法的基本操作

PS(首先需要导入commons-lang.jar,我的是3-3.6版本


   
   
  1. package com.zyy.common;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.apache.commons.lang3.ArrayUtils;
  5. /**
  6. * ArrayUtils常用方法除了下面的,还有isEmpty(),add(),indexOf()
  7. * ,isEquals()等
  8. * @author ZYY
  9. *
  10. */
  11. public class TestArrayUtils {
  12. public static void main(String []args) {
  13. testToObject();
  14. }
  15. /**
  16. * Contains
  17. * 如果某个数组包含某个值就返回true
  18. * 否则返回false
  19. */
  20. public static void testContains() {
  21. int []array= {1,2,3};
  22. System.out.println(ArrayUtils.contains(array, 2));//true
  23. System.out.println(ArrayUtils.contains(array, 4));//false
  24. }
  25. /**
  26. * AddAll
  27. * 把另一个数组的值全部赋给
  28. * 另一个数组,创建出一个新的数组,原数组的值不变
  29. */
  30. public static void testAddAll() {
  31. int []array1= {1,2,3};
  32. int []array2= {4,5,6};
  33. int[] array3= ArrayUtils.addAll(array1, array2);
  34. for (int i : array3) {
  35. System.out.println(i);
  36. }
  37. }
  38. /**
  39. * 复制对象
  40. */
  41. public static void testClone() {
  42. int []array1= {1,2,3};
  43. int[] clone = ArrayUtils.clone(array1);
  44. for (int i : clone) {
  45. System.out.println(i);
  46. }
  47. }
  48. /**
  49. * 截取某个数组的值
  50. * 从startIndexInclusive
  51. * 到endIndexExclusive
  52. * 即[)
  53. */
  54. public static void testSubarray() {
  55. int []array= {4,5,6};
  56. int[] subarray = ArrayUtils.subarray(array,0, 2);
  57. for (int i : subarray) {
  58. System.out.println(i);
  59. }
  60. }
  61. /**
  62. * 将基本类型数组变成
  63. * 对应的引用类型数组
  64. */
  65. public static void testToObject() {
  66. int []array= {4,5,6};
  67. Integer[] array1 = ArrayUtils.toObject(array);
  68. for (Integer integer : array1) {
  69. System.out.println(integer);
  70. }
  71. }
  72. }

3-3.6版本)
发布了244 篇原创文章 · 获赞 2 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_44813090/article/details/105466898