I'm wondering if i can have an array ( or basically a table ) with each of its element being a set of objects of different types. I mean i want to create something like this ( i know it's incorrect in syntax, just wanna demonstrate my idea ) :
List<String, int, double, Date, ... , etc > list_name
I'm doing this to ensure that when I save all these information to my database, i will have all these information in the same entry in the database. This is because i did some web scraping from different sites to gather all these data, i.e. in the list, string may be from site A, int may be from site B, etc. I found some information may be missed for some reasons ( say, for a particular element of the list, String from site A may be missing, other data are just there, perfectly fine. ). If i store these data into seperated lists, i'm afraid there will be some mismatch of data.
Now my solution is to create a class, say ClassA :
ClassA{
public String info1
public int info2
public double info3
..
..
public wtever info
}
and then i will have a of list of ClassA
I'm wondering if there is a better way to achieve this?
A vector will hold an object of a single type, and only a single type.
Storing and Accessing Class Objects in a Vector The primary difference in using vectors for storing class objects versus using an array, other than all the differences between vectors and arrays, is how you declare the vector to store class objects. We can use the same Student class definition as shown earlier.
Is an array a container? Arrays hold a set of elements of the same type in a contiguous memory location so, do they not qualify as containers? In most languages, an array would indeed qualify as a container.
Your way of creating a class for holding these different type of properties seems good.
Then you can use an ArrayList
, an implementation of List
interface like this:
ArrayList<ClassA> list = new ArrayList<ClassA>();
And then you can populate and manipulate it easily.
For your database operations, I suggest you to use DAO(data access object) classes and mapper classes to facilitate your persistence operations with your data source.
I'm doing this to ensure that when I save all these information to my database, i will have all these information in the same entry in the database.
Yes, you've done the correct thing by creating a class to hold all the values.
And, as you've already noted, you can then create a List
of this objects if you wish to perform multiple database insertions/updates.
Many persistence schemes operate on the basis of defining classes (known as DAOs) to represent the data stored in individual tables. Using a persistence provider that supports annotating classes (such as Hibernate) can really simplify your interaction with a database. I'd recommend you research this topic in more detail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With