• 【翻译】Java Array的排名前十方法(Top 10 Methods for Java Arrays)


    这里列举了Java Array 的前十的方法。他们在stackoverflow最大投票的问题。

    The following are top 10 methods for Java Array. They are the most voted questions from stackoverflow.

    0.声明一个数组

    0. Declare an array

    String[] aArray = new String[5];
    String[] bArray = {"a", "b", "c", "d", "e"};
    String[] cArray = {"a", "b", "c", "d", "e"};

     1.打印数组

    1. Print an array in Java

    int[] intArray = {1, 2, 3, 4, 5};
    String intArrayString = Arrays.toString(intArray);
    
    //直接输出Array,输出,内存地址:[I@4554617c
    System.out.println(intArray);
    
    //输出:[1, 2, 3, 4, 5]
    System.out.println(intArrayString);

    2.从数组中转为ArrayList

    2. Create an ArrayList from an array

     String[] stringArray = {"a", "b", "c", "d", "e"};
     ArrayList<String> arrayList = new ArrayList<>(Arrays.asList(stringArray));
    //输出[a, b, c, d, e] System.out.println(arrayList);

    3.判断数组是否含有某值

    3. Check if an array contains a certain value

      String[] stringArray = {"a", "b", "c", "d", "e"};
      boolean b = Arrays.asList(stringArray).contains("a");
     //true System.out.println(b);

    4.连接两个数组

    4. Concatenate two arrays

     //需要导入 org.apache.commons.lang3
     int[] intArray1 = {1, 2, 3, 4, 5};
     int[] intArray2 = {6, 7 , 8, 9, 10};
     int[] combinedIntArray = org.apache.commons.lang3.ArrayUtils.addAll(intArray1, intArray2);
     String arrayString = Arrays.toString(combinedIntArray);
     //[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
     System.out.println(arrayString);

     5.Declare an array inline  --- 不知道这个的作用

    method(new String[]{"a", "b", "c", "d", "e"});

    6.将数组的每个元素取出拼接成字符串

    6. Joins the elements of the provided array into a single String

     String j = org.apache.commons.lang3.StringUtils.join(new String[] { "a", "b", "c" }, ":");
    //a:b:c System.out.println(j);

    7.将ArrayList转换为数组

    7.Covnert an ArrayList to an array

     String[] stringsArray = {"a", "b", "c", "d", "e"};
     ArrayList<String> arrayList = new ArrayList<>(Arrays.asList(stringsArray));
     String[] stringArr = new String[arrayList.size()];
     arrayList.toArray(stringArr);

    8.将数组转换为set

    8. Convert an array to a set

      String[] stringsArray = {"a", "b", "c", "d", "e"};
      Set<String> set = new HashSet<String>(Arrays.asList(stringsArray));
      System.out.println(set);
    

    9.反转一个数组

    9. Reverse an array

     String[] stringsArray = {"a", "b", "c", "d", "e"};
     ArrayUtils.reverse(stringsArray);
     //[e, d, c, b, a]
     System.out.println(Arrays.toString(stringsArray));

    10.移除数组的某个元素

    10. Remove element of an array

     int[] intArray = { 1, 2, 3, 4, 5 };
     int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new array
     System.out.println(Arrays.toString(removed));
    

    One more - convert int to byte array

    byte[] bytes = ByteBuffer.allocate(4).putInt(8).array();
     
    for (byte t : bytes) {
       System.out.format("0x%x ", t);
    }
    

     

     
  • 相关阅读:
    前端agl分页的写法
    分布式项目spring 配置文件的约束
    电脑维修常用硬件技术
    电脑维修常用检修软件技术
    电脑维修快速入门
    电脑维修基本流程
    re模块
    flask-本地线程-请求上下文补充
    SEO(搜索引擎优化)
    前端基础之jquery
  • 原文地址:https://www.cnblogs.com/caoRM/p/8793512.html
Copyright © 2020-2023  润新知