簡體   English   中英

如何將列表轉換為字符串

[英]How do i convert List to String

我正在獲取前三列的值,但沒有獲取最后一列的值。 如何將列表轉換為字符串,以便獲取“組織”列的值?

這是我的代碼:

String appname = "abc";
String path = "//home/exportfile//";
String filename = path+"ApplicationExport-"+appname+".txt";
String ret = "false";

        QueryOptions ops = new QueryOptions();
    Filter [] filters = new Filter[1];
    filters[0] = Filter.eq("application.name", appname);
    ops.add(filters);

    List props = new ArrayList();
    props.add("identity.name");

    //Do search
    Iterator it = context.search(Link.class, ops, props);

    //Build file and export header row
    BufferedWriter out = new BufferedWriter(new FileWriter(filename));
    out.write("IdentityName,UserName,WorkforceID,Organization");
    out.newLine();          

    //Iterate Search Results
    if (it!=null)
    {                               
            while ( it.hasNext() ) {

                    //Get link and create object
                    Object [] record = it.next();
                    String identityName = (String) record[0];
                    Identity user = (Identity) context.getObject(Identity.class, identityName);

                    //Get Identity attributes for export
                    String workforceid = (String) user.getAttribute("workforceID");                 

                    //Get application attributes for export
                    String userid="";

                    List links = user.getLinks();
                    if (links!=null)
                    {
                            Iterator lit = links.iterator();
                            while (lit.hasNext())
                            {
                                    Link l = lit.next();
                                    String lname = l.getApplicationName();
                                    if (lname.equalsIgnoreCase(appname))
                                    {
                                              userid = (String) l.getAttribute("User Name");
                                              List organizations = l.getAttribute("Organization");

                                              StringBuilder sb = new StringBuilder();
                                              String listItemsSeparator = ","; // this you can change to anything you want, it separates items from list

                                                                                                            for (Object organization : organizations)
                                                                                                                    {
                                                                                                                            sb.append(organization.toString());
                                                                                                                            sb.append(listItemsSeparator);
                                                                                                                    }

                                                                                                                    org = sb.toString().trim();

                                    }
                            }
                    }                   

                    //Output file
                    out.write(identityName+","+userid+","+workforceid+","+org);                             
                    out.newLine();                                                                          
                    out.flush();
            }

                     ret="true";
    }

    //Close file and return
    out.close();
    return ret;

此代碼將Void寫入Organization列的值。 我怎樣才能解決這個問題?

您確實沒有提供太多有關應用程序中發生的事情或為什么要獲取組織列表的信息。 但是嘗試一下:

out.write(identityName+","+userid+","+workforceid+","+(String)orgList.get(0));   

另外,您的列表未參數化,因此我們不知道該列表存儲的對象類型。

您需要覆蓋列表中對象的默認toString方法。 在列表上調用toString將導致其中所有對象的toString方法被調用。 您還可以遍歷列表並打印每個值。

編輯:您的代碼確實很亂,但是我想我知道您要的是:

    List links = user.getLinks();
    if (links != null) {
        Iterator lit = links.iterator();
        while (lit.hasNext()) {
            Link l = lit.next();
            String lname = l.getApplicationName();
            if (lname.equalsIgnoreCase(appname)) {
                userid = (String) l.getAttribute("User Name");
                List organizations = l.getAttribute("Organization");

                StringBuilder sb = new StringBuilder();
                String listItemsSeparator = " "; // this you can change to anything you want, it separates items from list

                //iterating over list, to convert it to single String 
                for (Object organization : organizations) {
                    sb.append(organization.toString());
                    sb.append(listItemsSeparator);
                }

                orgList = sb.toString().trim();

            }
        }
    }

編輯2:

while (it.hasNext()) {

    //Get link and create object
    Object [] record = it.next();
    String identityName = (String) record[0];
    Identity user = (Identity) context.getObject(Identity.class, identityName);

    //Get Identity attributes for export
    String workforceid = (String) user.getAttribute("workforceID");

    //Get application attributes for export
    String userid="";

    List links = user.getLinks();
    if (links!=null)
    {
        Iterator lit = links.iterator();
        while (lit.hasNext())
        {
            Link l = lit.next();
            String lname = l.getApplicationName();
            if (lname.equalsIgnoreCase(appname))
            {
                userid = (String) l.getAttribute("User Name");
                List orgList = l.getAttribute("Organization");
                if(organization < orgList.size()) {
                    String singleOrganization = orgList.get(organization);
                    organization++;


                    //Output file
                    out.write(identityName+","+userid+","+workforceid+","+orgList);
                    out.newLine();
                    out.flush();

                }

            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM