Java – passing ArrayList to a separate class?

I have a code to read from the SQL database and save each column of information to the ArrayList I need to pass each ArrayList to a separate class. I can store the list as a single information (ie: the information in the first part of arraylist1 together with the information in the first part of arraylist2, etc.), and then sort it I don't know how to transfer this information to another course This is part of my main method of storing information in a list I need to pass this information to a named list Separate classes of Java:

String SelectStatement1 = "SELECT InvoiceID FROM Invoice;";
    ps = conn.prepareStatement(SelectStatement1);
    rs = ps.executeQuery();
    int count = 0;
    while (rs.next()){
        count++;
    }
    ps.close();
    ps = conn.prepareStatement(SelectStatement1);
    rs = ps.executeQuery();
    ArrayList<String> InvoiceIDList = new ArrayList<String>();
    String InvoiceID = null;
    int p = 0;
    while (p < count){
        rs.next();
        InvoiceID = rs.getString("InvoiceID");
        InvoiceIDList.add(InvoiceID);
        p++;
    }
    ps.close();
    p = 0;

Edit: This is just a part of my code. I already have code to open and close connections. I just need information about how to pass ArrayList to another class for sorting

Solution

Create a method in another class as follows:

public void receiveList (ArrayList<String> invoiceIDList) {
    // Do something with invoiceIDList data
}

Creating a constructor in the "list" class may not be a bad idea. It accepts ArrayList and creates a class instance with the required data

Please also change the name of this class! This is a confusing thing for people who read your code, because you have passed an ArrayList!

Edit:

You can also let your class implement the list interface, which will make things easier for you, because you can insert data into your class according to the data location in ArrayList

public class yourClass implements List<String> {
     // Your class methods and variables...
}

If you want to extend this to allow more than just strings, you can change it to: List < T >, which will give you a more general method

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>