简体   繁体   English

从数组列表中检索多个值

[英]Retrieving multiple values from an arraylist

I have saved values retrieved from a database in java to an arraylist. 我已将从Java中的数据库检索到的值保存到arraylist中。 I have used a class to save the data to the array list as shown below. 我已经使用一个类将数据保存到数组列表,如下所示。

    ArrayList<NewSms> details = new ArrayList<NewSms>();
    try{
        Statement query = conn.createStatement();
        ResultSet result = query.executeQuery("Select `senderAddress,message,linkid from sdp_smsincoming where status=0 AND smsServiceActivationNumber =1234 ");`

        while(result.next()){
            String address = result.getString("senderAddress");
            String message = result.getString("message");
            String linkid = result.getString("linkid");

            NewSms smsdetails = new NewSms();
            smsdetails.set_address(address);
            smsdetails.set_message(message);
            smsdetails.set_linkid(linkid);
            details.add(smsdetails);;
        }
    }

However i now want to retrieve the values individually per row,from another class in the program.How can i do this?By individually i mean getting the address,message and linkid per row in the arraylist. 但是我现在想从程序中的另一个类中每行分别获取值。我该怎么做呢?我分别是指获取arraylist中每行的地址,消息和linkid。

However i now want to retrieve the values individually per row,from another class in the program.How can i do this? 但是我现在想从程序的另一个类中每行分别检索值。我该怎么做?

You just access each NewSms in the list. 您只需访问列表中的每个NewSms To access one by index, you could use: 要按索引访问一个,可以使用:

NewSms sms = details.get(2); // Or whatever
// Now access the properties of sms

Or to access each of them in turn, use an enhanced for loop: 或者依次访问它们中的每一个,请使用增强的for循环:

for (NewSms sms : details) {
    // Now access the properties of sms
}

Note that to comply with Java naming conventions, your NewSms class should have methods such as getAddress , setAddress - not set_address . 请注意,为了遵守Java命名约定,您的NewSms类应具有诸如getAddresssetAddress -not set_address类的方法。

Also note that you need to close your result set / statement / connection - if you're using Java 7, you can use a try-with-resources statement; 还要注意,您需要关闭结果集/语句/连接-如果您使用的是Java 7,则可以使用try-with-resources语句。 otherwise you should use finally blocks. 否则,您应该使用finally块。

EDIT: If your problem is actually just returning the list, that's easy: 编辑:如果您的问题实际上只是返回列表,那很简单:

public List<NewSms> loadSmsDetails() {
    // Code as before...

    return details;
}

Then just call it from your other class: 然后从其他班级调用它:

// Where repository is a reference to an instance of the class containing the above method
List<NewSms> allDetails = repository.loadSmsDetails();
for(NewSms smsdetails:details){ 
String address = smsdetails.get_address();
String message = smsdetails.get_message();
String linkId = smsdetails.get_linkid();
}

However i now want to retrieve the values individually per row,from another class in the program 但是我现在想从程序的另一个类中每行分别获取值

You need to pass the arraylist to the another class and iterate over it there to get the individual elements. 您需要将arraylist传递给另一个类,并在那里进行迭代以获得单个元素。 The idea is to use a common araylist - to store the values from the resultset and in another class to iterate through them. 这个想法是使用一个通用的列表-存储结果集中的值,并在另一个类中迭代它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM