Create an ArrayList with multiple object types?

前端 未结 12 2552
抹茶落季
抹茶落季 2020-12-08 01:57

How do I create an ArrayList with integer and string input types? If I create one as:

List sections = new ArrayList 

        
相关标签:
12条回答
  • 2020-12-08 02:20

    You could create a List<Object>, but you really don't want to do this. Mixed lists that abstract to Object are not very useful and are a potential source of bugs. In fact the fact that your code requires such a construct gives your code a bad code smell and suggests that its design may be off. Consider redesigning your program so you aren't forced to collect oranges with orangutans.

    Instead -- do what G V recommends and I was about to recommend, create a custom class that holds both int and String and create an ArrayList of it. 1+ to his answer!

    0 讨论(0)
  • 2020-12-08 02:21

    You can always create an ArrayList of Objects. But it will not be very useful to you. Suppose you have created the Arraylist like this:

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

    and add objects to this list like this:

    myList.add(new Integer("5"));

    myList.add("object");

    myList.add(new Object());

    You won't face any problem while adding and retrieving the object but it won't be very useful. You have to remember at what location each type of object is it in order to use it. In this case after retrieving, all you can do is calling the methods of Object on them.

    0 讨论(0)
  • 2020-12-08 02:27
    List<Object> list = new ArrayList<>();
              list.add(1);
              list.add("1");
    

    As the return type of ArrayList is object, you can add any type of data to ArrayList but it is not a good practice to use ArrayList because there is unnecessary boxing and unboxing.

    0 讨论(0)
  • 2020-12-08 02:30

    User Defined Class Array List Example

    import java.util.*;  
    
    public class UserDefinedClassInArrayList {
    
        public static void main(String[] args) {
    
            //Creating user defined class objects  
            Student s1=new Student(1,"AAA",13);  
            Student s2=new Student(2,"BBB",14);  
            Student s3=new Student(3,"CCC",15); 
    
            ArrayList<Student> al=new ArrayList<Student>();
            al.add(s1);
            al.add(s2);  
            al.add(s3);  
    
            Iterator itr=al.iterator();  
    
            //traverse elements of ArrayList object  
            while(itr.hasNext()){  
                Student st=(Student)itr.next();  
                System.out.println(st.rollno+" "+st.name+" "+st.age);  
            }  
        }
    }
    
    class Student{  
        int rollno;  
        String name;  
        int age;  
        Student(int rollno,String name,int age){  
            this.rollno=rollno;  
            this.name=name;  
            this.age=age;  
        }  
    } 
    

    Program Output:

    1 AAA 13

    2 BBB 14

    3 CCC 15

    0 讨论(0)
  • 2020-12-08 02:30

    I am also newish to Java and just figured this out. You should create your own class which stores the string and integer, and then make a list of these objects. For instance (I am sure this code is imperfect, but better than arrayList):

    class Stuff {
        private String label;
        private Integer value;
    
        // Constructor or setter
        public void Stuff(String label, Integer value) {
            if (label == null || value == null) {
                return;
            }
            this.label = label;
            this.value = value;
        }
    
        // getters
    
        public String getLabel() {
            return this.label;
        }
    
        public Integer getValue() {
            return this.value;
        }
    }
    

    Then in your code:

    private ArrayList<Stuff> items = new ArrayList<Stuff>();
    items.add(new Stuff(label, value));
    
    for (Stuff item: items) {
         doSomething(item.getLabel()); // returns String
         doSomething(item.getValue()); // returns Integer
    }
    
    0 讨论(0)
  • 2020-12-08 02:30

    Just use Entry (as in java.util.Map.Entry) as the list type, and populate it using (java.util.AbstractMap’s) SimpleImmutableEntry:

    List<Entry<Integer, String>> sections = new ArrayList<>();
    sections.add(new SimpleImmutableEntry<>(anInteger, orString)):
    
    0 讨论(0)
提交回复
热议问题