Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing ArrayList to separate class?

Tags:

java

arraylist

I have a code that reads from an SQL Database and saves each column of information into an ArrayList. I need to pass each ArrayList into a separate class where I can store the lists as single pieces of information (IE: Information in the first part of ArrayList1 goes with information in the first part of ArrayList2 etc...) and then sort them. I don't know how to pass that information to another class though. This is a section of my main method that stores the information into a list. I need this information passed to a separate class called List.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 only a section of my code, I already have the code open and close the connections, I only need information on how to pass the ArrayList to another class for sorting.

like image 886
Vincent van Driel Avatar asked Apr 20 '15 03:04

Vincent van Driel


2 Answers

Create a method in your other class like this:

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

It may not be a bad idea to create a constructor in your "List" class, that accepts the ArrayList and creates the class instance with the required data

Also, please change the name of that class!! It will be confusing to others who read your code, as you are passing an ArrayList already!

EDIT:

You could also have your class implement the List interface, which would make things a lot easier for you, because you can insert data into your class based on the position of the data in the ArrayList.

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

If you wanted to expand on this to allow more than just Strings, you can change to: List<T>, this would give you a more generic approach.

like image 72
Evan Bechtol Avatar answered Oct 23 '22 08:10

Evan Bechtol


First, I suggest you perform a SELECT COUNT() instead of iterating your rows in your first query. Then remember to close() both the PreparedStatement and ResultSet. Finally, I would suggest you program to the List<String> interface. Putting it all together like,

Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;

Connect to your database and initialize conn.

int count = 0;
try {
    String query1 = "SELECT COUNT(InvoiceID) FROM Invoice;";
    ps = conn.prepareStatement(query1);
    rs = ps.executeQuery();
    if (rs.next()) {
        count = rs.getInt(1);
    }
} catch (SQLException e) {
    e.printStackTrace();
} finally {
    try {
        rs.close();
    } catch (Exception ignored) {
    }
    try {
        ps.close();
    } catch (Exception ignored) {
    }
}

The above block of code is necessary to close() both rs and ps in the correct order with the finally Block.

List<String> invoiceIdList = new ArrayList<>();
try {
    String query2 = "SELECT InvoiceID FROM Invoice;";
    ps = conn.prepareStatement(query2);
    rs = ps.executeQuery();
    while (rs.next()) {
        invoiceIdList.add(rs.getString("InvoiceID"));
    }
} catch (SQLException e) {
    e.printStackTrace();
} finally { // <-- it's identical to the finally block above.
    try {
        rs.close();
    } catch (Exception ignored) {
    }
    try {
        ps.close();
    } catch (Exception ignored) {
    }
}
// now you can pass invoiceIdList elsewhere...
if (!invoiceIdList.isEmpty()) {
    doSomething(invoiceIdList);
}
like image 21
Elliott Frisch Avatar answered Oct 23 '22 10:10

Elliott Frisch