Declaring a LinkedList in Java

前端 未结 11 1880
北荒
北荒 2021-02-20 14:26

I always learn when we declare a collection we should do, Interface ob = new Class(), if i want to use for example a LinkedList i\'ll do List ob = new LinkedL

11条回答
  •  庸人自扰
    2021-02-20 14:41

    I won't always suggest you to use generics ..... Coz sometimes you may need to wrap different objects as here....

              String str="a string";
              boolean status=false;
    
              LinkedList ll = new LinkedList();
              ll.add(str);
              ll.add(status);
    

    In some situations like case of RMI, u can only send serialized data.....and suppose you want to send a class object(which is unserialized).......There you can wrap the members of the class(primitives) in a LinkedList and pass that object as a whole.......not worrying about the huge number of arguments...... Consider for eg:

           public Class DataHouse
           {
                 public int a;
                 public String str;
                 .
                 .
                 .
           }
    

    Now Somewhere u need to pass the objects.... You can do the following....

                 DataHouse dh =new DataHouse();
                 LinkedList ll = new LinkedList();
                 ll.add(dh.a);
                 ll.add(dh.str);
    
                 // Now the content is serialized and can pass it as a capsuled data......
    

提交回复
热议问题