java - How to create a list of different data types, iterate and perform different actions based on type? -
i have list of vars made of strings, doubles , ints want store in 1 list, iterate through list , perform different actions based on type of data is.
initially thought create arraylist achieve this, of these primitive types , not objects, did not work.
i not know how many of each item list have, not think can create object hold different types this.
what best way achieve this?
class alternative way achieve can store type. in following solution have created employee class different type of variable.
public class employee { static list<employee> employeelist = new arraylist<employee>(); private int id; private string firstname; private int age; private double salary; private string department; public employee(int id, string firstname, int age, double salary, string department) { this.id = id; this.firstname = firstname; this.age = age; this.salary = salary; this.department = department; } public static void main(string[] argv) { employee employee1 = new employee(1, "pavan", 45, 20000.00, "uppal"); employee employee2 = new employee(2, "mahesh", 35, 10000.00, "uppal"); employeelist.add(employee1); employeelist.add(employee2); } }
2.other alternative create arraylist of object type
list<object> list=new arraylist<object>(); list.add(100); list.add("hi") list.add(12.0)
Comments
Post a Comment