0

I have an ArrayList in java. I read data from a database and want to add each row to this arraylist.
My problem is that I don't know how many arguments a row has. E.g. if I read from table person, a person has a firstname, nickname, age and if I read from an other table like player there is a different number arguments.
How can I dynamically create an object where I can add my data?
For example, how can I create something like this one dynamically: Person = {"a", "b", 1} and so I have an ArrayList<Person>.
Or should I create an ArrayList of ArrayList?
Thanks in advance!

mafioso
  • 1,630
  • 4
  • 25
  • 45
  • Why not read each row into an `ArrayList` of itself? Then have `ArrayList>` of to represent all databases? – Matthew Brzezinski Nov 18 '16 at 20:20
  • So is this the best way to solve it? I have already tried it but I didn't know if it's a good way – mafioso Nov 18 '16 at 20:22
  • 1
    If you are accessing databases without having any prior knowledge about them, then this is what I would do. If you have knowledge about them before hand, then I would make appropriate classes for the database, where I would do something like `ArrayList` where `DatabaseAlpha` would keep variables for each column. – Matthew Brzezinski Nov 18 '16 at 20:23
  • To add to what user123 said, that's e.g. `List`, `List`. That would be the ideal way. – DavidS Nov 18 '16 at 20:25

4 Answers4

3

You should rather use a HashMap<String, Object> where the key of the map will be the property name and the value of the Map, the value associated to the property.

Map<String, Object> myPerson = new HashMap<String, Object>();
myPerson.add("name", "doe");
myPerson.add("age", 20L)

Optionally, you could use a Wrapper class which contains the Map. In this way, you hide the implementation and you can provide custom methods to the client such as methods to get a value without explicit casting and you could provide a name to the mapped table :

Example :

public class DBObjectMapper{
   private Map<String, Object> map = new HashMap<String, Object>();
   private String tableName;

   private DBObjectMapper(String tableName){
       this.tableName=tableName;
   }

   public add(String key, Object value){
     map.put(key,value);
   }

   public <T extends Object> T get(String key) {
      return (T) map.get(key);
   }

   public String getTableName(){
      return tableName;
   }
}

Now, clients don't need to do explicit casts when retrieving a value from the mapper:

   DBObjectMapper mapper = new DBObjectMapper("person");
   mapper.add("isEnabled", Boolean.valueOf(true);
   mapper.add("name", "doe");
   String mappedTable = mapper.getTableName();
   Boolean b = mapper.get("isEnabled");
   String string = mapper.get("name");
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • How can I iterate through the `Map`? I mean without calling mapper.get for every property? – mafioso Nov 18 '16 at 20:43
  • The `Map` interface has a `keys` function that'll return all the key values. You can also iterate with a for loop with `for (Map.Entry<...> entry : map.entrySet())`. See http://stackoverflow.com/questions/8689725/map-entry-how-to-use-it – Shaun Nov 18 '16 at 20:56
  • If you want to display a specific information, for example, the name, why iterate over all entries ? – davidxxx Nov 18 '16 at 21:10
2

Are you asking for a HashMap?

Map<String, Object> person = new HashMap<String, Object>();
person.add("a", 1);
person.add("b", ...)

See

https://www.tutorialspoint.com/java/java_hashmap_class.htm http://beginnersbook.com/2013/12/hashmap-in-java-with-example/

Lots of good resources out there.

Shaun
  • 3,777
  • 4
  • 25
  • 46
  • @mafioso, note that with this solution (or `List` for that matter) you're dealing with `Object` values which may not be what you want, depending on your use case. – DavidS Nov 18 '16 at 20:26
0

Actually you can store an arraylist for each person object. Something like

Person p = new Person;
p.addAttribute(dbValue);

What addAttribute method do is:

class Person{
//init myPersonArraylist
....
private void addAttribute(String attribute){
myPersonArraylist.add(attribute);
}
}
ozanonurtek
  • 306
  • 5
  • 18
0

If you have no prior knowledge to opening the database, how many rows there will be, columns, or types of columns. Then I would create an ArrayList<ArrayList<String>> and just read eachline, and split on commas (or which ever delimiter you have). This way you will be able to dynamically adjust for as many columns as needed, and this will work in a generic case for any database which you are accessing.

If you have knowledge about the database then I would create appropriate classes for it. In your example Person.

class Person {
    String firstName;
    String nickname;
    int age;
}

Then parse each row into a Person class and add that into an ArrayList<Person>.

Matthew Brzezinski
  • 1,685
  • 4
  • 29
  • 53