Question

Is it possible to store a mixture of object types in an ArrayList? If so how?

This is what I have tried so far:

List<Object> list = new ArrayList<Object>();

list.add(new String("Hello World"));
list.add(new Integer(1));
list.add(new Long(1l));

for (i = 0; i < list.size(); i++) {
    if (list.get(i) instanceof String){
        sqlPreparedStatement.setString((i+1), (String) list.get(i));
    } else if (list.get(i) instanceof Integer) {
        sqlPreparedStatement.setInt((i+1), (Integer) list.get(i));
    } else if (list.get(i) instanceof Long) {
        sqlPreparedStatement.setLong((i+1), (Long) list.get(i));
    }
}

But it throws a casting exception.

Thanks in advance for any input!

Was it helpful?

Solution

This is what you should have:

List<Object> list = new ArrayList<Object>();

list.add(new String("Hello World"));
list.add(new Integer(1));
list.add(new Long(1l));

for (Object obj: list) {
    if (obj instanceof String){
        sqlPreparedStatement.setString((String) obj);
    } else if (obj instanceof Integer) {
        sqlPreparedStatement.setInt((Integer) obj);
    } else if (obj instanceof Long) {
        sqlPreparedStatement.setLong((Long) obj);
    }
}

OTHER TIPS

Sorry to crash your parade, but you shouldn't be using an ArrayList of 3 (or any) different types to begin with. If the information is related, create a class that holds the related information and create an ArrayList that holds only one type: objects of this class.

Edit 1:
For instance say a class to hold the data like so:

class SqlData {
   private String textData;
   private int intData;
   private long longData;

   public SqlData(String textData, int intData, long longData) {
      this.textData = textData;
      this.intData = intData;
      this.longData = longData;
   }

   public String getTextData() {
      return textData;
   }

   public int getIntData() {
      return intData;
   }

   public long getLongData() {
      return longData;
   }

}

and used like so:

  List<SqlData> sqlDataList = new ArrayList<SqlData>();
  sqlDataList.add(new SqlData("Hello World", 1, 11L));

  for (int i = 0; i < sqlDataList.size(); i++) {
     try {
        sqlPreparedStatement.setString(i + 1, sqlDataList.get(i).getTextData());
        sqlPreparedStatement.setInt(i + 1, sqlDataList.get(i).getIntData());
        sqlPreparedStatement.setLong(i + 1, sqlDataList.get(i).getLongData());
     } catch (SQLException e) {
        e.printStackTrace();
     }
  }

Why are you making this so hard? PreparedStatement has a setObject() method - just use that:

    List<Object> list = new ArrayList<Object>();
    list.add(new String("Hello World"));
    list.add(new Integer(1));
    list.add(new Long(1l));
    for (int i = 0; i < list.size(); i++)
        sqlPreparedStatement.setObject(i + 1, list.get(i)); // NOTE: columns count from 1

NOTE: The java SQL API counts everything from 1, not from zero, so columns are numbered 1...size() and not 0...size()-1

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top