关于java:如何将ArrayList转换为Array

How to convert an ArrayList to Array

本问题已经有最佳答案,请猛点这里访问。

我无法实现将整个AARYLIST传递给Java程序中的一个方法。我在论坛中搜索并尝试了一些其他程序员能够解决的解决方案,但在我的程序中,我无法实现相同的解决方案。

我在这里附加了代码,并突出显示了我所面对的问题所在的行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import java.io.File;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import org.apache.axiom.om.OMElement;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;

import com.mercury.itg.ws.dm.client.AddRequestNotesDocument;
import com.mercury.itg.ws.dm.client.AddRequestNotesResponseDocument;
import com.mercury.itg.ws.dm.client.Column;
import com.mercury.itg.ws.dm.client.CreateRequestDocument;
import com.mercury.itg.ws.dm.client.CreateRequestResponseDocument;
import com.mercury.itg.ws.dm.client.DeleteRequestsDocument;
import com.mercury.itg.ws.dm.client.DeleteRequestsResponseDocument;
import com.mercury.itg.ws.dm.client.DemandServiceStub;
import com.mercury.itg.ws.dm.client.ExecuteWFTransitionsDocument;
import com.mercury.itg.ws.dm.client.ExecuteWFTransitionsResponseDocument;
import com.mercury.itg.ws.dm.client.GetRequestsDocument;
import com.mercury.itg.ws.dm.client.GetRequestsResponseDocument;
import com.mercury.itg.ws.dm.client.Identifier;
import com.mercury.itg.ws.dm.client.Note;
import com.mercury.itg.ws.dm.client.RemoteReference;
import com.mercury.itg.ws.dm.client.Request;
import com.mercury.itg.ws.dm.client.SetRequestFieldsDocument;
import com.mercury.itg.ws.dm.client.SetRequestFieldsResponseDocument;
import com.mercury.itg.ws.dm.client.SetRequestRemoteReferenceStatusDocument;
import com.mercury.itg.ws.dm.client.SetRequestRemoteReferenceStatusResponseDocument;
import com.mercury.itg.ws.dm.client.SimpleField;
import com.mercury.itg.ws.dm.client.Table;
import com.mercury.itg.ws.dm.client.URLReference;

import oracle.jdbc.OracleTypes;
import oracle.jdbc.OracleDriver;

public class PMCDeleteRequest {

    protected ConfigurationContext ctx = null;

    public PMCDeleteRequest() {
        final String repositoryPath = System.getProperty("client.repository.dir");
        final String axis2 = repositoryPath +"/conf/client-axis2.xml";
        final File file = new File(axis2);
        if (file.exists()) {
            try {
                this.ctx = ConfigurationContextFactory.createConfigurationContextFromFileSystem(repositoryPath, axis2);
            } catch (final Exception e) {
                e.printStackTrace();
                System.exit(1);
            }
        }
    }
      String name;
     public void setName(String name) {
            this.name = name;
        }
    /**
     * The main program Parameter: args[0] - service URL. e.g.
     * http://server:port/itg/ppmservices/DemandService
     */


    public static void main(final String[] args) throws Exception {

        String URL = args[0];
        String username = args[1];
        String password = args[2];
        String requestids = args[3];
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection con = DriverManager.getConnection(URL, username, password);
        String request_ids = requestids;
        String commaSeparateRequestIds = request_ids.replaceAll("#@#",",");
        String request_delete_sql ="SELECT REQUEST_ID FROM (SELECT KFMP.REQUEST_ID FROM KCRT_FG_MASTER_PROJ_REF KFMP, PM_PROJECTS PP WHERE KFMP.REF_MASTER_PROJECT_ID = PP.PROJECT_ID AND PP.PROJECT_ID IN ("
                + commaSeparateRequestIds +"))";
        PreparedStatement sgStmt = con.prepareStatement(request_delete_sql);
        ResultSet rs = sgStmt.executeQuery();
        List<String> delete = new ArrayList<String>();
        try {
        while (rs.next()) {

            final String serviceURL ="https://.../DemandService";
            String requestId = Integer.toString(rs.getInt(1));

            delete.add(requestId);


        }
        System.out.println("arraylist:"+delete);


                pmc.deleteRequests(serviceURL,delete);  // Here i am unable to pass the entire arraylist

            }

        catch (final Exception e) {
                if (e instanceof org.apache.axis2.AxisFault) {
                    final org.apache.axis2.AxisFault xe = (org.apache.axis2.AxisFault) e;
                    System.out.println(xe.getMessage());
                    System.out.println("---detail---");
                    final Iterator iter = xe.getDetail().getChildElements();
                    while (iter.hasNext()) {
                        final OMElement item = (OMElement) iter.next();
                        System.out.println(item.getText());
                    }
                    System.out.println("------------");
                } else {
                    throw e;

                }
                System.exit(1);
            }

        rs.close();
        con.close();

    }

    private void deleteRequests(final String serviceURL, final String[] requestIds) throws Exception {

        // construct the Identifier array
        final Identifier[] ids = new Identifier[requestIds.length];

        for (int i = 0; i < requestIds.length; i++) {

            ids[i] = Identifier.Factory.newInstance();
            ids[i].setId(requestIds[i]);

        }

        // get Webservice handle

        final DemandServiceStub stub = new DemandServiceStub(this.ctx, serviceURL);

        // Construct message to send
        final DeleteRequestsDocument inDoc = DeleteRequestsDocument.Factory.newInstance();

        final DeleteRequestsDocument.DeleteRequests deletes = inDoc.addNewDeleteRequests();

        deletes.setRequestIdsArray(ids);

        //Set the path for cacerts for searching certificate before invoking web service
System.setProperty("javax.net.ssl.trustStore","C:\\Java\\jdk1.7.0_80\\jre\\lib\\security\\cacerts");
// Invoke web service
        final DeleteRequestsResponseDocument outDoc = stub.deleteRequests(inDoc);

        // Process the return request
        final int returnCode = outDoc.getDeleteRequestsResponse().getReturn();
        System.out.println("Delete Requests Succeeded. Return Code:" + returnCode);
    }

}

在将整个arraylist传递给方法时,我所面对的问题所在的行:

1
pmc.deleteRequests(serviceURL,delete);

这里,deleteRequests参数可以有(String,String[])

任何指导/建议都将非常有用。

谢谢和问候,萨梅特


使用toArray(T[] a)方法将List转换为数组。

1
2
String[] deleteArray = delete.toArray(new String[delete.size()]);
pmc.deleteRequests(serviceURL, deleteArray);