zoukankan      html  css  js  c++  java
  • java set转list,数组与list的转换

    读zookeeper的源码(zookeeper.java)时发现的,平时没有怎么注意:

     private final ZKWatchManager watchManager;
    
        List<String> getDataWatches() {
            synchronized(watchManager.dataWatches) {
                List<String> rc = new ArrayList<String>(watchManager.dataWatches.keySet());
                return rc;
            }
        }
        List<String> getExistWatches() {
            synchronized(watchManager.existWatches) {
                List<String> rc =  new ArrayList<String>(watchManager.existWatches.keySet());
                return rc;
            }
        }
        List<String> getChildWatches() {
            synchronized(watchManager.childWatches) {
                List<String> rc = new ArrayList<String>(watchManager.childWatches.keySet());
                return rc;
            }
        }

     另数组与list的转换:

    数组-->list

    String[] userid = {"aa","bb","cc"};
    List<String> userList = new ArrayList<String>();
    Collections.addAll(userList, userid);

    数组-->list

    String[] userid = {"aa","bb","cc"};
    List<String> userList = Arrays.asList(userid);
    另:Arrays.asList()返回一个受指定数组支持的固定大小的列表。所以不能做Add、Remove等操作。
    List list = new ArrayList(Arrays.asList(userid));这样操作就可以了。

    list-->数组

    List<String> strList = new ArrayList<String>();
    strList.add("aa");
    strList.add("bb");
    Object[] objs = strList.toArray();

    参考文献:

    【1】http://jingyan.baidu.com/article/86f4a73e607f2937d7526972.html

    【2】https://github.com/apache/zookeeper

  • 相关阅读:
    第四周上机作业
    第三周作业
    java第十二周作业
    java第十二周随堂
    java第十一周作业
    java第十一周随堂
    java第十周随堂
    java第九周随堂
    java第八周作业
    java第八周随堂
  • 原文地址:https://www.cnblogs.com/davidwang456/p/5000470.html
Copyright © 2011-2022 走看看