简体   繁体   中英

how to create arraylist<Object> from variable?

i need to create an arraylist of objects but need to load name of the object from variable. I have this...

public store_table(String table_name){    
switch (table_name)
  case: "table_1" table_1_list=new ArrayList<Person>; break;
  case: "table_2" table_2_list=new ArrayList<Car>; break;
...
}

and i need to create something like this:

list=new ArrayList<table_name>;

Im reading data from database with several tables using ORM. So the table_name is also name of the object.

so here is more code...

store_into table(String table_name) throws SQLException{
query="SELECT * FROM " +table_name+" OFFSET"+offset+ " FETCH NEXT 50 ROOWS ONLY";
Statement st= conn.createStetement();
Resulset rs= st.executeQuery(query);
while(rs.next()){
case "Person": 
        person=new Person (rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6));
        person_list.add(person);

        break;
    case "Car": ... 
     break;

...
}
} rs.close();
st.close();
switch (table_name){
    case "Person": 
        Singleton.getInstance().populate_tabule(person_list);
        break;
    case "car": ...
     break;
} }

and here is how i print row

public void populate_table(ArrayList<Person> list){


    Object[] row=new Object[6];
    for(int i=0;i<offset_size;i++){
        row[0]=list.get(i).getId();
        row[1]=list.get(i).getName();
        row[2]=list.get(i).getBirthday();
        row[3]=list.get(i).getEmail();
        row[4]=list.get(i).getAddress();
        row[5]=list.get(i).getSalary();

        view.setModel(row); 
    }

Your problem is that your whole "mental" model is deficient. As soon as you start creating variables that are named a1, a2, ... followed by writing code that does things explicitly for each variable - chances are that you are doing wrong.

In that sense: you don't use lists because you can; because you thought about building a mental model of the aspects in the real world that you want to express.

First of all, you should probably use some kind of ORM framework to do such things for you (your code is nothing else but attempting to build similar functionality yourself).

Beyond that; as said: you should rather step back and improve your object model.

You could make a HashMap where you have all the possible ArrayLists already made and then the key value is a string with the table name.

something like:

private final Map<String, ArrayList> TableArrays = new HashMap<>();

TableArrays.put("Person" , new ArrayList<Person>);
TableArrays.put("Car", new ArrayList<Car>);

ArrayList getTableArray(String table_name) {
    return TableArrays.get(table_name);
}

So, you want to create ArrayList s of Object s of type as table_name

switch has the limitation of comparing actual values rather than values assigned to objects.
For example, you are not allowed to do something like

switch (table_name) {
    case temp1:
        System.out.println("This is test1");
        break;
    case temp2:
        System.out.println("This is test2");
        break;
    }

You are allowed to do

switch (table_name) {
    case "test1":
        System.out.println("This is test1");
        break;
    case "test2":
        System.out.println("This is test2");
        break;
    }

I recommend moving from a switch to multiple if-else statements to solve this purpose.

So you might have a Person class as

class Person {
    int data;

    Person(int data) {
        this.data = data;
    }
}

and a Car class as

class Car {
    int data;

    Car(int data) {
        this.data = data;
    }
}

Therefore, you will create ArrayLists in the following fashion

public class Store_table {
    List<Person> personList;
    List<Car> carList;

    Store_table(Object o) {
        if (o instanceof Person) {
            personList = new ArrayList<Person>();
        } else if (o instanceof Car) {
            carList = new ArrayList<Car>();
        }
    }
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM