• Java 数组


    1. 声明数组 

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

    2. 输出数组 

    int[] intArray = { 1, 2, 3, 4, 5 };  
    String intArrayString = Arrays.toString(intArray);  

    3. 数组-->ArrayList

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

    4.  检查一个数组是否包含某个值 

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

    5.  连接两个数组 

    int[] intArray = { 1, 2, 3, 4, 5 };  
    int[] intArray2 = { 6, 7, 8, 9, 10 };  
    // Apache Commons Lang library  
    int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2); 

    6. String[] --> String

    // Apache common lang  
    String j = StringUtils.join(new String[] { "a", "b", "c" }, ", ");  
    System.out.println(j);   // a, b, c  

    7. ArrayList --> 数组

    String[] stringArray = { "a", "b", "c", "d", "e" };  
    ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));  
    // ArrayList<String> --> String[] String[] stringArr
    = new String[arrayList.size()]; arrayList.toArray(stringArr);
    return stringArr;
    // or this way
    String[] stringArr = (String[])arrayList.toArray(new String[0]);
    return stringArr;

    8. 数组 --> Set

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

    9. reverse 数组 

    int[] intArray = { 1, 2, 3, 4, 5 };  
    ArrayUtils.reverse(intArray);  

    10. 移除数组中的元素

    int[] intArray = { 1, 2, 3, 4, 5 };  
    int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new array  
  • 相关阅读:
    Django复习
    AI-CBV写法
    CHENGDU3-Restful API 接口规范、django-rest-framework框架
    人工智能玩具制作
    POJ 3176 Cow Bowling
    HDU 2044 一只小蜜蜂
    HDU 4662 MU Puzzle
    POJ 3262 Protecting the Flowers
    POJ 1862 Stripies
    POJ 1017 Packets
  • 原文地址:https://www.cnblogs.com/hesier/p/5571392.html
Copyright © 2020-2023  润新知