zoukankan      html  css  js  c++  java
  • 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  
  • 相关阅读:
    apache2.4+php7.3.2+mysql5.7
    redis 中的key值过期后,触发通知事件
    zookeeper之分布式锁以及分布式计数器(通过curator框架实现)
    SpringCloud学习笔记(4)——Zuul
    SpringCloud学习笔记(3)——Hystrix
    SpringCloud学习笔记(2)——Ribbon
    c++各种排序的简单实现
    c++动态规划dp算法题
    华为机试练习
    ubuntu遇到的 the system is runing low-graphics mode 问题
  • 原文地址:https://www.cnblogs.com/hesier/p/5571392.html
Copyright © 2011-2022 走看看