1
java.util.List records = new java.util.ArrayList();
java.sql.ResultSet rs = selectestatement.executeQuery(query1);

 while (rs.next()) {

     java.util.List record = new java.util.ArrayList();

     record.add(rs.getString("WHLO").trim());
     record.add("888509018579");                            
     record.add(rs.getString("ITEM_CODE").trim());
     record.add(rs.getString("ARRIVAL_DATE").trim());
     record.add(rs.getString("PAIRS_PER_CASE").trim());  
     record.add(rs.getString("ATS").trim());
     records.add(record);   
 }

In this code, Final arraylist is the "records array". This records arraylist contents few record arrays.

How can i access the 1st element of record arraylist from the records arraylist?

1
  • Note that it's a pretty bad idea to store each row as a List; create a class to store it in and create instances from the record data, then store those. Commented May 28, 2018 at 8:48

3 Answers 3

3

Don't use raw types:

List<List<String>> records = new ArrayList<>();
List<String> record = new ArrayList<>();
...
records.add(record);

This way records.get(i) will return a List<String> instead of an Object, so you can access the elements of the inner List:

String first = records.get(0).get(0);
Sign up to request clarification or add additional context in comments.

Comments

2

What you really want is a class containing your row data.

class RecordData {
    public String whlo;
    public long someNumber = 888509018579;
    public String itemCode;
    public String arrivalDate;
    public String pairsPerCase;
    public String ats;
}

and then do

java.util.List<RecordData> records = new java.util.ArrayList<>();
while (rs.next()) {

    RecordData record = new RecordData();
    record.whlo = rs.getString("WHLO").trim();
    record.itemCode = rs.getString("ITEM_CODE").trim();
    record.arrivalDate = rs.getString("ARRIVAL_DATE").trim();
    record.pairsPerCase = rs.getString("PAIRS_PER_CASE").trim();
    record.ats = rs.getString("ATS").trim();

    records.add(record);    
}

In fact, you want to make the members private and accessible via getters and setters, and use LocalDate for the arrivalDate and int for the pairsPerCase member, but the first point is not using a List to store the retrieved values but wrap it in a business-oriented class.

Comments

0

You can do something like this

((ArrayList)records.get(0)).get(0) to access the first element of the array list that is in the first position of the records array list.

Please note that if you specify what does the records contains (in this case records will contains array lists) then you won't need to cast the element to array list.

List<List<String>> records = new ArrayList<ArrayList>();
{...}
records.get(0).get(0); //You don't need the cast because Java already knows that what it is inside records are Lists

1 Comment

use generics not just halfway through: List<List<String>> records = new ArrayList<>();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.